warehouse.php 1.32 KB
Newer Older
1 2 3 4 5 6 7 8
<?php namespace System\DB\Eloquent;

class Warehouse {

	/**
	 * Save an Eloquent model to the database.
	 *
	 * @param  object  $eloquent
9
	 * @return bool
10 11 12 13 14 15 16 17 18 19 20
	 */
	public static function store($eloquent)
	{
		$model = get_class($eloquent);

		// -----------------------------------------------------
		// Get a fresh query instance for the model.
		// -----------------------------------------------------
		$eloquent->query = \System\DB\Query::table(Meta::table($model));

		// -----------------------------------------------------
21
		// Set the creation and update timestamps.
22 23 24 25 26 27 28 29
		// -----------------------------------------------------
		if (property_exists($model, 'timestamps') and $model::$timestamps)
		{
			static::timestamp($eloquent);
		}

		if ($eloquent->exists)
		{
30
			return ($eloquent->query->where('id', '=', $eloquent->attributes['id'])->update($eloquent->dirty) == 1) ? true : false;
31 32 33 34 35 36 37
		}
		else
		{
			$eloquent->attributes['id'] =  $eloquent->query->insert_get_id($eloquent->attributes);
		}

		$eloquent->exists = true;
38 39

		return true;
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	}

	/**
	 * 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;
		}
	}

}