Octobercms onSave 更新其他模型
Octobercms onSave update other model
我需要在保存贷款时更新模型关系中书籍的 status
字段,但它不起作用。
我的代码
class Lending extends Model
{
/**
* @var string The database table used by the model.
*/
public $table = 'ribsousa_library_lendings';
/*
* Relations
*/
public $belongsToMany = [
'books' => [
'Ribsousa\Library\Models\Book',
'table' => 'ribsousa_library_lendings_books',
'order' => 'title desc'
]
];
public function afterSave()
{
$this->book->status = 1;
$this->book->save();
}
}
错误:
`"Indirect modification of overloaded property Ribsousa\Library\Models\Lending::$book has no effect" on line 95 of C:\wamp64\www\iepm.dev\plugins\ribsousa\library\models\Lending.php`
我试过这个 code
和 its working fine
你能试试这个吗。
它在我的 model which model I am saving
里面,所以当 saving is done
我在 updating related demo model's
status 字段并且看起来 its working
.
public $belongsTo = [
'demo' => 'HardikSatasiya\DemoTest\Models\Demo',
'user' => 'RainLab\User\Models\User'
];
/**
* @var string The database table used by the model.
*/
public $table = 'hardiksatasiya_demotest_relation';
public function afterSave() {
$this->demo->status = 1;
$this->demo->save();
}
更新
看来你有 更新的答案 和它的关系 is Many to Many
所以现在我有 no proper solution
一次更新多条记录,因为集合update
方法没有生成proper query structure
所以它抛出错误collection update
.
但是 我有其他解决方案 can fire multiple queries
但目前 seems working solution for your issue
.
你的关系是 belongsToMany
所以我们需要不同的方法。
public function afterSave() {
$this->books()->each(function($book) {
$book->update(['status' => 1]);
// if you get mass assign error -use this
// $book->status = 1;
// $book->save();
});
}
Make sure your Book
model don't have any afterSave
logic and if it has then just make sure it don't update your parent model in your case Lending
, as it will call infinite loop.
请试试这个并告诉我们。
我需要在保存贷款时更新模型关系中书籍的 status
字段,但它不起作用。
我的代码
class Lending extends Model
{
/**
* @var string The database table used by the model.
*/
public $table = 'ribsousa_library_lendings';
/*
* Relations
*/
public $belongsToMany = [
'books' => [
'Ribsousa\Library\Models\Book',
'table' => 'ribsousa_library_lendings_books',
'order' => 'title desc'
]
];
public function afterSave()
{
$this->book->status = 1;
$this->book->save();
}
}
错误:
`"Indirect modification of overloaded property Ribsousa\Library\Models\Lending::$book has no effect" on line 95 of C:\wamp64\www\iepm.dev\plugins\ribsousa\library\models\Lending.php`
我试过这个 code
和 its working fine
你能试试这个吗。
它在我的 model which model I am saving
里面,所以当 saving is done
我在 updating related demo model's
status 字段并且看起来 its working
.
public $belongsTo = [
'demo' => 'HardikSatasiya\DemoTest\Models\Demo',
'user' => 'RainLab\User\Models\User'
];
/**
* @var string The database table used by the model.
*/
public $table = 'hardiksatasiya_demotest_relation';
public function afterSave() {
$this->demo->status = 1;
$this->demo->save();
}
更新
看来你有 更新的答案 和它的关系 is Many to Many
所以现在我有 no proper solution
一次更新多条记录,因为集合update
方法没有生成proper query structure
所以它抛出错误collection update
.
但是 我有其他解决方案 can fire multiple queries
但目前 seems working solution for your issue
.
你的关系是 belongsToMany
所以我们需要不同的方法。
public function afterSave() {
$this->books()->each(function($book) {
$book->update(['status' => 1]);
// if you get mass assign error -use this
// $book->status = 1;
// $book->save();
});
}
Make sure your
Book
model don't have anyafterSave
logic and if it has then just make sure it don't update your parent model in your caseLending
, as it will call infinite loop.
请试试这个并告诉我们。