Refactoring DB\Eloquent.
This commit is contained in:
@@ -116,7 +116,7 @@ abstract class Eloquent {
|
|||||||
{
|
{
|
||||||
$model = new $class;
|
$model = new $class;
|
||||||
|
|
||||||
// Since this method is only used for instantiating/ models for querying
|
// Since this method is only used for instantiating models for querying
|
||||||
// purposes, we will go ahead and set the Query instance on the model.
|
// purposes, we will go ahead and set the Query instance on the model.
|
||||||
$model->query = Query::table(static::table($class));
|
$model->query = Query::table(static::table($class));
|
||||||
|
|
||||||
@@ -132,6 +132,7 @@ abstract class Eloquent {
|
|||||||
public static function with()
|
public static function with()
|
||||||
{
|
{
|
||||||
$model = static::make(get_called_class());
|
$model = static::make(get_called_class());
|
||||||
|
|
||||||
$model->includes = func_get_args();
|
$model->includes = func_get_args();
|
||||||
|
|
||||||
return $model;
|
return $model;
|
||||||
@@ -197,15 +198,16 @@ abstract class Eloquent {
|
|||||||
/**
|
/**
|
||||||
* Retrieve the query for a 1:1 or 1:* relationship.
|
* Retrieve the query for a 1:1 or 1:* relationship.
|
||||||
*
|
*
|
||||||
|
* The default foreign key for has one and has many relationships is the name
|
||||||
|
* of the model with an appended _id. For example, the foreign key for a
|
||||||
|
* User model would be user_id. Photo would be photo_id, etc.
|
||||||
|
*
|
||||||
* @param string $model
|
* @param string $model
|
||||||
* @param string $foreign_key
|
* @param string $foreign_key
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private function has_one_or_many($model, $foreign_key)
|
private function has_one_or_many($model, $foreign_key)
|
||||||
{
|
{
|
||||||
// The default foreign key for has one and has many relationships is the name
|
|
||||||
// of the model with an appended _id. For example, the foreign key for a
|
|
||||||
// User model would be user_id. Photo would be photo_id, etc.
|
|
||||||
$this->relating_key = (is_null($foreign_key)) ? strtolower(get_class($this)).'_id' : $foreign_key;
|
$this->relating_key = (is_null($foreign_key)) ? strtolower(get_class($this)).'_id' : $foreign_key;
|
||||||
|
|
||||||
return static::make($model)->where($this->relating_key, '=', $this->id);
|
return static::make($model)->where($this->relating_key, '=', $this->id);
|
||||||
@@ -214,6 +216,10 @@ abstract class Eloquent {
|
|||||||
/**
|
/**
|
||||||
* Retrieve the query for a 1:1 belonging relationship.
|
* Retrieve the query for a 1:1 belonging relationship.
|
||||||
*
|
*
|
||||||
|
* The default foreign key for belonging relationships is the name of the
|
||||||
|
* relationship method name with _id. So, if a model has a "manager" method
|
||||||
|
* returning a belongs_to relationship, the key would be manager_id.
|
||||||
|
*
|
||||||
* @param string $model
|
* @param string $model
|
||||||
* @param string $foreign_key
|
* @param string $foreign_key
|
||||||
* @return mixed
|
* @return mixed
|
||||||
@@ -228,9 +234,6 @@ abstract class Eloquent {
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// The default foreign key for belonging relationships is the name of the
|
|
||||||
// relationship method name with _id. So, if a model has a "manager" method
|
|
||||||
// returning a belongs_to relationship, the key would be manager_id.
|
|
||||||
list(, $caller) = debug_backtrace(false);
|
list(, $caller) = debug_backtrace(false);
|
||||||
|
|
||||||
$this->relating_key = $caller['function'].'_id';
|
$this->relating_key = $caller['function'].'_id';
|
||||||
@@ -242,6 +245,12 @@ abstract class Eloquent {
|
|||||||
/**
|
/**
|
||||||
* Retrieve the query for a *:* relationship.
|
* Retrieve the query for a *:* relationship.
|
||||||
*
|
*
|
||||||
|
* By default, the intermediate table name is the plural names of the models
|
||||||
|
* arranged alphabetically and concatenated with an underscore.
|
||||||
|
*
|
||||||
|
* The default foreign key for many-to-many relations is the name of the model
|
||||||
|
* with an appended _id. This is the same convention as has_one and has_many.
|
||||||
|
*
|
||||||
* @param string $model
|
* @param string $model
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @return mixed
|
* @return mixed
|
||||||
@@ -250,23 +259,19 @@ abstract class Eloquent {
|
|||||||
{
|
{
|
||||||
$this->relating = __FUNCTION__;
|
$this->relating = __FUNCTION__;
|
||||||
|
|
||||||
if ( ! is_null($table))
|
if (is_null($table))
|
||||||
{
|
{
|
||||||
$this->relating_table = $table;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// By default, the intermediate table name is the plural names of the models
|
|
||||||
// arranged alphabetically and concatenated with an underscore.
|
|
||||||
$models = array(Inflector::plural($model), Inflector::plural(get_class($this)));
|
$models = array(Inflector::plural($model), Inflector::plural(get_class($this)));
|
||||||
|
|
||||||
sort($models);
|
sort($models);
|
||||||
|
|
||||||
$this->relating_table = strtolower($models[0].'_'.$models[1]);
|
$this->relating_table = strtolower($models[0].'_'.$models[1]);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$this->relating_table = $table;
|
||||||
|
}
|
||||||
|
|
||||||
// The default foreign key for many-to-many relations is the name of the model with an appended _id.
|
|
||||||
// This is the same convention as has_one and has_many.
|
|
||||||
$this->relating_key = strtolower(get_class($this)).'_id';
|
$this->relating_key = strtolower(get_class($this)).'_id';
|
||||||
|
|
||||||
return static::make($model)
|
return static::make($model)
|
||||||
|
|||||||
Reference in New Issue
Block a user