I’m trying to use exist validation rule to ensure that my relation is met (i.e. event_id in table EventOther exists in table Event). I’m trying to use (EventOther.php)
public function rules(){
return array(
array('event_id','exists','allowEmpty'=>false,
'attributeName'=>'id','className'=>'Event',
'message'=>'Specified event does not exist. (event_id incosistent)')
);
and the create new entry in the database (record with id 17 does not exist):
$ev = new EventOther;
$ev->event_id = 17;
$ev->location_id = 1;
$ev->location = "Test Location - hardcoded";
if(!$ev->save()) print_r($ev->getErrors());
which always results in CDbException:
CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`fiss`.`event_other`, CONSTRAINT `event_other_ibfk_1` FOREIGN KEY (`event_id`) REFERENCES `event` (`id`)). The SQL statement executed was: INSERT INTO `event_other` (`event_id`, `location_id`, `location`) VALUES (:yp0, :yp1, :yp2)
Moreover, the validation still passes if $ev->event_id is not set, which due to 'allowEmpty'=>false it should not.
Event::model()->exists("id=2"); (where record with id 2 exists) returns 1 while Event::model()->exists("id=17"); returns empty.
Did I misunderstand/misuse CExistValidator? All help is much appreciated.
array('event_id','exists','allowEmpty'=>false,should be:
array('event_id','exist','allowEmpty'=>false,