在一个使用 Yii 的简单项目中,我有一个模型:
签到.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_id 和 $item_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() 执行时,所有验证器都在工作,但不是用于检查模型项目中是否存在外部密钥的验证器
array('item_id', 'exist', 'on'=>'create', 'attributeName'=>'id', 'className'=>'Items'),
如果我尝试保存在 item_id 中具有值的 Checkins 而在 Items 中没有相同的 id,我不会出现任何验证错误。
这是正确的方法吗?
谢谢