Laravel save()方法返回true但不更新记录

时间:2015-03-12 10:19:50

标签: php laravel eloquent laravel-5

我正在使用Eloquent来更新我的桌子机会,

机会模式

<?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) {

我得到了身份并找到了机会。

$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。

但数据库中没有更改记录。

有什么想法吗?

来自修补匠的两张图片

Using Tinker to update

Using Tinker to update part 2

Var_dump after save

4 个答案:

答案 0 :(得分:12)

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

Protected $primaryKey = "opportunityID";

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

答案 1 :(得分:3)

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

环境:

SELECTINSERT操作与protected $primaryKey = 'USER_ID';一起使用,但UPDATE操作即使save()返回true也无效。

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

答案 2 :(得分:1)

回答标题中的问题:

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

这将返回 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() 为 false,模型将不会更新并返回 true。

答案 3 :(得分:0)

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

$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) ;
...