Files
leenooks/src/Traits/CompositeKeys.php

114 lines
2.1 KiB
PHP

<?php
namespace Leenooks\Traits;
use Illuminate\Database\Eloquent\Builder;
/**
* Trait CompositeKeys
* Enable Models to have multiple primary keys
*
* Need to add to the Model...
* public $incrementing = false;
* protected $primaryKey = [<ARRAY_OF_KEYS>];
*
* @package Leenooks\Traits
*/
trait CompositeKeys {
/**
* Set the keys for a save update query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function setKeysForSaveQuery($query)
{
$keys = $this->getKeyName();
return !is_array($keys)
? parent::setKeysForSaveQuery($query)
: $query->where(function($q) use($keys) {
foreach ($keys as $key){
$q->where($key,$this->getAttribute($key));
}
});
}
/**
* Get the casts array.
*
* @return array
*/
public function getCasts()
{
if ($this->getIncrementing())
return array_merge([$this->getKeyName() => $this->getKeyType()],$this->casts);
return $this->casts;
}
/**
* @return false
*/
public function getIncrementing()
{
return FALSE;
}
/**
* Get the value of the model's primary key.
*
* @return mixed
*/
public function getKey()
{
$fields = $this->getKeyName();
$keys = [];
array_map(function($key) use(&$keys) {
$keys[] = $this->getAttribute($key);
}, $fields);
return $keys;
}
/**
* Finds model by primary keys
*
* @param array $ids
* @return mixed
*/
public static function find(array $ids)
{
$modelClass = get_called_class();
$model = new $modelClass();
$keys = $model->primaryKey;
return $model->where(function($query) use($ids,$keys) {
foreach ($keys as $idx => $key) {
if (isset($ids[$idx]))
$query->where($key, $ids[$idx]);
else
$query->whereNull($key);
}
})->first();
}
/**
* Find model by primary key or throws ModelNotFoundException
*
* @param array $ids
* @return mixed
*/
public static function findOrFail(array $ids)
{
$modelClass = get_called_class();
$model = new $modelClass();
$record = $model->find($ids);
if (! $record)
throw new ModelNotFoundException;
return $record;
}
}