我写了这个简单的PHP trait 来调整 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);
}
}
将它放在你的主模型目录下的Traits 目录中,然后你可以在任何复合键模型的顶部添加一个简单的单行:
class MyModel extends Eloquent {
use Traits\HasCompositePrimaryKey; // *** THIS!!! ***
/**
* The primary key of the table.
*
* @var string
*/
protected $primaryKey = array('key1', 'key2');
...
由 jessedp 添加:
这对我来说非常有效,直到我想使用 Model::find ... 所以以下是一些可以添加到上面的 hasCompositePrimaryKey 特征的代码(可能会更好):
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);
}
2016 年 11 月 17 日更新
我现在将其作为名为@987654322@ 的开源包的一部分进行维护。
2020 年 6 月 10 日更新
LaravelTreats 已经死了,但还是享受代码吧 :)
多年来,一些深入的用例引起了我的注意,这些用例出现了问题。这应该适用于绝大多数用例,但要知道,如果你想变得花哨,你可能需要重新考虑你的方法。