Create new content in Table 1 and update Table 2 with new values created in Table 1?

Create new content in Table 1 and update Table 2 with new values created in Table 1?

cachiapcachiap Posts: 40Questions: 5Answers: 0

Hi Allan,

I have two tables Client Table (1) and Location Table (2).

I want to create a new entry in Table 1 (Client Table) and then add a row in Table 2 (Location Table) using the Index key from Table 1 as a Foreign key in Table 2 as well as use the Name created in Table 1 as a Name in Table 2.

A Client can potentially have several locations associated with it and I need to create those using Editor in Table 2. Before I can add new locations to this new Client however, Table 2 requires the new Client Name (from Table 1) and ID (the Index key from Table 1 used as the Foreign key in Table 2) set up in Table 2

Is this possible in DataTables Editor? I am using DataTable 1.10.7 and Editor 1.4.2

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    Thanks for the description of what you are looking to do. Unfortunately, at this time, no, using the primary key value from the first table in the joined table is not something that the Editor PHP libraries support.

    However, that does make a lot of sense and I'll take a look at what would be involved in adding support for that.

    Regards,
    Allan

  • cachiapcachiap Posts: 40Questions: 5Answers: 0

    Thanks for your quick response Allan. I'll look at how to do this with PHP. I think I saw a way using something called "last value added". I'll let you know if this exists or not.

    Thanks again for your time.

    Cheers,

    Paul

  • cachiapcachiap Posts: 40Questions: 5Answers: 0

    Allan,

    I found that MySQL allows you to INSERT the last Key Index into a new table using the function LAST_INSERT_ID(). I can create a new client in Table 1 and pass the newly created Key Index on to a sequential INSERT statement for Table 2. I will not need to pass on the client name as I was originally planning. I am going to incorporate these inserts into a BEGIN; COMMIT; transaction.

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Editor does have the id that is created internally - the key question is how to expose that value in such a way that it can be used.

    At the moment my only real option for doing that is to use some kind of static property that would tell Editor that it should use that value:

    Field::inst( 'joined.field' )
      ->setValue( Editor::PKEYVALUE )
    

    or something like that.

    Allan

  • cachiapcachiap Posts: 40Questions: 5Answers: 0

    Allan,

    Thanks again. I'll try to see what I can do with your suggestion. If I have any success I'll be sure to let you know.

    Cheers,

    Paul

This discussion has been closed.