使用ActiveRecord:link()
方法时,我是否被迫以"安全"的方式更新关系:
foreach ($postData['User']['lab'] as $labId) {
$lab = Lab::findOne($labId);
if ($lab instanceof appmodelsLab) {
$model->link('lab', $lab);
}
}
或者我可以这样做,"懒惰"的方式:
foreach ($postData['User']['lab'] as $labId) {
$model->link('lab', Lab::findOne($labId));
}
不关心额外的检查?
如果link
的提要带有null
,会失败还是传递(因为对Lab::findOne($labId)
的调用在某些迭代中找不到给定的记录)?
据
我从代码中看到,它会给出错误。
有或没有 via 从 $model
调用方法,如果Lab::findOne($labId)
null
- 你会得到一个错误。
if ($relation->via !== null) {
if ($this->getIsNewRecord() || $model->getIsNewRecord()) {
......
else {
$p1 = $model->isPrimaryKey(array_keys($relation->link));
......
正如文档所说:
Note that this method requires that the primary key value is not null.