2015-03-12 22 views
9

Tôi đang sử dụng hùng hồn cho cập nhật Cơ hội bàn của tôi,Laravel phương pháp tiết kiệm() trở thành sự thật nhưng không cập nhật hồ sơ

Opportunity Mẫu

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

tôi nhận được id và tìm cơ hội.

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

tôi đã chết và đổ này và tôi có được điều này

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 
} 

Đó là chính xác. sau đó tôi cập nhật những với

$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']; 

Bây giờ nếu tôi chết và đổ tôi nhận được

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 
} 

tôi chỉ thay đổi giá trị trong đó cho thấy sự thay đổi.

bây giờ tôi chạy

$something->save(); 

Nó trả về true khi tôi chết và đổ nó.

Nhưng không có bản ghi nào được thay đổi trong cơ sở dữ liệu.

Bất kỳ ý tưởng nào?

hai hình ảnh từ tinker

Using Tinker to update

Using Tinker to update part 2

Var_dump after save

+1

Yêu cầu $ array là gì? Bất kỳ giá trị nào trong đó? – Carter

+0

Trong laravel-5 bạn sử dụng lớp Request trên tất cả các bài viết, nó tương tự như sử dụng Input cũ trong laravel 4.2 –

+1

Nếu nó là instance được yêu cầu, hãy thử lấy giá trị bằng cách gọi input() thay vào đó, như $ request-> đầu vào ('margin'). – Carter

Trả lời

12

dòng trong mô hình Opportunity cố định vấn đề này.

Protected $primaryKey = "opportunityID"; 

Mặc dù rất khó hiểu tại sao vẫn có thể truy xuất dữ liệu và tạo bản ghi mới.

+0

Có vẻ như nó có liên quan đến việc mã hóa cơ sở dữ liệu/bảng của bạn. Phương thức '' 'save()' '' sử dụng '' 'where''' từ trình tạo truy vấn của Laravel để tạo truy vấn với các thuộc tính của đối tượng của bạn. Vì vậy, đây không phải là thực sự Laravel cuối cùng, nó chỉ làm cho một truy vấn với các phím bạn đã thiết lập. Bây giờ mã hóa bảng của bạn có thể thay đổi mọi thứ, '' 'utf8_general_ci''' thường được đề xuất để tránh loại vấn đề này. –

2

Tôi gặp sự cố rất giống và bài đăng này đưa tôi đến giải pháp. Tôi cũng đã ghi đè một số primaryKey.

Môi trường:

SELECTINSERT hoạt động làm việc với protected $primaryKey = 'USER_ID';, tuy nhiên, UPDATE hoạt động không hoạt động ngay cả khi save() trả lại true.

Sau khi tìm bài đăng này, tôi đã thay đổi trường hợp. protected $primaryKey = 'user_id';, Whammy, cả ba hoạt động đều hoạt động! Tôi ước tôi có một lời giải thích chắc chắn hơn về lý do tại sao nó hoạt động. Khi tôi tạo bảng tôi đã sử dụng rõ ràng trên USER_ID VARCHAR2(50 BYTE) NOT NULL.

Các vấn đề liên quan