Laravel save() 方法返回 true 但不更新记录
Laravel save() method returning true but not updating records
我正在使用 Eloquent 更新我的 table 机会,
机会模型
<?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();
它returns当我死掉并丢弃它时是真的。
但数据库中没有任何记录发生变化。
有什么想法吗?
tinker 的两张图片
机会模型中的这一行解决了这个问题。
Protected $primaryKey = "opportunityID";
尽管很难理解为什么仍然可以检索数据并创建新记录。
我有一个非常相似的问题,这个 post 引导我找到了解决方案。我也正在覆盖 primaryKey
.
环境:
- PHP 7.1
- Lumen 5.3(显然 Eloquent 已启用)
- Oracle 12(使用 https://github.com/yajra/laravel-oci8)
SELECT
和 INSERT
操作可与 protected $primaryKey = 'USER_ID';
一起使用,但是,即使 save()
返回 true
,UPDATE
操作也不起作用。
找到这个后 post 我改变了大小写。 protected $primaryKey = 'user_id';
,糟糕,三个操作都有效!我希望我有一个更可靠的解释为什么这有效。当我创建 table 时,我显然使用了 upper USER_ID VARCHAR2(50 BYTE) NOT NULL
.
我对 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) ;
...
回答标题中的问题:
$user = \App\User::find(1);
$boolean = $user->save();
这将 return 为真。保存总是 returns true,除非更新或保存没有成功(因为 user_id 没有找到或类似的东西)。它不会检查属性是否已更改。要查看属性是否已更改,请参阅 Laravel check if updateOrCreate performed update
这里有点Laravel eloquent 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()
为假,模型将不会更新,而 return 为真。
我正在使用 Eloquent 更新我的 table 机会,
机会模型
<?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();
它returns当我死掉并丢弃它时是真的。
但数据库中没有任何记录发生变化。
有什么想法吗?
tinker 的两张图片
机会模型中的这一行解决了这个问题。
Protected $primaryKey = "opportunityID";
尽管很难理解为什么仍然可以检索数据并创建新记录。
我有一个非常相似的问题,这个 post 引导我找到了解决方案。我也正在覆盖 primaryKey
.
环境:
- PHP 7.1
- Lumen 5.3(显然 Eloquent 已启用)
- Oracle 12(使用 https://github.com/yajra/laravel-oci8)
SELECT
和 INSERT
操作可与 protected $primaryKey = 'USER_ID';
一起使用,但是,即使 save()
返回 true
,UPDATE
操作也不起作用。
找到这个后 post 我改变了大小写。 protected $primaryKey = 'user_id';
,糟糕,三个操作都有效!我希望我有一个更可靠的解释为什么这有效。当我创建 table 时,我显然使用了 upper USER_ID VARCHAR2(50 BYTE) NOT NULL
.
我对 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) ;
...
回答标题中的问题:
$user = \App\User::find(1);
$boolean = $user->save();
这将 return 为真。保存总是 returns true,除非更新或保存没有成功(因为 user_id 没有找到或类似的东西)。它不会检查属性是否已更改。要查看属性是否已更改,请参阅 Laravel check if updateOrCreate performed update
这里有点Laravel eloquent 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()
为假,模型将不会更新,而 return 为真。