|
@@ -1,6 +1,7 @@
|
|
|
<?php namespace Laravel\Database\Eloquent;
|
|
|
|
|
|
use Laravel\Str;
|
|
|
+use Laravel\Event;
|
|
|
use Laravel\Database;
|
|
|
use Laravel\Database\Eloquent\Relationships\Has_Many_And_Belongs_To;
|
|
|
|
|
@@ -115,14 +116,23 @@ abstract class Model {
|
|
|
* Hydrate the model with an array of attributes.
|
|
|
*
|
|
|
* @param array $attributes
|
|
|
+ * @param bool $raw
|
|
|
* @return Model
|
|
|
*/
|
|
|
- public function fill($attributes)
|
|
|
+ public function fill(array $attributes, $raw = false)
|
|
|
{
|
|
|
- $attributes = (array) $attributes;
|
|
|
-
|
|
|
foreach ($attributes as $key => $value)
|
|
|
{
|
|
|
+ // If the "raw" flag is set, it means that we'll just load every value from
|
|
|
+ // the array directly into the attributes, without any accessibility or
|
|
|
+ // mutators being accounted for. What you pass in is what you get.
|
|
|
+ if ($raw)
|
|
|
+ {
|
|
|
+ $this->set_attribute($key, $value);
|
|
|
+
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
// If the "accessible" property is an array, the developer is limiting the
|
|
|
// attributes that may be mass assigned, and we need to verify that the
|
|
|
// current attribute is included in that list of allowed attributes.
|
|
@@ -154,14 +164,29 @@ abstract class Model {
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Fill the model with the contents of the array.
|
|
|
+ *
|
|
|
+ * No mutators or accessibility checks will be accounted for.
|
|
|
+ *
|
|
|
+ * @param array $attributes
|
|
|
+ * @return Model
|
|
|
+ */
|
|
|
+ public function fill_raw(array $attributes)
|
|
|
+ {
|
|
|
+ return $this->fill($attributes, true);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Set the accessible attributes for the given model.
|
|
|
*
|
|
|
* @param array $attributes
|
|
|
* @return void
|
|
|
*/
|
|
|
- public static function accessible($attributes)
|
|
|
+ public static function accessible($attributes = null)
|
|
|
{
|
|
|
+ if (is_null($attributes)) return static::$accessible;
|
|
|
+
|
|
|
static::$accessible = $attributes;
|
|
|
}
|
|
|
|
|
@@ -355,6 +380,8 @@ abstract class Model {
|
|
|
$this->timestamp();
|
|
|
}
|
|
|
|
|
|
+ $this->fire_event('saving');
|
|
|
+
|
|
|
// If the model exists, we only need to update it in the database, and the update
|
|
|
// will be considered successful if there is one affected row returned from the
|
|
|
// fluent query instance. We'll set the where condition automatically.
|
|
@@ -382,6 +409,11 @@ abstract class Model {
|
|
|
// dirty and subsequent calls won't hit the database.
|
|
|
$this->original = $this->attributes;
|
|
|
|
|
|
+ if ($result)
|
|
|
+ {
|
|
|
+ $this->fire_event('saved');
|
|
|
+ }
|
|
|
+
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
@@ -394,7 +426,13 @@ abstract class Model {
|
|
|
{
|
|
|
if ($this->exists)
|
|
|
{
|
|
|
- return $this->query()->where(static::$key, '=', $this->get_key())->delete();
|
|
|
+ $this->fire_event('deleting');
|
|
|
+
|
|
|
+ $result = $this->query()->where(static::$key, '=', $this->get_key())->delete();
|
|
|
+
|
|
|
+ $this->fire_event('deleted');
|
|
|
+
|
|
|
+ return $result;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -585,6 +623,19 @@ abstract class Model {
|
|
|
return $attributes;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Fire a given event for the model.
|
|
|
+ *
|
|
|
+ * @param string $event
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ protected function fire_event($event)
|
|
|
+ {
|
|
|
+ $events = array("eloquent.{$event}", "eloquent.{$event}: ".get_class($this));
|
|
|
+
|
|
|
+ Event::fire($events, array($this));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Handle the dynamic retrieval of attributes and associations.
|
|
|
*
|