Attribute Valitator

Hi,

I have a database setup like this:

Table A


ID

Table B


ID

A_ID - NOT NULL

Table C


ID

A_ID - NOT NULL

B_ID - DEFAULT NULL

A has many B

B belongs to A

C belongs to A

C belongs to B

B has one C

the record of C could be missing completely in some cases.

And now the problem:

Ik want to check before i save a new record of C(with A_ID and B_ID both set) that A_ID in Table B is the same as A_ID in Table C. How do i create a validator for this? And i want to check if another record of C already is connected to B because there can only be one relation between C and B.

edit:

this works


public function myValidator($attribute,$params)

    {

        if( $this->A_ID != $this->B->A_ID)

        {

            $this->addError($attribute, 'id\'s not the same');

        }

    }

but is this ok? because i can use any attribute i want and it always checks the same thing.

Thanks!

Your validator looks fine. I would suggest changing the schema of table C a bit to:




Table C

-------

B_ID - DEFAULT NULL

A_ID - NOT NULL



If it is a HAS_ONE relation to B then the primary key column should also be a foreign key column. This way it is going to be recognized as a HAS_ONE automatically. Since the primary key is unique, there can’t be two C records pointing to the same B.

The relation between B and C is a 0 or 1 relation to be exact. B_ID in table C can be NULL.