trimmed comment bloat. returning boolean on eloquent save.
This commit is contained in:
@@ -36,9 +36,6 @@ class Connector {
|
||||
{
|
||||
$connection = new \PDO($config->driver.':host='.$config->host.';dbname='.$config->database, $config->username, $config->password, static::$options);
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Set the correct character set.
|
||||
// ---------------------------------------------------
|
||||
if (isset($config->charset))
|
||||
{
|
||||
$connection->prepare("SET NAMES '".$config->charset."'")->execute();
|
||||
@@ -46,9 +43,6 @@ class Connector {
|
||||
|
||||
return $connection;
|
||||
}
|
||||
// ---------------------------------------------------
|
||||
// If the driver isn't supported, bail out.
|
||||
// ---------------------------------------------------
|
||||
else
|
||||
{
|
||||
throw new \Exception('Database driver '.$config->driver.' is not supported.');
|
||||
|
||||
@@ -76,14 +76,7 @@ abstract class Eloquent {
|
||||
*/
|
||||
public static function with()
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Create a new model instance.
|
||||
// -----------------------------------------------------
|
||||
$model = Eloquent\Factory::make(get_called_class());
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Set the eager relationships.
|
||||
// -----------------------------------------------------
|
||||
$model->includes = func_get_args();
|
||||
|
||||
return $model;
|
||||
@@ -117,14 +110,8 @@ abstract class Eloquent {
|
||||
*/
|
||||
private function _first()
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Load the hydrated models.
|
||||
// -----------------------------------------------------
|
||||
$results = Eloquent\Hydrate::from($this->take(1));
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Return the first result.
|
||||
// -----------------------------------------------------
|
||||
if (count($results) > 0)
|
||||
{
|
||||
reset($results);
|
||||
@@ -185,11 +172,16 @@ abstract class Eloquent {
|
||||
/**
|
||||
* Save the model to the database.
|
||||
*
|
||||
* @return void
|
||||
* @return bool
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
Eloquent\Warehouse::store($this);
|
||||
if ($this->exists and count($this->dirty) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return Eloquent\Warehouse::store($this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -215,17 +207,11 @@ abstract class Eloquent {
|
||||
// -----------------------------------------------------
|
||||
$model = $this->$key();
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Return the relationship results.
|
||||
// -----------------------------------------------------
|
||||
return ($this->relating == 'has_one' or $this->relating == 'belongs_to')
|
||||
? $this->ignore[$key] = $model->first()
|
||||
: $this->ignore[$key] = $model->get();
|
||||
}
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Check the "regular" attributes.
|
||||
// -----------------------------------------------------
|
||||
return (array_key_exists($key, $this->attributes)) ? $this->attributes[$key] : null;
|
||||
}
|
||||
|
||||
@@ -243,9 +229,6 @@ abstract class Eloquent {
|
||||
}
|
||||
else
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Add the value to the attributes.
|
||||
// -----------------------------------------------------
|
||||
$this->attributes[$key] = $value;
|
||||
$this->dirty[$key] = $value;
|
||||
}
|
||||
@@ -274,17 +257,11 @@ abstract class Eloquent {
|
||||
*/
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Is the "get" method being called?
|
||||
// -----------------------------------------------------
|
||||
if ($method == 'get')
|
||||
{
|
||||
return $this->_get();
|
||||
}
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Is the "first" method being called?
|
||||
// -----------------------------------------------------
|
||||
if ($method == 'first')
|
||||
{
|
||||
return $this->_first();
|
||||
@@ -312,22 +289,13 @@ abstract class Eloquent {
|
||||
*/
|
||||
public static function __callStatic($method, $parameters)
|
||||
{
|
||||
// -----------------------------------------------------
|
||||
// Create a new model instance.
|
||||
// -----------------------------------------------------
|
||||
$model = Eloquent\Factory::make(get_called_class());
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Do we need to return the entire table?
|
||||
// -----------------------------------------------------
|
||||
if ($method == 'get')
|
||||
{
|
||||
return $model->_get();
|
||||
}
|
||||
|
||||
// -----------------------------------------------------
|
||||
// Do we need to return the first model from the table?
|
||||
// -----------------------------------------------------
|
||||
if ($method == 'first')
|
||||
{
|
||||
return $model->_first();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -81,14 +81,7 @@ class Query {
|
||||
*/
|
||||
public function __construct($table, $connection = null)
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Set the database connection name.
|
||||
// ---------------------------------------------------
|
||||
$this->connection = (is_null($connection)) ? \System\Config::get('db.default') : $connection;
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Build the FROM clause.
|
||||
// ---------------------------------------------------
|
||||
$this->from = 'FROM '.$this->wrap($this->table = $table);
|
||||
}
|
||||
|
||||
@@ -122,9 +115,6 @@ class Query {
|
||||
*/
|
||||
public function select()
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Handle DISTINCT selections.
|
||||
// ---------------------------------------------------
|
||||
$this->select = ($this->distinct) ? 'SELECT DISTINCT ' : 'SELECT ';
|
||||
|
||||
// ---------------------------------------------------
|
||||
@@ -372,15 +362,7 @@ class Query {
|
||||
*/
|
||||
public function find($id)
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Set the primary key.
|
||||
// ---------------------------------------------------
|
||||
$this->where('id', '=', $id);
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Get the first result.
|
||||
// ---------------------------------------------------
|
||||
return $this->first();
|
||||
return $this->where('id', '=', $id)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -400,9 +382,6 @@ class Query {
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Initialize the SELECT clause if it's null.
|
||||
// ---------------------------------------------------
|
||||
if (is_null($this->select))
|
||||
{
|
||||
call_user_func_array(array($this, 'select'), (count(func_get_args()) > 0) ? func_get_args() : array('*'));
|
||||
@@ -420,14 +399,8 @@ class Query {
|
||||
*/
|
||||
private function aggregate($aggregator, $column)
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Build the SELECT clause.
|
||||
// ---------------------------------------------------
|
||||
$this->select = 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate');
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Execute the statement.
|
||||
// ---------------------------------------------------
|
||||
$results = \System\DB::query(Query\Compiler::select($this), $this->bindings);
|
||||
|
||||
return $results[0]->aggregate;
|
||||
@@ -452,54 +425,28 @@ class Query {
|
||||
*/
|
||||
public function insert_get_id($values)
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Compile the SQL statement.
|
||||
// ---------------------------------------------------
|
||||
$sql = Query\Compiler::insert($this, $values);
|
||||
|
||||
// ---------------------------------------------------
|
||||
// The Postgres PDO implementation does not cleanly
|
||||
// implement the last insert ID function. So, we'll
|
||||
// use the RETURNING clause available in Postgres.
|
||||
// Postgres.
|
||||
// ---------------------------------------------------
|
||||
if (\System\DB::connection($this->connection)->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'pgsql')
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Add the RETURNING clause to the SQL.
|
||||
// ---------------------------------------------------
|
||||
$sql .= ' RETURNING '.$this->wrap('id');
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Prepare the PDO statement.
|
||||
// ---------------------------------------------------
|
||||
$query = \System\DB::connection($this->connection)->prepare($sql);
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Execute the PDO statement.
|
||||
// ---------------------------------------------------
|
||||
$query->execute(array_values($values));
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Fetch the insert ID from the results.
|
||||
// ---------------------------------------------------
|
||||
$result = $query->fetch(\PDO::FETCH_ASSOC);
|
||||
|
||||
return $result['id'];
|
||||
}
|
||||
// ---------------------------------------------------
|
||||
// When using MySQL or SQLite, we can just use the PDO
|
||||
// last insert ID function.
|
||||
// MySQL and SQLite.
|
||||
// ---------------------------------------------------
|
||||
else
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Execute the statement.
|
||||
// ---------------------------------------------------
|
||||
\System\DB::query($sql, array_values($values), $this->connection);
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Get the last insert ID.
|
||||
// ---------------------------------------------------
|
||||
return \System\DB::connection($this->connection)->lastInsertId();
|
||||
}
|
||||
}
|
||||
@@ -523,17 +470,11 @@ class Query {
|
||||
*/
|
||||
public function delete($id = null)
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Set the primary key.
|
||||
// ---------------------------------------------------
|
||||
if ( ! is_null($id))
|
||||
{
|
||||
$this->where('id', '=', $id);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Execute the statement.
|
||||
// ---------------------------------------------------
|
||||
return \System\DB::query(Query\Compiler::delete($this), $this->bindings, $this->connection);
|
||||
}
|
||||
|
||||
@@ -555,9 +496,6 @@ class Query {
|
||||
$wrap = '`';
|
||||
}
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Wrap the element in keyword identifiers.
|
||||
// ---------------------------------------------------
|
||||
return implode('.', array_map(function($segment) use ($wrap) {return ($segment != '*') ? $wrap.$segment.$wrap : $segment;}, explode('.', $value)));
|
||||
}
|
||||
|
||||
|
||||
@@ -10,30 +10,18 @@ class Compiler {
|
||||
*/
|
||||
public static function select($query)
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Add the SELECT, FROM, and WHERE clauses.
|
||||
// ---------------------------------------------------
|
||||
$sql = $query->select.' '.$query->from.' '.$query->where;
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Add the ORDER BY clause.
|
||||
// ---------------------------------------------------
|
||||
if (count($query->orderings) > 0)
|
||||
{
|
||||
$sql .= ' ORDER BY '.implode(', ', $query->orderings);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Add the LIMIT.
|
||||
// ---------------------------------------------------
|
||||
if ( ! is_null($query->limit))
|
||||
{
|
||||
$sql .= ' LIMIT '.$query->limit;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Add the OFFSET.
|
||||
// ---------------------------------------------------
|
||||
if ( ! is_null($query->offset))
|
||||
{
|
||||
$sql .= ' OFFSET '.$query->offset;
|
||||
@@ -51,9 +39,6 @@ class Compiler {
|
||||
*/
|
||||
public static function insert($query, $values)
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Start the query. Add the table name.
|
||||
// ---------------------------------------------------
|
||||
$sql = 'INSERT INTO '.$query->table.' (';
|
||||
|
||||
// ---------------------------------------------------
|
||||
@@ -66,9 +51,6 @@ class Compiler {
|
||||
$columns[] = $query->wrap($column);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Concatenate the column names and values.
|
||||
// ---------------------------------------------------
|
||||
return $sql .= implode(', ', $columns).') VALUES ('.$query->parameterize($values).')';
|
||||
}
|
||||
|
||||
@@ -81,13 +63,10 @@ class Compiler {
|
||||
*/
|
||||
public static function update($query, $values)
|
||||
{
|
||||
// ---------------------------------------------------
|
||||
// Start the query. Add the table name.
|
||||
// ---------------------------------------------------
|
||||
$sql = 'UPDATE '.$query->table.' SET ';
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Wrap each column name in keyword identifiers.
|
||||
// Add each column set the query.
|
||||
// ---------------------------------------------------
|
||||
$columns = array();
|
||||
|
||||
@@ -96,9 +75,6 @@ class Compiler {
|
||||
$columns[] = $query->wrap($column).' = ?';
|
||||
}
|
||||
|
||||
// ---------------------------------------------------
|
||||
// Concatenate the column names and the WHERE clause.
|
||||
// ---------------------------------------------------
|
||||
return $sql .= implode(', ', $columns).' '.$query->where;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user