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

@@ -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;
}
}
}