Yii Active Record CExistValidator no parece hacer nada
Frecuentes
Visto 679 equipos
0
Estoy tratando de usar la regla de validación existente para garantizar que se cumpla mi relación (es decir, event_id en la tabla EventOther existe en la tabla Event). Estoy tratando de usar (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)')
);
y la creación de una nueva entrada en la base de datos (el registro con id 17 no existe):
$ev = new EventOther;
$ev->event_id = 17;
$ev->location_id = 1;
$ev->location = "Test Location - hardcoded";
if(!$ev->save()) print_r($ev->getErrors());
que siempre da como resultado 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)
Además, la validación aún pasa si $ev->event_id
no está configurado, lo que debido a 'allowEmpty'=>false
no debería.
Event::model()->exists("id=2");
(donde existe un registro con id 2) devuelve 1 mientras Event::model()->exists("id=17");
devuelve vacío.
¿Entendí mal/utilicé mal CExistValidator? Toda ayuda es muy apreciada.
1 Respuestas
2
array('event_id','exists','allowEmpty'=>false,
debiera ser:
array('event_id','exist','allowEmpty'=>false,
Respondido 01 Jul 12, 04:07
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php yii or haz tu propia pregunta.
¡Gracias! ¡No puedo creer cuánto tiempo pasé en este simple error tipográfico! Muchas gracias, marcaré la respuesta como aceptada tan pronto como pueda :) - Bart Platak