Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
3ca50f30
Commit
3ca50f30
authored
Jan 13, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clean up AR
parent
46ab456f
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
114 additions
and
393 deletions
+114
-393
ActiveRecord.md
docs/api/db/ActiveRecord.md
+9
-0
ModelBehavior.php
framework/base/ModelBehavior.php
+0
-60
ActiveQuery.php
framework/db/ActiveQuery.php
+3
-3
ActiveRecord.php
framework/db/ActiveRecord.php
+13
-23
ActiveRecordBehavior.php
framework/db/ActiveRecordBehavior.php
+0
-102
Customer.php
tests/unit/data/ar/Customer.php
+2
-0
ActiveRecordTest.php
tests/unit/framework/db/ActiveRecordTest.php
+87
-205
No files found.
docs/api/db/ActiveRecord.md
0 → 100644
View file @
3ca50f30
-
`beforeInsert`
. Raised before the record is saved.
By setting
[
[\yii\base\ModelEvent::isValid
]
] to be false, the normal
[
[save()
]
] will be stopped.
-
`afterInsert`
. Raised after the record is saved.
-
`beforeUpdate`
. Raised before the record is saved.
By setting
[
[\yii\base\ModelEvent::isValid
]
] to be false, the normal
[
[save()
]
] will be stopped.
-
`afterUpdate`
. Raised after the record is saved.
-
`beforeDelete`
. Raised before the record is deleted.
By setting
[
[\yii\base\ModelEvent::isValid
]
] to be false, the normal
[
[delete()
]
] process will be stopped.
-
`afterDelete`
. Raised after the record is deleted.
framework/base/ModelBehavior.php
deleted
100644 → 0
View file @
46ab456f
<?php
/**
* ModelBehavior class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\base
;
/**
* ModelBehavior class.
*
* ModelBehavior is a base class for behaviors that are attached to a model object.
* The model should be an instance of [[Model]] or its child classes.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ModelBehavior
extends
Behavior
{
/**
* Declares event handlers for the owner's events.
* The default implementation returns the following event handlers:
*
* - `beforeValidate` event
* - `afterValidate` event
*
* You may override these event handler methods to respond to the corresponding owner events.
* @return array events (array keys) and the corresponding event handler methods (array values).
*/
public
function
events
()
{
return
array
(
'beforeValidate'
=>
'beforeValidate'
,
'afterValidate'
=>
'afterValidate'
,
);
}
/**
* Responds to the owner's `beforeValidate` event.
* Override this method if you want to handle the `beforeValidate` event of the [[owner]].
* You may set the [[ModelEvent::isValid|isValid]] property of the event parameter
* to be false to cancel the validation process.
* @param ModelEvent $event event parameter
*/
public
function
beforeValidate
(
$event
)
{
}
/**
* Responds to the owner's `afterValidate` event.
* Override this method if you want to handle the `beforeValidate` event of the [[owner]].
* @param Event $event event parameter
*/
public
function
afterValidate
(
$event
)
{
}
}
framework/db/ActiveQuery.php
View file @
3ca50f30
...
@@ -68,7 +68,7 @@ class ActiveQuery extends Query
...
@@ -68,7 +68,7 @@ class ActiveQuery extends Query
if
(
$rows
!==
array
())
{
if
(
$rows
!==
array
())
{
$models
=
$this
->
createModels
(
$rows
);
$models
=
$this
->
createModels
(
$rows
);
if
(
!
empty
(
$this
->
with
))
{
if
(
!
empty
(
$this
->
with
))
{
$this
->
fetchRelatedModel
s
(
$models
,
$this
->
with
);
$this
->
populateRelation
s
(
$models
,
$this
->
with
);
}
}
return
$models
;
return
$models
;
}
else
{
}
else
{
...
@@ -92,7 +92,7 @@ class ActiveQuery extends Query
...
@@ -92,7 +92,7 @@ class ActiveQuery extends Query
$model
=
$class
::
create
(
$row
);
$model
=
$class
::
create
(
$row
);
if
(
!
empty
(
$this
->
with
))
{
if
(
!
empty
(
$this
->
with
))
{
$models
=
array
(
$model
);
$models
=
array
(
$model
);
$this
->
fetchRelatedModel
s
(
$models
,
$this
->
with
);
$this
->
populateRelation
s
(
$models
,
$this
->
with
);
$model
=
$models
[
0
];
$model
=
$models
[
0
];
}
}
return
$model
;
return
$model
;
...
@@ -216,7 +216,7 @@ class ActiveQuery extends Query
...
@@ -216,7 +216,7 @@ class ActiveQuery extends Query
return
$models
;
return
$models
;
}
}
protected
function
fetchRelatedModel
s
(
&
$models
,
$with
)
protected
function
populateRelation
s
(
&
$models
,
$with
)
{
{
$primaryModel
=
new
$this
->
modelClass
;
$primaryModel
=
new
$this
->
modelClass
;
$relations
=
$this
->
normalizeRelations
(
$primaryModel
,
$with
);
$relations
=
$this
->
normalizeRelations
(
$primaryModel
,
$with
);
...
...
framework/db/ActiveRecord.php
View file @
3ca50f30
...
@@ -11,9 +11,9 @@
...
@@ -11,9 +11,9 @@
namespace
yii\db
;
namespace
yii\db
;
use
yii\base\Model
;
use
yii\base\Model
;
use
yii\base\Event
;
use
yii\base\ModelEvent
;
use
yii\base\ModelEvent
;
use
yii\base\BadMethodException
;
use
yii\base\BadMethodException
;
use
yii\base\BadParamException
;
use
yii\db\Exception
;
use
yii\db\Exception
;
use
yii\db\Connection
;
use
yii\db\Connection
;
use
yii\db\TableSchema
;
use
yii\db\TableSchema
;
...
@@ -23,23 +23,12 @@ use yii\util\StringHelper;
...
@@ -23,23 +23,12 @@ use yii\util\StringHelper;
/**
/**
* ActiveRecord is the base class for classes representing relational data.
* ActiveRecord is the base class for classes representing relational data.
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @include @yii/db/ActiveRecord.md
* @since 2.0
*
*
* @property array $attributes attribute values indexed by attribute names
* @property array $attributes attribute values indexed by attribute names
*
*
* ActiveRecord provides a set of events for further customization:
* @author Qiang Xue <qiang.xue@gmail.com>
*
* @since 2.0
* - `beforeInsert`. Raised before the record is saved.
* By setting [[\yii\base\ModelEvent::isValid]] to be false, the normal [[save()]] will be stopped.
* - `afterInsert`. Raised after the record is saved.
* - `beforeUpdate`. Raised before the record is saved.
* By setting [[\yii\base\ModelEvent::isValid]] to be false, the normal [[save()]] will be stopped.
* - `afterUpdate`. Raised after the record is saved.
* - `beforeDelete`. Raised before the record is deleted.
* By setting [[\yii\base\ModelEvent::isValid]] to be false, the normal [[delete()]] process will be stopped.
* - `afterDelete`. Raised after the record is deleted.
*
*/
*/
abstract
class
ActiveRecord
extends
Model
abstract
class
ActiveRecord
extends
Model
{
{
...
@@ -620,9 +609,6 @@ abstract class ActiveRecord extends Model
...
@@ -620,9 +609,6 @@ abstract class ActiveRecord extends Model
*/
*/
public
function
updateCounters
(
$counters
)
public
function
updateCounters
(
$counters
)
{
{
if
(
$this
->
getIsNewRecord
())
{
throw
new
Exception
(
'The active record cannot be updated because it is new.'
);
}
$this
->
updateAllCounters
(
$counters
,
$this
->
getOldPrimaryKey
(
true
));
$this
->
updateAllCounters
(
$counters
,
$this
->
getOldPrimaryKey
(
true
));
foreach
(
$counters
as
$name
=>
$value
)
{
foreach
(
$counters
as
$name
=>
$value
)
{
$this
->
_attributes
[
$name
]
+=
$value
;
$this
->
_attributes
[
$name
]
+=
$value
;
...
@@ -787,7 +773,8 @@ abstract class ActiveRecord extends Model
...
@@ -787,7 +773,8 @@ abstract class ActiveRecord extends Model
/**
/**
* Creates an active record with the given attributes.
* Creates an active record with the given attributes.
* Note that this method does not save the record into database.
* This method is called by [[ActiveQuery]] to populate the query results
* into Active Records.
* @param array $row attribute values (name => value)
* @param array $row attribute values (name => value)
* @return ActiveRecord the newly created active record.
* @return ActiveRecord the newly created active record.
*/
*/
...
@@ -833,9 +820,12 @@ abstract class ActiveRecord extends Model
...
@@ -833,9 +820,12 @@ abstract class ActiveRecord extends Model
}
}
/**
/**
* @param string $name
* Returns the relation object with the specified name.
* @return ActiveRelation
* A relation is defined by a getter method which returns an [[ActiveRelation]] object.
* @throws Exception
* It can be declared in either the Active Record class itself or one of its behaviors.
* @param string $name the relation name
* @return ActiveRelation the relation object
* @throws BadParamException if the named relation does not exist.
*/
*/
public
function
getRelation
(
$name
)
public
function
getRelation
(
$name
)
{
{
...
@@ -847,7 +837,7 @@ abstract class ActiveRecord extends Model
...
@@ -847,7 +837,7 @@ abstract class ActiveRecord extends Model
}
}
}
catch
(
BadMethodException
$e
)
{
}
catch
(
BadMethodException
$e
)
{
}
}
throw
new
Exception
(
'Unknown relation: '
.
$name
);
throw
new
BadParamException
(
get_class
(
$this
)
.
' has no relation named "'
.
$name
.
'".'
);
}
}
/**
/**
...
...
framework/db/ActiveRecordBehavior.php
deleted
100644 → 0
View file @
46ab456f
<?php
/**
* ActiveRecordBehavior class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\db
;
use
yii\base\ModelBehavior
;
/**
* ActiveRecordBehavior is the base class for behaviors that can be attached to [[ActiveRecord]].
*
* Compared to [[\yii\base\ModelBehavior]], ActiveRecordBehavior responds to more events
* that are specific to [[ActiveRecord]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ActiveRecordBehavior
extends
ModelBehavior
{
/**
* Declares events and the corresponding event handler methods.
* If you override this method, make sure you merge the parent result to the return value.
* @return array events (array keys) and the corresponding event handler methods (array values).
* @see \yii\base\Behavior::events()
*/
public
function
events
()
{
return
array_merge
(
parent
::
events
(),
array
(
'beforeInsert'
=>
'beforeInsert'
,
'afterInsert'
=>
'afterInsert'
,
'beforeUpdate'
=>
'beforeUpdate'
,
'afterUpdate'
=>
'afterUpdate'
,
'beforeDelete'
=>
'beforeDelete'
,
'afterDelete'
=>
'afterDelete'
,
));
}
/**
* Responds to the owner's `beforeInsert` event.
* Overrides this method if you want to handle the corresponding event of the owner.
* You may set the [[ModelEvent::isValid|isValid]] property of the event parameter
* to be false to quit the ActiveRecord inserting process.
* @param \yii\base\ModelEvent $event event parameter
*/
public
function
beforeInsert
(
$event
)
{
}
/**
* Responds to the owner's `afterInsert` event.
* Overrides this method if you want to handle the corresponding event of the owner.
* @param \yii\base\ModelEvent $event event parameter
*/
public
function
afterInsert
(
$event
)
{
}
/**
* Responds to the owner's `beforeUpdate` event.
* Overrides this method if you want to handle the corresponding event of the owner.
* You may set the [[ModelEvent::isValid|isValid]] property of the event parameter
* to be false to quit the ActiveRecord updating process.
* @param \yii\base\ModelEvent $event event parameter
*/
public
function
beforeUpdate
(
$event
)
{
}
/**
* Responds to the owner's `afterUpdate` event.
* Overrides this method if you want to handle the corresponding event of the owner.
* @param \yii\base\ModelEvent $event event parameter
*/
public
function
afterUpdate
(
$event
)
{
}
/**
* Responds to the owner's `beforeDelete` event.
* Overrides this method if you want to handle the corresponding event of the owner.
* You may set the [[ModelEvent::isValid|isValid]] property of the event parameter
* to be false to quit the ActiveRecord deleting process.
* @param \yii\base\ModelEvent $event event parameter
*/
public
function
beforeDelete
(
$event
)
{
}
/**
* Responds to the owner's `afterDelete` event.
* Overrides this method if you want to handle the corresponding event of the owner.
* @param \yii\base\ModelEvent $event event parameter
*/
public
function
afterDelete
(
$event
)
{
}
}
tests/unit/data/ar/Customer.php
View file @
3ca50f30
...
@@ -8,6 +8,8 @@ class Customer extends ActiveRecord
...
@@ -8,6 +8,8 @@ class Customer extends ActiveRecord
const
STATUS_ACTIVE
=
1
;
const
STATUS_ACTIVE
=
1
;
const
STATUS_INACTIVE
=
2
;
const
STATUS_INACTIVE
=
2
;
public
$status2
;
public
static
function
tableName
()
public
static
function
tableName
()
{
{
return
'tbl_customer'
;
return
'tbl_customer'
;
...
...
tests/unit/framework/db/ActiveRecordTest.php
View file @
3ca50f30
This diff is collapsed.
Click to expand it.
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