trimmed comment bloat. returning boolean on eloquent save.
This commit is contained in:
@@ -10,13 +10,10 @@ class Factory {
|
||||
*/
|
||||
public static function make($class)
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Create a new model instance.
|
||||
// -----------------------------------------------------
|
||||
$model = new $class;
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Set the active query instance on the model.
|
||||
// Set the fluent query builder on the model.
|
||||
// -----------------------------------------------------
|
||||
$model->query = \System\DB\Query::table(Meta::table($class));
|
||||
|
||||
|
||||
@@ -22,17 +22,11 @@ class Hydrate {
|
||||
{
|
||||
foreach ($eloquent->includes as $include)
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Verify the relationship is defined.
|
||||
// -----------------------------------------------------
|
||||
if ( ! method_exists($eloquent, $include))
|
||||
{
|
||||
throw new \Exception("Attempting to eager load [$include], but the relationship is not defined.");
|
||||
}
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Eagerly load the relationship.
|
||||
// -----------------------------------------------------
|
||||
static::eagerly($eloquent, $include, $results);
|
||||
}
|
||||
}
|
||||
@@ -49,34 +43,17 @@ class Hydrate {
|
||||
*/
|
||||
private static function base($class, $models)
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Initialize the hydrated model array.
|
||||
// -----------------------------------------------------
|
||||
$results = array();
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Hydrate the models from the results.
|
||||
// -----------------------------------------------------
|
||||
foreach ($models as $model)
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Instantiate a new model instance.
|
||||
// -----------------------------------------------------
|
||||
$result = new $class;
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Set the model's attributes.
|
||||
// -----------------------------------------------------
|
||||
$result->attributes = (array) $model;
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Indicate that the model already exists.
|
||||
// -----------------------------------------------------
|
||||
$result->exists = true;
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Add the hydrated model to the array of models.
|
||||
// The array is keyed by the primary keys of the models.
|
||||
// The results are keyed by the ID on the record.
|
||||
// -----------------------------------------------------
|
||||
$results[$result->id] = $result;
|
||||
}
|
||||
@@ -107,13 +84,9 @@ class Hydrate {
|
||||
unset($eloquent->attributes[$spoof]);
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Reset the WHERE clause on the query.
|
||||
// Reset the WHERE clause and bindings on the query.
|
||||
// -----------------------------------------------------
|
||||
$model->query->where = 'WHERE 1 = 1';
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Reset the bindings on the query.
|
||||
// -----------------------------------------------------
|
||||
$model->query->bindings = array();
|
||||
|
||||
// -----------------------------------------------------
|
||||
@@ -124,23 +97,14 @@ class Hydrate {
|
||||
$result->ignore[$include] = (strpos($eloquent->relating, 'has_many') === 0) ? array() : null;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Eagerly load a 1:1 or 1:* 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);
|
||||
}
|
||||
// -----------------------------------------------------
|
||||
// Eagerly load a 1:1 (belonging) relationship.
|
||||
// -----------------------------------------------------
|
||||
elseif ($eloquent->relating == 'belongs_to')
|
||||
{
|
||||
static::eagerly_load_belonging($eloquent->relating_key, $include, $model, $results);
|
||||
}
|
||||
// -----------------------------------------------------
|
||||
// Eagerly load a *:* relationship.
|
||||
// -----------------------------------------------------
|
||||
else
|
||||
{
|
||||
static::eagerly_load_many_to_many($eloquent->relating_key, $eloquent->relating_table, strtolower(get_class($eloquent)).'_id', $include, $model, $results);
|
||||
|
||||
@@ -11,14 +11,7 @@ class Relate {
|
||||
*/
|
||||
public static function has_one($model, $eloquent)
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Set the relating type.
|
||||
// -----------------------------------------------------
|
||||
$eloquent->relating = __FUNCTION__;
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Return the Eloquent model.
|
||||
// -----------------------------------------------------
|
||||
return static::has_one_or_many($model, $eloquent);
|
||||
}
|
||||
|
||||
@@ -31,14 +24,7 @@ class Relate {
|
||||
*/
|
||||
public static function has_many($model, $eloquent)
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Set the relating type.
|
||||
// -----------------------------------------------------
|
||||
$eloquent->relating = __FUNCTION__;
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Return the Eloquent model.
|
||||
// -----------------------------------------------------
|
||||
return static::has_one_or_many($model, $eloquent);
|
||||
}
|
||||
|
||||
@@ -51,11 +37,7 @@ class Relate {
|
||||
*/
|
||||
private static function has_one_or_many($model, $eloquent)
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Set the relating key.
|
||||
// -----------------------------------------------------
|
||||
$eloquent->relating_key = \System\Str::lower(get_class($eloquent)).'_id';
|
||||
|
||||
return Factory::make($model)->where($eloquent->relating_key, '=', $eloquent->id);
|
||||
}
|
||||
|
||||
@@ -69,19 +51,9 @@ class Relate {
|
||||
*/
|
||||
public static function belongs_to($caller, $model, $eloquent)
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Set the relating type.
|
||||
// -----------------------------------------------------
|
||||
$eloquent->relating = __FUNCTION__;
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Set the relating key.
|
||||
// -----------------------------------------------------
|
||||
$eloquent->relating_key = $caller['function'].'_id';
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Return the Eloquent model.
|
||||
// -----------------------------------------------------
|
||||
return Factory::make($model)->where('id', '=', $eloquent->attributes[$eloquent->relating_key]);
|
||||
}
|
||||
|
||||
@@ -98,31 +70,12 @@ class Relate {
|
||||
// Get the models involved in the relationship.
|
||||
// -----------------------------------------------------
|
||||
$models = array(\System\Str::lower($model), \System\Str::lower(get_class($eloquent)));
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Sort the model names involved in the relationship.
|
||||
// -----------------------------------------------------
|
||||
sort($models);
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Get the intermediate table name, which is the names
|
||||
// of the two related models alphabetized.
|
||||
// -----------------------------------------------------
|
||||
$eloquent->relating_table = implode('_', $models);
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Set the relating type.
|
||||
// -----------------------------------------------------
|
||||
$eloquent->relating = __FUNCTION__;
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Set the relating key.
|
||||
// -----------------------------------------------------
|
||||
$eloquent->relating_key = $eloquent->relating_table.'.'.\System\Str::lower(get_class($eloquent)).'_id';
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Return the Eloquent model.
|
||||
// -----------------------------------------------------
|
||||
return Factory::make($model)
|
||||
->select(Meta::table($model).'.*')
|
||||
->join($eloquent->relating_table, Meta::table($model).'.id', '=', $eloquent->relating_table.'.'.\System\Str::lower($model).'_id')
|
||||
|
||||
@@ -6,13 +6,10 @@ class Warehouse {
|
||||
* Save an Eloquent model to the database.
|
||||
*
|
||||
* @param object $eloquent
|
||||
* @return void
|
||||
* @return bool
|
||||
*/
|
||||
public static function store($eloquent)
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Get the model name.
|
||||
// -----------------------------------------------------
|
||||
$model = get_class($eloquent);
|
||||
|
||||
// -----------------------------------------------------
|
||||
@@ -21,30 +18,25 @@ class Warehouse {
|
||||
$eloquent->query = \System\DB\Query::table(Meta::table($model));
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Set the activity timestamps.
|
||||
// Set the creation and update timestamps.
|
||||
// -----------------------------------------------------
|
||||
if (property_exists($model, 'timestamps') and $model::$timestamps)
|
||||
{
|
||||
static::timestamp($eloquent);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------
|
||||
// If the model exists in the database, update it.
|
||||
// Otherwise, insert the model and set the ID.
|
||||
// -----------------------------------------------------
|
||||
if ($eloquent->exists)
|
||||
{
|
||||
return $eloquent->query->where('id', '=', $eloquent->attributes['id'])->update($eloquent->dirty);
|
||||
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);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Set the existence flag to true.
|
||||
// -----------------------------------------------------
|
||||
$eloquent->exists = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user