ActiveRecord:link() 方法会失败或传递不正确的数据吗?
Will ActiveRecord:link() method fail or pass on on incorrect data?
当使用 ActiveRecord:link()
方法时,我是否被迫以 "secure" 方式更新关系:
foreach ($postData['User']['lab'] as $labId) {
$lab = Lab::findOne($labId);
if ($lab instanceof \app\models\Lab) {
$model->link('lab', $lab);
}
}
或者我可以这样做,"lazy" 方式:
foreach ($postData['User']['lab'] as $labId) {
$model->link('lab', Lab::findOne($labId));
}
不关心额外检查?
如果使用 null
进行提要(因为调用 Lab::findOne($labId)
在某些迭代中找不到给定的记录),link
会失败还是继续?
就我从代码中看到的,它会给出错误。
有或没有关系通过它从 $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.
当使用 ActiveRecord:link()
方法时,我是否被迫以 "secure" 方式更新关系:
foreach ($postData['User']['lab'] as $labId) {
$lab = Lab::findOne($labId);
if ($lab instanceof \app\models\Lab) {
$model->link('lab', $lab);
}
}
或者我可以这样做,"lazy" 方式:
foreach ($postData['User']['lab'] as $labId) {
$model->link('lab', Lab::findOne($labId));
}
不关心额外检查?
如果使用 null
进行提要(因为调用 Lab::findOne($labId)
在某些迭代中找不到给定的记录),link
会失败还是继续?
就我从代码中看到的,它会给出错误。
有或没有关系通过它从 $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.