overall code refactoring.

This commit is contained in:
Taylor Otwell
2011-06-14 17:27:11 -05:00
parent af24e8db45
commit 30c83f265d
36 changed files with 720 additions and 559 deletions

View File

@@ -68,6 +68,46 @@ abstract class Eloquent {
*/
public $query;
/**
* Get the table name for a model.
*
* @param string $class
* @return string
*/
public static function table($class)
{
// -----------------------------------------------------
// Check for a table name override.
// -----------------------------------------------------
if (property_exists($class, 'table'))
{
return $class::$table;
}
return \System\Str::lower(\System\Inflector::plural($class));
}
/**
* Factory for creating new Eloquent model instances.
*
* @param string $class
* @return object
*/
public static function make($class)
{
// -----------------------------------------------------
// Instantiate the Eloquent model.
// -----------------------------------------------------
$model = new $class;
// -----------------------------------------------------
// Set the fluent query builder on the model.
// -----------------------------------------------------
$model->query = Query::table(static::table($class));
return $model;
}
/**
* Create a new model instance and set the relationships
* that should be eagerly loaded.
@@ -76,7 +116,14 @@ abstract class Eloquent {
*/
public static function with()
{
$model = Eloquent\Factory::make(get_called_class());
// -----------------------------------------------------
// Create a new model instance.
// -----------------------------------------------------
$model = static::make(get_called_class());
// -----------------------------------------------------
// Set the relationships that should be eager loaded.
// -----------------------------------------------------
$model->includes = func_get_args();
return $model;
@@ -90,7 +137,7 @@ abstract class Eloquent {
*/
public static function find($id)
{
return Eloquent\Factory::make(get_called_class())->where('id', '=', $id)->first();
return static::make(get_called_class())->where('id', '=', $id)->first();
}
/**
@@ -100,7 +147,7 @@ abstract class Eloquent {
*/
private function _get()
{
return Eloquent\Hydrate::from($this);
return Eloquent\Hydrator::hydrate($this);
}
/**
@@ -110,14 +157,7 @@ abstract class Eloquent {
*/
private function _first()
{
$results = Eloquent\Hydrate::from($this->take(1));
if (count($results) > 0)
{
reset($results);
return current($results);
}
return (count($results = Eloquent\Hydrator::hydrate($this->take(1))) > 0) ? reset($results) : null;
}
/**
@@ -129,7 +169,8 @@ abstract class Eloquent {
*/
public function has_one($model, $foreign_key = null)
{
return Eloquent\Relate::has_one($model, $foreign_key, $this);
$this->relating = __FUNCTION__;
return $this->has_one_or_many($model, $foreign_key);
}
/**
@@ -141,23 +182,65 @@ abstract class Eloquent {
*/
public function has_many($model, $foreign_key = null)
{
return Eloquent\Relate::has_many($model, $foreign_key, $this);
$this->relating = __FUNCTION__;
return $this->has_one_or_many($model, $foreign_key);
}
/**
* Retrieve the query for a 1:1 or 1:* relationship.
*
* @param string $model
* @param string $foreign_key
* @return mixed
*/
private function has_one_or_many($model, $foreign_key)
{
// -----------------------------------------------------
// Determine the foreign key for the relationship.
//
// The foreign key is typically the name of the related
// model with an appeneded _id.
// -----------------------------------------------------
$this->relating_key = (is_null($foreign_key)) ? \System\Str::lower(get_class($this)).'_id' : $foreign_key;
return static::make($model)->where($this->relating_key, '=', $this->id);
}
/**
* Retrieve the query for a 1:1 belonging relationship.
*
* @param string $model
* @param string $foreign_key
* @return mixed
*/
public function belongs_to($model)
public function belongs_to($model, $foreign_key = null)
{
// -----------------------------------------------------
// Get the calling function name.
// -----------------------------------------------------
list(, $caller) = debug_backtrace(false);
$this->relating = __FUNCTION__;
return Eloquent\Relate::belongs_to($caller, $model, $this);
// -----------------------------------------------------
// Determine the foreign key of the relationship.
// -----------------------------------------------------
if ( ! is_null($foreign_key))
{
$this->relating_key = $foreign_key;
}
else
{
// -----------------------------------------------------
// Get the calling function name.
// -----------------------------------------------------
list(, $caller) = debug_backtrace(false);
// -----------------------------------------------------
// Determine the foreign key for the relationship.
//
// The foreign key for belonging relationships is the
// name of the relationship method with an appended _id.
// -----------------------------------------------------
$this->relating_key = $caller['function'].'_id';
}
return static::make($model)->where('id', '=', $this->attributes[$this->relating_key]);
}
/**
@@ -167,9 +250,39 @@ abstract class Eloquent {
* @param string $table
* @return mixed
*/
public function has_many_and_belongs_to($model, $table = null)
public function has_and_belongs_to_many($model, $table = null)
{
return Eloquent\Relate::has_many_and_belongs_to($model, $table, $this);
$this->relating = __FUNCTION__;
// -----------------------------------------------------
// Determine the intermediate table name.
// -----------------------------------------------------
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(\System\Inflector::plural($model), \System\Inflector::plural(get_class($this)));
sort($models);
$this->relating_table = \System\Str::lower($models[0].'_'.$models[1]);
}
// -----------------------------------------------------
// Determine the foreign key for the relationship.
// -----------------------------------------------------
$this->relating_key = $this->relating_table.'.'.\System\Str::lower(get_class($this)).'_id';
return static::make($model)
->select(static::table($model).'.*')
->join($this->relating_table, static::table($model).'.id', '=', $this->relating_table.'.'.\System\Str::lower($model).'_id')
->where($this->relating_key, '=', $this->id);
}
/**
@@ -188,7 +301,43 @@ abstract class Eloquent {
return true;
}
$result = Eloquent\Warehouse::put($this);
// -----------------------------------------------------
// Get the class name of the Eloquent model.
// -----------------------------------------------------
$model = get_class($this);
// -----------------------------------------------------
// Get a fresh query instance for the model.
// -----------------------------------------------------
$this->query = Query::table(static::table($model));
// -----------------------------------------------------
// Set the creation and update timestamps.
// -----------------------------------------------------
if (property_exists($model, 'timestamps') and $model::$timestamps)
{
$this->updated_at = date('Y-m-d H:i:s');
if ( ! $this->exists)
{
$this->created_at = $this->updated_at;
}
}
// -----------------------------------------------------
// If the model already exists in the database, we only
// need to update it. Otherwise, we'll insert it.
// -----------------------------------------------------
if ($this->exists)
{
$result = $this->query->where('id', '=', $this->attributes['id'])->update($this->dirty) == 1;
}
else
{
$this->attributes['id'] = $this->query->insert_get_id($this->attributes);
$result = $this->exists = is_numeric($this->id);
}
// -----------------------------------------------------
// The dirty attributes can be cleared after each save.
@@ -209,7 +358,7 @@ abstract class Eloquent {
// -----------------------------------------------------
if ($this->exists)
{
return Eloquent\Warehouse::forget($this);
return Query::table(static::table(get_class($this)))->delete($this->id) == 1;
}
return $this->query->delete($id);
@@ -252,12 +401,15 @@ abstract class Eloquent {
public function __set($key, $value)
{
// -----------------------------------------------------
// Is the key actually a relationship?
// If the key is a relationship, add it to the ignored.
// -----------------------------------------------------
if (method_exists($this, $key))
{
$this->ignore[$key] = $value;
}
// -----------------------------------------------------
// Set the attribute and add it to the dirty array.
// -----------------------------------------------------
else
{
$this->attributes[$key] = $value;
@@ -288,11 +440,17 @@ abstract class Eloquent {
*/
public function __call($method, $parameters)
{
// -----------------------------------------------------
// Retrieve an array of models.
// -----------------------------------------------------
if ($method == 'get')
{
return $this->_get();
}
// -----------------------------------------------------
// Retrieve the first model result.
// -----------------------------------------------------
if ($method == 'first')
{
return $this->_first();
@@ -320,13 +478,19 @@ abstract class Eloquent {
*/
public static function __callStatic($method, $parameters)
{
$model = Eloquent\Factory::make(get_called_class());
$model = static::make(get_called_class());
// -----------------------------------------------------
// Retrieve the entire table of models.
// -----------------------------------------------------
if ($method == 'get')
{
return $model->_get();
}
// -----------------------------------------------------
// Retrieve the first model result.
// -----------------------------------------------------
if ($method == 'first')
{
return $model->_first();

View File

@@ -1,23 +0,0 @@
<?php namespace System\DB\Eloquent;
class Factory {
/**
* Factory for creating new model instances.
*
* @param string $class
* @return object
*/
public static function make($class)
{
$model = new $class;
// -----------------------------------------------------
// Set the fluent query builder on the model.
// -----------------------------------------------------
$model->query = \System\DB\Query::table(Meta::table($class));
return $model;
}
}

View File

@@ -1,6 +1,6 @@
<?php namespace System\DB\Eloquent;
class Hydrate {
class Hydrator {
/**
* Load the array of hydrated models.
@@ -8,7 +8,7 @@ class Hydrate {
* @param object $eloquent
* @return array
*/
public static function from($eloquent)
public static function hydrate($eloquent)
{
// -----------------------------------------------------
// Load the base models.
@@ -38,27 +38,30 @@ class Hydrate {
* Hydrate the base models for a query.
*
* @param string $class
* @param array $models
* @param array $results
* @return array
*/
private static function base($class, $models)
private static function base($class, $results)
{
$results = array();
$models = array();
foreach ($models as $model)
foreach ($results as $result)
{
$result = new $class;
$model = new $class;
$result->attributes = (array) $model;
$result->exists = true;
// -----------------------------------------------------
// Set the attributes and existence flag on the model.
// -----------------------------------------------------
$model->attributes = (array) $result;
$model->exists = true;
// -----------------------------------------------------
// The results are keyed by the ID on the record.
// -----------------------------------------------------
$results[$result->id] = $result;
$models[$model->id] = $model;
}
return $results;
return $models;
}
/**
@@ -97,6 +100,9 @@ class Hydrate {
$result->ignore[$include] = (strpos($eloquent->relating, 'has_many') === 0) ? array() : null;
}
// -----------------------------------------------------
// Eagerly load the relationship.
// -----------------------------------------------------
if ($eloquent->relating == 'has_one' or $eloquent->relating == 'has_many')
{
static::eagerly_load_one_or_many($eloquent->relating_key, $eloquent->relating, $include, $model, $results);
@@ -203,7 +209,9 @@ class Hydrate {
// We also add the foreign key to the select which will allow us
// to match the models back to their parents.
// -----------------------------------------------------
$inclusions = $model->query->where_in($relating_key, array_keys($results))->get(Meta::table(get_class($model)).'.*', $relating_table.'.'.$foreign_key);
$inclusions = $model->query
->where_in($relating_key, array_keys($results))
->get(\System\DB\Eloquent::table(get_class($model)).'.*', $relating_table.'.'.$foreign_key);
// -----------------------------------------------------
// Get the class name of the related model.
@@ -217,6 +225,9 @@ class Hydrate {
{
$related = new $class;
// -----------------------------------------------------
// Set the attributes and existence flag on the model.
// -----------------------------------------------------
$related->exists = true;
$related->attributes = (array) $inclusion;

View File

@@ -1,24 +0,0 @@
<?php namespace System\DB\Eloquent;
class Meta {
/**
* Get the table name for a model.
*
* @param string $class
* @return string
*/
public static function table($class)
{
// -----------------------------------------------------
// Check for a table name override.
// -----------------------------------------------------
if (property_exists($class, 'table'))
{
return $class::$table;
}
return \System\Str::lower(\System\Inflector::plural($class));
}
}

View File

@@ -1,97 +0,0 @@
<?php namespace System\DB\Eloquent;
class Relate {
/**
* Retrieve the query for a 1:1 relationship.
*
* @param string $model
* @param string $foreign_key
* @param object $eloquent
* @return mixed
*/
public static function has_one($model, $foreign_key, $eloquent)
{
$eloquent->relating = __FUNCTION__;
return static::has_one_or_many($model, $foreign_key, $eloquent);
}
/**
* Retrieve the query for a 1:* relationship.
*
* @param string $model
* @param string $foreign_key
* @param object $eloquent
* @return mixed
*/
public static function has_many($model, $foreign_key, $eloquent)
{
$eloquent->relating = __FUNCTION__;
return static::has_one_or_many($model, $foreign_key, $eloquent);
}
/**
* Retrieve the query for a 1:1 or 1:* relationship.
*
* @param string $model
* @param string $foreign_key
* @param object $eloquent
* @return mixed
*/
private static function has_one_or_many($model, $foreign_key, $eloquent)
{
$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);
}
/**
* Retrieve the query for a 1:1 belonging relationship.
*
* @param array $caller
* @param string $model
* @param object $eloquent
* @return mixed
*/
public static function belongs_to($caller, $model, $eloquent)
{
$eloquent->relating = __FUNCTION__;
$eloquent->relating_key = $caller['function'].'_id';
return Factory::make($model)->where('id', '=', $eloquent->attributes[$eloquent->relating_key]);
}
/**
* Retrieve the query for a *:* relationship.
*
* @param string $model
* @param string $table
* @param object $eloquent
* @return mixed
*/
public static function has_many_and_belongs_to($model, $table, $eloquent)
{
// -----------------------------------------------------
// Figure out the intermediate table name.
// -----------------------------------------------------
if (is_null($table))
{
$models = array(\System\Str::lower($model), \System\Str::lower(get_class($eloquent)));
sort($models);
$eloquent->relating_table = implode('_', $models);
}
else
{
$eloquent->relating_table = $table;
}
$eloquent->relating = __FUNCTION__;
$eloquent->relating_key = $eloquent->relating_table.'.'.\System\Str::lower(get_class($eloquent)).'_id';
return Factory::make($model)
->select(Meta::table($model).'.*')
->join($eloquent->relating_table, Meta::table($model).'.id', '=', $eloquent->relating_table.'.'.\System\Str::lower($model).'_id')
->where($eloquent->relating_key, '=', $eloquent->id);
}
}

View File

@@ -1,69 +0,0 @@
<?php namespace System\DB\Eloquent;
class Warehouse {
/**
* Save an Eloquent model to the database.
*
* @param object $eloquent
* @return bool
*/
public static function put($eloquent)
{
$model = get_class($eloquent);
// -----------------------------------------------------
// Get a fresh query instance for the model.
// -----------------------------------------------------
$eloquent->query = \System\DB\Query::table(Meta::table($model));
// -----------------------------------------------------
// Set the creation and update timestamps.
// -----------------------------------------------------
if (property_exists($model, 'timestamps') and $model::$timestamps)
{
static::timestamp($eloquent);
}
if ($eloquent->exists)
{
return ($eloquent->query->where('id', '=', $eloquent->attributes['id'])->update($eloquent->dirty) == 1) ? true : false;
}
else
{
$eloquent->attributes['id'] = $eloquent->query->insert_get_id($eloquent->attributes);
}
$eloquent->exists = true;
return true;
}
/**
* Delete an Eloquent model from the database.
*
* @param object $eloquent
* @return bool
*/
public static function forget($eloquent)
{
return \System\DB::table(Meta::table(get_class($eloquent)))->where('id', '=', $eloquent->id)->delete() == 1;
}
/**
* Set the activity timestamps on a model.
*
* @param object $eloquent
* @return void
*/
private static function timestamp($eloquent)
{
$eloquent->updated_at = date('Y-m-d H:i:s');
if ( ! $eloquent->exists)
{
$eloquent->created_at = $eloquent->updated_at;
}
}
}

View File

@@ -10,18 +10,30 @@ class Compiler {
*/
public static function select($query)
{
// ----------------------------------------------------
// Add the SELECT, FROM, and WHERE clause to the query.
// ----------------------------------------------------
$sql = $query->select.' '.$query->from.' '.$query->where;
// ----------------------------------------------------
// Add the ORDER BY clause to the query.
// ----------------------------------------------------
if (count($query->orderings) > 0)
{
$sql .= ' ORDER BY '.implode(', ', $query->orderings);
}
// ----------------------------------------------------
// Add the LIMIT clause to the query.
// ----------------------------------------------------
if ( ! is_null($query->limit))
{
$sql .= ' LIMIT '.$query->limit;
}
// ----------------------------------------------------
// Add the OFFSET clause to the query.
// ----------------------------------------------------
if ( ! is_null($query->offset))
{
$sql .= ' OFFSET '.$query->offset;
@@ -39,11 +51,14 @@ class Compiler {
*/
public static function insert($query, $values)
{
// -----------------------------------------------------
// Begin the INSERT statement.
// -----------------------------------------------------
$sql = 'INSERT INTO '.$query->table.' (';
// ---------------------------------------------------
// ----------------------------------------------------
// Wrap each column name in keyword identifiers.
// ---------------------------------------------------
// ----------------------------------------------------
$columns = array();
foreach (array_keys($values) as $column)
@@ -51,6 +66,9 @@ class Compiler {
$columns[] = $query->wrap($column);
}
// -----------------------------------------------------
// Concatenante the columns and values to the statement.
// -----------------------------------------------------
return $sql .= implode(', ', $columns).') VALUES ('.$query->parameterize($values).')';
}
@@ -63,10 +81,13 @@ class Compiler {
*/
public static function update($query, $values)
{
// ----------------------------------------------------
// Initialize the UPDATE statement.
// ----------------------------------------------------
$sql = 'UPDATE '.$query->table.' SET ';
// ---------------------------------------------------
// Add each column set the query.
// Add each column set the statement.
// ---------------------------------------------------
$columns = array();
@@ -75,6 +96,9 @@ class Compiler {
$columns[] = $query->wrap($column).' = ?';
}
// ---------------------------------------------------
// Concatenate the SETs to the statement.
// ---------------------------------------------------
return $sql .= implode(', ', $columns).' '.$query->where;
}