【问题标题】:Laravel save() method returning true but not updating recordsLaravel save() 方法返回 true 但不更新记录
【发布时间】:2015-03-12 10:19:50
【问题描述】:

我正在使用 Eloquent 更新我的表 Opportunity,

机会模型

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Opportunity extends Model {

protected $primaryKey = 'OpportunityID';

protected $table = 'opportunitys';
// relationships
public function csfs()
{
    return $this->hasMany('App\Csf', 'opportunityID');
}

public function benefits()
{
    return $this->hasMany('App\Benefit', 'opportunityID');
}

public function risks()
{
    return $this->hasMany('App\Risk', 'opportunityID');
}

public function projects()
{
    return $this->belongsTo('App\Project', 'projectID');
}

public static function createNewOpportunity($input, $projectID)
{
    $opportunity = new Opportunity;
    $opportunity->value = $input['value'];
    $opportunity->margin = $input['margin'];
    $opportunity->duration = $input['duration'];
    $opportunity->tender_type = $input['tender_type'];
    $opportunity->likelihood_of_success = $input['likelihood_of_success'];
    $opportunity->scope_of_work = $input['scope_of_work'];
    $opportunity->deliverables = $input['deliverables'];
    $opportunity->projectID = $projectID;
    $opportunity->high_level_background = $input['high_level_background'];
    if($opportunity->save())
        {
            Opportunity::leadSalesOppComplete($projectID);
            return true;
        };

}

public static function leadSalesOppComplete($projectID)
{
    $task = Lead::where('projectID', '=', $projectID)->first();
    $task->sales_opp = true;
    return $task->save();
}

}

public function updateOpportunity(Request $request, $id) {

我得到了 id 并找到了机会。

$something = Opportunity::find($id);

我已经死了,扔了这个,我得到了这个

Opportunity {#259 ▼
 #primaryKey: "OpportunityID"
#table: "opportunitys"
#connection: null
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:12 [▼
"opportunityID" => 11
"value" => 0
"margin" => 0
"tender_type" => ""
"likelihood_of_success" => 0
"high_level_background" => ""
"scope_of_work" => ""
"deliverables" => ""
"duration" => ""
"projectID" => 6
"created_at" => "2015-03-11 17:45:47"
"updated_at" => "2015-03-11 17:45:47"
 ]
  #original: array:12 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}

这是正确的。然后我用

更新这些
    $something->margin = $request['margin'];
    $something->duration = $request['duration'];
    $something->tender_type = $request['tender_type'];
    $something->likelihood_of_success = $request['likelihood_of_success'];
    $something->scope_of_work = $request['scope_of_work'];
    $something->deliverables = $request['deliverables'];
    $something->high_level_background = $request['high_level_background'];

现在,如果我死了并倾倒,我会得到

Opportunity {#259 ▼
#primaryKey: "OpportunityID"
#table: "opportunitys"
#connection: null
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:12 [▼
"opportunityID" => 11
"value" => "25000"
"margin" => "0"
"tender_type" => "Proposal"
"likelihood_of_success" => "0"
"high_level_background" => ""
"scope_of_work" => ""
"deliverables" => ""
"duration" => ""
"projectID" => 6
"created_at" => "2015-03-11 17:45:47"
"updated_at" => "2015-03-11 17:45:47"
]
#original: array:12 [▼
  "opportunityID" => 11
  "value" => 0
  "margin" => 0
  "tender_type" => ""
  "likelihood_of_success" => 0
  "high_level_background" => ""
  "scope_of_work" => ""
  "deliverables" => ""
  "duration" => ""
  "projectID" => 6
  "created_at" => "2015-03-11 17:45:47"
  "updated_at" => "2015-03-11 17:45:47"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}

我只更改了显示更改的值。

我现在运行

$something->save();

当我死掉并转储它时它返回 true。

但数据库中没有任何记录发生变化。

有什么想法吗?

来自 tinker 的两张图片

【问题讨论】:

  • 什么是数组$request?里面有什么值吗?
  • 如果是Request的注入实例,尝试通过调用input()来获取值,比如$request->input('margin')。
  • 如何检查数据库中的记录没有变化?
  • 这很棘手。但我认为这个问题与你 dd() 和 save() 有关。我怀疑 dd() 只是停止执行 php 脚本。不打断保存,直接查数据库怎么样?
  • 非常有趣。当您尝试更新记录时,也许有一些数据库事务正在进行中?另外,您可以尝试从php artisan tinker 更新记录吗?

标签: php laravel eloquent laravel-5


【解决方案1】:

机会模型中的这一行解决了这个问题。

Protected $primaryKey = "opportunityID";

虽然很难理解为什么仍然可以检索数据并创建新记录。

【讨论】:

  • 感觉好像和你的数据库/表的编码有关。 save() 方法使用来自 Laravel 的查询构建器的 where 来创建具有对象属性的查询。所以这最终并不是真正的 Laravel,它只是用你设置的键进行查询。现在你的表格编码可以改变一些事情,utf8_general_ci 一般建议避免这种问题。
【解决方案2】:

我遇到了一个非常相似的问题,这篇文章引导我找到了解决方案。我也覆盖了primaryKey

环境:

SELECTINSERT 操作与 protected $primaryKey = 'USER_ID'; 一起工作,但是,即使 save() 返回 trueUPDATE 操作也不起作用。

找到这篇文章后,我改变了大小写。 protected $primaryKey = 'user_id';,Whammy,所有三个操作都有效!我希望我有一个更可靠的解释为什么会这样。当我创建表格时,我清楚地使用了上面的USER_ID VARCHAR2(50 BYTE) NOT NULL

【讨论】:

  • 这就解决了我的问题!但不知道我的代码到底发生了什么!!!还是谢谢
【解决方案3】:

回答标题中的问题:

$user = \App\User::find(1);
$boolean = $user->save();

这将返回 true。保存总是返回 true,除非更新或保存没有成功(因为找不到 user_id 或类似的东西)。它不检查属性是否已更改。要查看属性是否已更改,请参阅Laravel check if updateOrCreate performed update

这里有点 Laravel 雄辩的 Model.php :

    // If the model already exists in the database we can just update our record
    // that is already in this database using the current IDs in this "where"
    // clause to only update this model. Otherwise, we'll just insert them.
    if ($this->exists) {
      $saved = $this->isDirty() ?
         $this->performUpdate($query) : true;
    }


    // ...

    return $saved;

isDirty() 如果模型属性已更改且未保存,则为真。所以如果isDirty() 为假,模型将不会更新并返回真。

【讨论】:

    【解决方案4】:

    我在 Laravel 5.4MySQL 中遇到了同样的问题。我原来的代码是:

    $attach = new Attachments ;
    $attach->site_id = ($type == 'site' ? $typeId : null) ;
    ...
    

    通过添加一个空主 ID,save() 的行为与预期一样。

    $attach = new Attachments ;
    $attach->id = null ;
    $attach->site_id = ($type == 'site' ? $typeId : null) ;
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 2015-06-09
      • 1970-01-01
      相关资源
      最近更新 更多