added ability to specify foreign key for has_one and has_many relationships.
This commit is contained in:
parent
2bcf7ed327
commit
05b2e28770
|
@ -124,22 +124,24 @@ private function _first()
|
||||||
* Retrieve the query for a 1:1 relationship.
|
* Retrieve the query for a 1:1 relationship.
|
||||||
*
|
*
|
||||||
* @param string $model
|
* @param string $model
|
||||||
|
* @param string $foreign_key
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function has_one($model)
|
public function has_one($model, $foreign_key = null)
|
||||||
{
|
{
|
||||||
return Eloquent\Relate::has_one($model, $this);
|
return Eloquent\Relate::has_one($model, $foreign_key, $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the query for a 1:* relationship.
|
* Retrieve the query for a 1:* relationship.
|
||||||
*
|
*
|
||||||
* @param string $model
|
* @param string $model
|
||||||
|
* @param string $foreign_key
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function has_many($model)
|
public function has_many($model, $foreign_key = null)
|
||||||
{
|
{
|
||||||
return Eloquent\Relate::has_many($model, $this);
|
return Eloquent\Relate::has_many($model, $foreign_key, $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -6,38 +6,41 @@ class Relate {
|
||||||
* Retrieve the query for a 1:1 relationship.
|
* Retrieve the query for a 1:1 relationship.
|
||||||
*
|
*
|
||||||
* @param string $model
|
* @param string $model
|
||||||
|
* @param string $foreign_key
|
||||||
* @param object $eloquent
|
* @param object $eloquent
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function has_one($model, $eloquent)
|
public static function has_one($model, $foreign_key, $eloquent)
|
||||||
{
|
{
|
||||||
$eloquent->relating = __FUNCTION__;
|
$eloquent->relating = __FUNCTION__;
|
||||||
return static::has_one_or_many($model, $eloquent);
|
return static::has_one_or_many($model, $foreign_key, $eloquent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the query for a 1:* relationship.
|
* Retrieve the query for a 1:* relationship.
|
||||||
*
|
*
|
||||||
* @param string $model
|
* @param string $model
|
||||||
|
* @param string $foreign_key
|
||||||
* @param object $eloquent
|
* @param object $eloquent
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function has_many($model, $eloquent)
|
public static function has_many($model, $foreign_key, $eloquent)
|
||||||
{
|
{
|
||||||
$eloquent->relating = __FUNCTION__;
|
$eloquent->relating = __FUNCTION__;
|
||||||
return static::has_one_or_many($model, $eloquent);
|
return static::has_one_or_many($model, $foreign_key, $eloquent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the query for a 1:1 or 1:* relationship.
|
* Retrieve the query for a 1:1 or 1:* relationship.
|
||||||
*
|
*
|
||||||
* @param string $model
|
* @param string $model
|
||||||
|
* @param string $foreign_key
|
||||||
* @param object $eloquent
|
* @param object $eloquent
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private static function has_one_or_many($model, $eloquent)
|
private static function has_one_or_many($model, $foreign_key, $eloquent)
|
||||||
{
|
{
|
||||||
$eloquent->relating_key = \System\Str::lower(get_class($eloquent)).'_id';
|
$eloquent->relating_key = (is_null($foreign_key)) ? \System\Str::lower(get_class($eloquent)).'_id' : $foreign_key;
|
||||||
return Factory::make($model)->where($eloquent->relating_key, '=', $eloquent->id);
|
return Factory::make($model)->where($eloquent->relating_key, '=', $eloquent->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue