Ser*_*net 42 php model composite-primary-key web laravel
我在我的DataBase中有一个包含两个主键(id和language_id)的表,我需要把它放在我的模型中.Models中的默认primaryKey(Laravel 5中的Model.php)是id,我希望primaryKeys是id和id_language.我尝试用数组或字符串','但它不起作用.它告诉我数组无法在String中转换.
请帮忙.
谢谢!
mop*_*922 74
我编写了这个简单的PHP特性来使Eloquent能够处理复合键:
<?php
namespace App\Model\Traits; // *** Adjust this to match your model namespace! ***
use Illuminate\Database\Eloquent\Builder;
trait HasCompositePrimaryKey
{
/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing()
{
return false;
}
/**
* Set the keys for a save update query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function setKeysForSaveQuery(Builder $query)
{
foreach ($this->getKeyName() as $key) {
// UPDATE: Added isset() per devflow's comment.
if (isset($this->$key))
$query->where($key, '=', $this->$key);
else
throw new Exception(__METHOD__ . 'Missing part of the primary key: ' . $key);
}
return $query;
}
// UPDATE: From jessedp. See his edit, below.
/**
* Execute a query for a single record by ID.
*
* @param array $ids Array of keys, like [column => value].
* @param array $columns
* @return mixed|static
*/
public static function find($ids, $columns = ['*'])
{
$me = new self;
$query = $me->newQuery();
foreach ($me->getKeyName() as $key) {
$query->where($key, '=', $ids[$key]);
}
return $query->first($columns);
}
}
Run Code Online (Sandbox Code Playgroud)
将它放在Traits主模型目录下的目录中,然后您可以在任何复合键模型的顶部添加一个简单的单行:
class MyModel extends Eloquent {
use Traits\HasCompositePrimaryKey; // *** THIS!!! ***
/**
* The primary key of the table.
*
* @var string
*/
protected $primaryKey = array('key1', 'key2');
...
Run Code Online (Sandbox Code Playgroud)
protected static function find($id, $columns = ['*'])
{
$me = new self;
$query = $me->newQuery();
$i=0;
foreach ($me->getKeyName() as $key) {
$query->where($key, '=', $id[$i]);
$i++;
}
return $query->first($columns);
}
Run Code Online (Sandbox Code Playgroud)
我现在将其作为一个名为LaravelTreats的开源软件包的一部分进行维护.
luk*_*ter 32
你不能.Eloquent不支持复合主键.
sba*_*sba 16
它似乎发生了变化,因为这个至少与Laravel 5.1一起使用:
$table->primary(['key1', 'key2']);
Run Code Online (Sandbox Code Playgroud)
我只是运行迁移,我在数据库中看到的内容符合我在上面的代码中放置的内容(当然上面的名称字段仅用于演示目的).
更新:这对于迁移是正确的,但是只要您想通过eloquent插入它就不能使用复合键,并且永远不会(最后一个条目):
https://github.com/laravel/framework/issues/5517
在迁移中,您可以简单地为表定义复合主键,如@ erick-suarez和@sba所说,在您的Schema::create或Schema::table块写入$table->primary(['key1', 'key2']);
在表示该表的Eloquent模型中,您不能直接将该复合键与Eloquent方法一起使用,例如find($key),save($data)但您仍然可以检索模型实例以供查看使用
$modelObject = ModelName->where(['key1' => $key1, 'key2' => $key2])->first();
Run Code Online (Sandbox Code Playgroud)
如果要更新该表中的记录,可以使用以下QueryBuilder方法:
ModelName->where(['key1' => $key1, 'key2' => $key2])->update($data);
Run Code Online (Sandbox Code Playgroud)
$data您想要更新模型的数据关联数组在哪里['attribute1' => 'value1', ..].
注意:您仍然可以安全地使用Eloquent关系来检索此类模型,因为它们通常用作打破多对多关系结构的数据透视表.
| 归档时间: |
|
| 查看次数: |
53924 次 |
| 最近记录: |