[英]How can I update data with Eloquent without id in Laravel 4
當我想更新FaqTrans
數據庫時,此數據集有兩個主鍵(faq_ida和lang) $table->primary(array('lang', 'faq_id'));
table- $table->primary(array('lang', 'faq_id'));
。 所以我不需要帶有auto_increment的id
列。
但是,當我使用以下代碼更新數據庫時,錯誤消息提示我沒有id
列。
$faq_trans = FaqTrans::where('faq_id','=',$faq_id)->where('lang','=',$lang)->first();
$faq_trans->lang = Input::get('lang');
$faq_trans->body = Input::get('body');
$faq_trans->title = Input::get('title');
$faq_trans->save();
錯誤信息
SQLSTATE [42S22]:找不到列:
FAQtrans
子句'中的未知列'id'(SQL:updateFAQtrans
setbody
=?,updated_at
=?,其中id
為null)(綁定:數組(0 =>'adfadaaadfadaa', 1 =>'2013-07-04 11:12:42',))
當我添加一個id
列時,代碼可以正常工作...
有什么方法可以更新沒有ID列的數據庫?
將此行寫入模型
public $primaryKey = '_id';
_id是您的手動主鍵
Laravel / Eloquent不支持復合主鍵。
當您檢查Eloquent Model類時,您會看到主鍵只能是一個字符串,用於創建WHERE子句。
很簡單:
FaqTrans::where(
[
'faq_id' => $faq_id,
'lang' => $lang
]
)->update(
[
'body' => $body,
'title' => $title
]
);
對於將來遇到這個問題的人(就像我一樣),這是Eloquent中復合主鍵的一個不錯的解決方法。
這是模型類的頂部:
protected $primaryKey = array('key1', 'key2');
然后,您可以使用以下方法在模型上覆蓋save()
方法:
public function save(array $options = array())
{
if(!is_array($this->getKeyName()))
return parent::save($options);
// Fire Event for others to hook
if($this->fireModelEvent('saving') === false)
return false;
// Prepare query for inserting or updating
$query = $this->newQueryWithoutScopes();
// Perform Update
if ($this->exists) {
if (count($this->getDirty()) > 0) {
// Fire Event for others to hook
if ($this->fireModelEvent('updating') === false)
return false;
// Touch the timestamps
if ($this->timestamps)
$this->updateTimestamps();
//
// START FIX
//
// Convert primary key into an array if it's a single value
$primary = (count($this->getKeyName()) > 1) ? $this->getKeyName() : [$this->getKeyName()];
// Fetch the primary key(s) values before any changes
$unique = array_intersect_key($this->original, array_flip($primary));
// Fetch the primary key(s) values after any changes
$unique = !empty($unique) ? $unique : array_intersect_key($this->getAttributes(), array_flip($primary));
// Fetch the element of the array if the array contains only a single element
$unique = (count($unique) <> 1) ? $unique : reset($unique);
// Apply SQL logic
$query->where($unique);
//
// END FIX
//
// Update the records
$query->update($this->getDirty());
// Fire an event for hooking into
$this->fireModelEvent('updated', false);
}
// Perform Insert
} else {
// Fire an event for hooking into
if ($this->fireModelEvent('creating') === false)
return false;
// Touch the timestamps
if ($this->timestamps)
$this->updateTimestamps();
// Retrieve the attributes
$attributes = $this->attributes;
if ($this->incrementing && !is_array($this->getKeyName()))
$this->insertAndSetId($query, $attributes);
else
$query->insert($attributes);
// Set exists to true in case someone tries to update it during an event
$this->exists = true;
// Fire an event for hooking into
$this->fireModelEvent('created', false);
}
// Fires an event
$this->fireModelEvent('saved', false);
// Sync
$this->original = $this->attributes;
// Touches all relations
if (array_get($options, 'touch', true))
$this->touchOwners();
return true;
}
原始來源: https : //github.com/laravel/framework/issues/5517#issuecomment-52996610
更新2016-05-03
我最喜歡的新方法:
轉到您的FaqTrans
模型並添加它。
public $key = 'faq_id';
假設faq_id
比lang
更重要,就應該這樣做
如果有人有同樣的問題。 我用Nishchit Dhanani答案,它工作得很好這是: 寫這行到你的模型
public $ primaryKey ='_id';
_id是您的手動主鍵
就我而言。 我有一個用戶表,部分用戶是學生。 因此,我的學生表中的主鍵是“ user_id”。
在我的學生模型中添加以下行:public $ primaryKey ='user_id';
更新學生對象時,我可以使用save()方法。
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.