Yii-用于外部密钥的CExistValidator

Yii-用于外部密钥的CExistValidator,yii,Yii,在Yii的一个简单项目中,我有一个模型: Checkins.php * The followings are the available columns in table 'checkins': * @property integer $id * @property integer $user_id * @property integer $item_id * @property double $lat * @property double $long $user\u id和$it

在Yii的一个简单项目中,我有一个模型:

Checkins.php

 * The followings are the available columns in table 'checkins':
 * @property integer $id
 * @property integer $user_id
 * @property integer $item_id
 * @property double $lat
 * @property double $long
$user\u id和$item\u id这两个值属于其他两个表:

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
        'item' => array(self::BELONGS_TO, 'Items', 'item_id'),
    );
}
我定义了一些验证器:

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('user_id, item_id, lat, long', 'required'),

        array('item_id', 'exist', 'on'=>'create', 'attributeName'=>'id', 'className'=>'Items'),

        array('user_id, item_id', 'numerical', 'integerOnly'=>true),
        array('lat, long', 'numerical'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, user_id, item_id, lat, long', 'safe', 'on'=>'search'),


    );
}
在actionCreate方法save execute中,所有验证器都在工作,但不是设计用于检查模型项中是否存在外部密钥的验证器

array('item_id', 'exist', 'on'=>'create', 'attributeName'=>'id', 'className'=>'Items'),
如果我试图保存一个签入,该签入在item_id中有一个值,而在Items_id中没有相同的id,那么我不会发现任何验证错误

这是正确的方法吗


谢谢

我认为这很可能是因为您在保存之前没有将模型的场景设置为“创建”,我只是猜测,因为您没有在控制器的actionCreate中附加$checkin->save代码。。另一个验证很可能有效,因为它们没有设置为特定的场景,即它们将在所有验证中有效

例如,如果没有id为5的项目,请输入以下代码

    $checkin = new Checkin();
    $checkin->text = 'test';
    $checkin->item_id = 5;
    if (!$checkin->validate()){
        print_r($checkin->errors);
    }
    $checkin = new Checkin();
    $checkin->scenario = 'create'; 
    //or you can set it in the instantiation 
    //$checkin = new Checkin('create');
    $checkin->text = 'test';
    $checkin->item_id = 5;
    if (!$checkin->validate()){
        print_r($checkin->errors);
    }
将正常工作,因为签入的方案未设置为“创建”,默认设置为“插入”。但我尝试了下面的代码

    $checkin = new Checkin();
    $checkin->text = 'test';
    $checkin->item_id = 5;
    if (!$checkin->validate()){
        print_r($checkin->errors);
    }
    $checkin = new Checkin();
    $checkin->scenario = 'create'; 
    //or you can set it in the instantiation 
    //$checkin = new Checkin('create');
    $checkin->text = 'test';
    $checkin->item_id = 5;
    if (!$checkin->validate()){
        print_r($checkin->errors);
    }
这将导致验证错误


是否可以粘贴模型保存代码?

是的,如果要确保item_id属性需要在另一个表中具有相应的id,这是正确的方法。奇怪的是,我尝试在我的代码中进行测试,结果很好。你确定在保存之前没有忘记设置场景吗?佩特拉,谢谢你的回答。是的,它正在工作。我不清楚,如果不使用$checkin->scenario='create'设置,actionCreate中的场景是否不是'create';再次感谢!不客气。别忘了把问题标为已回答。