【问题标题】:Laravel - Eloquent - temporarily disable touchOwnersLaravel - Eloquent - 暂时禁用 touchOwners
【发布时间】:2014-10-09 23:15:27
【问题描述】:

我有模型

用户

在保存时对每条记录执行 touchOwners。

<?php

class User extends Eloquent {

  protected $touches = array('userDesk');

  [...]

}

当我要新建一条记录时,问题来了:eloquent 尝试去触摸一个相关表中不存在的字段(既然用户是新创建的,那怎么会有一条记录引用给相关表中的这个用户?!?...)。

如何暂时禁用此功能以允许我创建新用户而不会引发异常:

调用未定义的方法 Illuminate \ Database \ Eloquent \ Collection :: touchOwners()

?

我试过这个:

$user->setTouchedRelations(array());

但当然它没有用......

【问题讨论】:

  • of course it did not work 是什么意思?这正是您所需要的。

标签: php laravel eloquent


【解决方案1】:

要保存没有touching 的模型,将false 传递给save 方法:

$someModel = new SomeModel;
... // do something with your model

$someModel->save(['touch' => false]);

当然setTouchedRelations 也可以:

$someModel = new SomeModel;
... // do what you need

$someModel->setTouchedRelations([]);
$someModel->save();

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 2014-10-10
    • 2017-12-29
    • 2021-04-19
    • 2012-12-25
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多