Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
UserAdminV2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
庄欣
UserAdminV2
Commits
ca065823
Commit
ca065823
authored
Apr 26, 2012
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add some events to Eloquent models.
parent
1f3d269a
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
67 additions
and
13 deletions
+67
-13
laravel/database/eloquent/model.php
+56
-5
laravel/database/eloquent/query.php
+2
-6
laravel/documentation/changes.md
+1
-0
laravel/event.php
+8
-2
No files found.
laravel/database/eloquent/model.php
View file @
ca065823
<?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.
...
...
@@ -155,13 +165,28 @@ abstract class Model {
}
/**
* 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
;
}
}
...
...
@@ -586,6 +624,19 @@ abstract class Model {
}
/**
* 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.
*
* @param string $key
...
...
laravel/database/eloquent/query.php
View file @
ca065823
<?php
namespace
Laravel\Database\Eloquent
;
use
Laravel\Event
;
use
Laravel\Database
;
use
Laravel\Database\Eloquent\Relationships\Has_Many_And_Belongs_To
;
...
...
@@ -119,12 +120,7 @@ class Query {
// We need to set the attributes manually in case the accessible property is
// set on the array which will prevent the mass assignemnt of attributes if
// we were to pass them in using the constructor or fill methods.
foreach
(
$result
as
$key
=>
$value
)
{
$new
->
set_attribute
(
$key
,
$value
);
}
$new
->
original
=
$new
->
attributes
;
$new
->
fill_raw
(
$result
);
$models
[]
=
$new
;
}
...
...
laravel/documentation/changes.md
View file @
ca065823
...
...
@@ -61,6 +61,7 @@
-
`Schema::drop`
now accepts
`$connection`
as second parameter.
-
Added
`Input::merge`
method.
-
Added
`Input::replace`
method.
-
Added
`eloquent.saved`
,
`eloquent.saving`
,
`eloquent.deleting`
, and
`eloquent.deleted`
events to Eloquent models.
<a
name=
"upgrade-3.2"
></a>
## Upgrading From 3.1
...
...
laravel/event.php
View file @
ca065823
...
...
@@ -108,14 +108,17 @@ class Event {
*
* // Fire the "start" event passing an array of parameters
* $responses = Event::fire('start', array('Laravel', 'Framework'));
*
* // Fire multiple events with the same parameters
* $responses = Event::fire(array('start', 'loading'), $parameters);
* </code>
*
* @param string $event
* @param string
|array
$event
* @param array $parameters
* @param bool $halt
* @return array
*/
public
static
function
fire
(
$event
,
$parameters
=
array
(),
$halt
=
false
)
public
static
function
fire
(
$event
s
,
$parameters
=
array
(),
$halt
=
false
)
{
$responses
=
array
();
...
...
@@ -124,6 +127,8 @@ class Event {
// If the event has listeners, we will simply iterate through them and call
// each listener, passing in the parameters. We will add the responses to
// an array of event responses and return the array.
foreach
((
array
)
$events
as
$event
)
{
if
(
static
::
listeners
(
$event
))
{
foreach
(
static
::
$events
[
$event
]
as
$callback
)
...
...
@@ -144,6 +149,7 @@ class Event {
$responses
[]
=
$response
;
}
}
}
return
$responses
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment