issue with writing to compound key table

issue with writing to compound key table

nlooijenlooije Posts: 49Questions: 10Answers: 0

I am trying to write a record into a compound key table.

The table has three fields: 'ID', 'description' and 'languageText_KEY'.
The table contains translations for another table and is linked by 'ID'.
'ID and 'languageText_KEY' are the compound keys.

I am getting a validation error: "When inserting into a compound key table, all fields that are part of the compound key must be submitted with a specific value."

I don't understand why this error is generated because i can see in the headers of the POST request that all compound key fields posted have specific values.

The PHP code used:

$editor = Editor::inst($db, 'tblLogTrendKonfig_Text', ["ID","languageText_KEY"]);
$editor->debug(false);
$editor->write(true);

$f = Field::inst('tblLogTrendKonfig_Text.ID');
$f->setFormatter(Format::ifEmpty(null));
$fields[] = $f;

$f = Field::inst('tblLogTrendKonfig_Text.description');
$f->setFormatter(Format::ifEmpty(null));
$fields[] = $f;

$f = Field::inst('tblLogTrendKonfig_Text.languageText_KEY');
$f->setFormatter(Format::ifEmpty(null));
$fields[] = $f;

$editor->fields($fields);
$editor->process($_POST)->json();

I cannot change the table design unfortunately otherwise I would.
To me it make more sense to have a unique ID and a TEXT_ID field linked to the the other table.
But unfortunately I cannot change it.

Anyone know why this validation error is generated?

Answers

  • allanallan Posts: 63,679Questions: 1Answers: 10,498 Site admin

    Honestly, no idea! What you have there looks entirely sensible, and the example operates as expected, so I'm not entirely sure what is going wrong there I'm afraid.

    What version of the PHP libraries are you using?

    This is where the pkey validation is done. It would be interesting to know if it throwing due to not finding the field, or if the apply is returning false.

    Are you able to add some debug statements into your local copy? I'd do it here, but the example I have appears to be working correctly.

    Allan

  • nlooijenlooije Posts: 49Questions: 10Answers: 0

    I am using version 2.2.2.

    I debugged the pkey validation and turns out it was not finding the field.
    As a first check, I added the table part to the primary keys and then it worked!

    $editor = Editor::inst($db, 'tblLogTrendKonfig_Text', ["tblLogTrendKonfig_Text.ID","tblLogTrendKonfig_Text.languageText_KEY"]);
    

    Thanks for pointing me in the right direction.

  • allanallan Posts: 63,679Questions: 1Answers: 10,498 Site admin

    Hmmm - interesting! The example doesn't use that so there is something I'm still missing, but that is something for me to look at.

    Good to hear you've got a workaround at the moment.

    Allan

  • nlooijenlooije Posts: 49Questions: 10Answers: 0

    Maybe it is because I am specifying the table parts in the field instances it then expects the primary keys to also have the table parts?

  • allanallan Posts: 63,679Questions: 1Answers: 10,498 Site admin

    That doesn't seem to be an issue in this example though.

    Editor::inst( $db, 'users_visits', array('user_id', 'visit_date') )
        ->debug( true )
        ->field(
            Field::inst( 'users_visits.user_id' )
                ->options( Options::inst()
                    ->table( 'users' )
                    ->value( 'id' )
                    ->label( array('first_name', 'last_name') )
                )
                ->validator( Validate::dbValues() ),
            Field::inst( 'users_visits.site_id' )
                ->options( Options::inst()
                    ->table( 'sites' )
                    ->value( 'id' )
                    ->label( 'name' )
                )
                ->validator( Validate::dbValues() ),
    

    Allan

Sign In or Register to comment.