【问题标题】:Yii Active Record CExistValidator doesn't seem to do anythingYii Active Record CExistValidator 似乎没有做任何事情
【发布时间】:2012-07-01 03:47:02
【问题描述】:

我正在尝试使用存在验证规则来确保满足我的关系(即表 EventOther 中的 event_id 存在于表 Event 中)。我正在尝试使用(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)')
    );

以及在数据库中创建新条目(id 为 17 的记录不存在):

    $ev = new EventOther;
    $ev->event_id = 17;
    $ev->location_id = 1;
    $ev->location = "Test Location - hardcoded";
    if(!$ev->save()) print_r($ev->getErrors());

这总是导致 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)

此外,如果$ev->event_id 未设置,验证仍然通过,由于'allowEmpty'=>false 它不应该设置。

Event::model()->exists("id=2");(其中存在 id 为 2 的记录)返回 1,而 Event::model()->exists("id=17"); 返回空。

我是否误解/误用了 CExistValidator?非常感谢所有帮助。

【问题讨论】:

    标签: php yii


    【解决方案1】:

    array('event_id','exists','allowEmpty'=>false,

    应该是:

    array('event_id','exist','allowEmpty'=>false,

    【讨论】:

    • 谢谢!我无法相信我在这个简单的错字上花了多少时间!非常感谢,我会尽快将答案标记为已接受:)
    猜你喜欢
    • 2016-04-09
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 2019-11-26
    • 2019-12-08
    • 2018-10-08
    • 2020-12-21
    • 2023-04-06
    相关资源
    最近更新 更多