Php Laravel save()方法返回true但不更新记录
我正在使用Elount更新我的表Opportunity 机会模型Php Laravel save()方法返回true但不更新记录,php,laravel,eloquent,laravel-5,Php,Laravel,Eloquent,Laravel 5,我正在使用Elount更新我的表Opportunity 机会模型 <?php namespace App; use Illuminate\Database\Eloquent\Model; class Opportunity extends Model { protected $primaryKey = 'OpportunityID'; protected $table = 'opportunitys'; // relationships public function csfs()
<?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();
}
我得到了身份证,找到了机会
$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();
当我死了并把它扔掉时,它返回真值
但数据库中没有更改任何记录
有什么想法吗
tinker的两个图像
商机模型中的这一行修复了问题
Protected $primaryKey = "opportunityID";
尽管很难理解为什么仍然可以检索数据并创建新记录。我遇到了一个非常类似的问题,这篇文章让我找到了一个解决方案。我也在重写一个
primaryKey
环境:
- PHP7.1
- 流明5.3(明显有说服力)
- 甲骨文12(使用)
选择和插入操作使用受保护的$primaryKey='USER_ID'但是,尽管save()
返回了true
,但UPDATE
操作仍不起作用
找到这个帖子后,我换了个案子<代码>受保护的$primaryKey='user_id'代码>,糟糕,这三个操作都可以!我希望我能有一个更确切的解释,为什么这是有效的。当我创建表时,我清楚地使用了upper用户ID VARCHAR2(50字节)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();
这将成为现实。Save始终返回true,除非更新或保存未成功(因为找不到用户id或类似的原因)。如果某个属性已更改,则不会对其进行检查。要查看属性是否已更改,请参见
这里有一点类似于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()
为false,模型将不会更新并返回true。数组$request是什么?其中有任何值吗?如果它是Request的注入实例,请尝试通过调用input()来获取值,例如$Request->input('margin')。如何检查数据库中的记录没有更改?这很棘手。但我认为这个问题与您dd()save()有关。我怀疑dd()只是停止了php脚本的执行。不打断它,直接检查数据库怎么样?非常有趣。当您尝试更新记录时,可能有一些数据库事务正在进行中?另外,您是否可以尝试从php artisan tinker
更新记录?感觉它与数据库/表的编码有关。save()
方法使用Laravel的查询生成器中的where
创建具有对象属性的查询。因此,最终这并不是真正的Laravel,它只是用您设置的键进行查询。现在您的表编码可以改变情况了,utf8\u general\u ci
通常被推荐来避免这种问题。这就是解决我的问题!但是我不知道我的代码到底是怎么回事!!!无论如何谢谢你
// 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;