Editor create instance writing to two different tables?

Editor create instance writing to two different tables?

nigel pasconigel pasco Posts: 37Questions: 6Answers: 0
edited August 2014 in Free community support

Hi everyone,
Has anyone managed to use the "editor_create" server script to add data to mulitple tables? For example, I wish to add data inputted from a "create" lightbox to a table "projects", then add the newly created "project_id" into the table "persons".

Something like this?!?

$editor = Editor::inst( $db, 'project', 'project_id' )
    ->fields(
        Field::inst( 'project.numero' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'project.title' )->validator( 'Validate::notEmpty' )
);
$editor2 = Editor::inst( $db, 'persons', 'person_id' )
    ->fields(
        Field::inst( 'person.numero' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'person.project_id' )
);
$data = $editor
$data2 = $editor2
    ->process($_POST)
    ->data();
echo json_encode( $data );
echo json_encode( $data2 );

I am sure it is straight forward, but I can not find anything in regards to this.
Looking forward to some feedback.
thanks,
nige

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    I'm not aware of anyone having done it using that method and it wasn't really designed to operate in that way.

    There are a few issues with the above code:

    • process() is only being called on one instance
    • The first json_encode() is just encoding the $editor variable, which I have no idea what PHP will make of that!
    • It is outputting invalid JSON.

    The most complex part I think you'll find is the data gathering from the two tables - the code above, for example is outputting invalid JSON - {...}{...} is not valid JSON - it is two valid JSON objects concatenated together.

    Is there a reason you can't do this as a leftJoin() which will automatically do the insert into the second table if required?

    Allan

  • nigel pasconigel pasco Posts: 37Questions: 6Answers: 0

    Yes, that has worked fine.

    I had been using Joins, but had not inserts could be done so simply using them - I had assumed a second editor class would be required. Such a simple solution - obvious now that I look at it?!?

    Thank you for the response Allan

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Good to hear that worked :-). Always nice when things turn out to be easier than expected!

    Allan

This discussion has been closed.