Eug*_*ang 14 php laravel eloquent laravel-4
当我想更新FaqTrans数据库时,但这个数据有两个主键(faq_ida和lang)$table->primary(array('lang', 'faq_id'));.所以我不需要一个idauto_increment列.
但是,当我使用以下代码更新数据库时,错误消息提示我没有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();
Run Code Online (Sandbox Code Playgroud)
错误信息
SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'id'(SQL:update
FAQtranssetbody=?,updated_at=?whereidis null)(Bindings:array(0 =>'adfadaaadfadaa',1 =>' 2013-07-04 11:12:42',))
当我添加一id列时,代码工作正常......
有没有办法可以在没有ID列的情况下更新数据库?
小智 5
Pretty simple:
FaqTrans::where(
[
'faq_id' => $faq_id,
'lang' => $lang
]
)->update(
[
'body' => $body,
'title' => $title
]
);
Run Code Online (Sandbox Code Playgroud)
对于任何在将来偶然发现这个问题的人(就像我一样),这里是Eloquent中复合主键的一个很好的解决方法.
这是模型类的顶部:
protected $primaryKey = array('key1', 'key2');
Run Code Online (Sandbox Code Playgroud)
然后使用以下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;
}
Run Code Online (Sandbox Code Playgroud)
原始资料来源:https://github.com/laravel/framework/issues/5517#issuecomment-52996610
更新2016-05-03
我最喜欢这样做的方法:
| 归档时间: |
|
| 查看次数: |
22216 次 |
| 最近记录: |