Validate::unique with table and field options fails during update

Validate::unique with table and field options fails during update

janosijanosi Posts: 7Questions: 4Answers: 0

Dear community

I have encountered a problem with the server-side validator of the editor.

I have a left-joined table in which one of the fields is required to be unique.
The field is made unique like this:

$tParent  = "crm_table1";
$t2 = "crm_options";

Editor::inst( $db, $tParent )
    ->pkey( 'uid' )
    ->fields(
        Field::inst( $tParent . '.uid' ),       
        Field::inst( $tParent . '.mezo_id' )
            ->validator( 'Validate::unique', array(
                "table"  => "someTable",
                "field"  => "someField",
        "message" => "Make it unique"
            ))
        ->options( $t2, 'mezo_id', 'cimke' ),
        Field::inst( $t2 . '.cimke' )
    )
    ->leftJoin( $t2, 
                $t2 . '.mezo_id', 
                '=', 
                $tParent . '.mezo_id' )  
    ->process( $_POST )
    ->json();

The setup works just fine when creating new records and when deleting old records, but fails during update.

The actual error message appreas in the editor window and is the following:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'crm_table1.uid' in 'where clause'.

This error dissapears if I remove the validate::unique. It appears that the editor tries to add the record UID to the where clause in the unique table, however this results in invalid SQL statement. I can not add (or do not know how) field called 'crm_table1.uid' to 'someTable'.

What is the recommended practice for unique fields on left-joined tables?

This question has an accepted answers - jump to answer

Answers

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

    Hi,

    Could you say what your someTable and someField parameters are please? It looks like what you have should work okay.

    Unfortunately I'm traveling at the moment so don't have access to my dev setup to try it out myself, but when I get back I'll try putting together a test using the unique validator and joined tables.

    Allan

  • janosijanosi Posts: 7Questions: 4Answers: 0

    Dear Allan

    "someTable" is a filtered version of "crm_table1". I only need 'mezo_id' to be unique in this filtered table.

    'someField' is equal to 'mezo_id'

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Sorry for the long delay with this. Are you still having a problem with this (or presumably moved on now)?

    Allan

This discussion has been closed.