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
36f73025
Commit
36f73025
authored
Jan 02, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AR WIP
parent
f8664a38
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
28 deletions
+32
-28
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+24
-27
ActiveRelation.php
framework/db/ar/ActiveRelation.php
+8
-1
No files found.
framework/db/ar/ActiveRecord.php
View file @
36f73025
...
@@ -44,10 +44,6 @@ use yii\util\StringHelper;
...
@@ -44,10 +44,6 @@ use yii\util\StringHelper;
abstract
class
ActiveRecord
extends
Model
abstract
class
ActiveRecord
extends
Model
{
{
/**
/**
* @var ActiveRecord[] global model instances indexed by model class names
*/
private
static
$_models
=
array
();
/**
* @var array attribute values indexed by attribute names
* @var array attribute values indexed by attribute names
*/
*/
private
$_attributes
=
array
();
private
$_attributes
=
array
();
...
@@ -55,20 +51,11 @@ abstract class ActiveRecord extends Model
...
@@ -55,20 +51,11 @@ abstract class ActiveRecord extends Model
* @var array old attribute values indexed by attribute names.
* @var array old attribute values indexed by attribute names.
*/
*/
private
$_oldAttributes
;
private
$_oldAttributes
;
/**
/**
* Returns a model instance to support accessing non-static methods such as [[table()]], [[primaryKey()]].
* @var array related models indexed by the relation names
* @return ActiveRecord
*/
*/
public
static
function
model
()
private
$_related
;
{
$className
=
get_called_class
();
if
(
!
isset
(
self
::
$_models
[
$className
]))
{
self
::
$_models
[
$className
]
=
new
static
;
}
return
self
::
$_models
[
$className
];
}
/**
/**
* Returns the database connection used by this AR class.
* Returns the database connection used by this AR class.
...
@@ -87,7 +74,7 @@ abstract class ActiveRecord extends Model
...
@@ -87,7 +74,7 @@ abstract class ActiveRecord extends Model
* Because [[ActiveQuery]] implements a set of query building methods,
* Because [[ActiveQuery]] implements a set of query building methods,
* additional query conditions can be specified by calling the methods of [[ActiveQuery]].
* additional query conditions can be specified by calling the methods of [[ActiveQuery]].
*
*
* Below are some
usage
examples:
* Below are some examples:
*
*
* ~~~
* ~~~
* // find all customers
* // find all customers
...
@@ -110,7 +97,8 @@ abstract class ActiveRecord extends Model
...
@@ -110,7 +97,8 @@ abstract class ActiveRecord extends Model
*
*
* @param mixed $q the query parameter. This can be one of the followings:
* @param mixed $q the query parameter. This can be one of the followings:
*
*
* - a scalar value (integer or string): query by a single primary key value.
* - a scalar value (integer or string): query by a single primary key value and return the
* corresponding record.
* - an array of name-value pairs: it will be used to configure the [[ActiveQuery]] object.
* - an array of name-value pairs: it will be used to configure the [[ActiveQuery]] object.
*
*
* @return ActiveQuery|ActiveRecord|null the [[ActiveQuery]] instance for query purpose, or
* @return ActiveQuery|ActiveRecord|null the [[ActiveQuery]] instance for query purpose, or
...
@@ -119,14 +107,14 @@ abstract class ActiveRecord extends Model
...
@@ -119,14 +107,14 @@ abstract class ActiveRecord extends Model
*/
*/
public
static
function
find
(
$q
=
null
)
public
static
function
find
(
$q
=
null
)
{
{
$query
=
static
::
create
Active
Query
();
$query
=
static
::
createQuery
();
if
(
is_array
(
$q
))
{
if
(
is_array
(
$q
))
{
foreach
(
$q
as
$name
=>
$value
)
{
foreach
(
$q
as
$name
=>
$value
)
{
$query
->
$name
=
$value
;
$query
->
$name
=
$value
;
}
}
}
elseif
(
$q
!==
null
)
{
}
elseif
(
$q
!==
null
)
{
// query by primary key
// query by primary key
$primaryKey
=
static
::
model
()
->
primaryKey
();
$primaryKey
=
static
::
primaryKey
();
return
$query
->
where
(
array
(
$primaryKey
[
0
]
=>
$q
))
->
one
();
return
$query
->
where
(
array
(
$primaryKey
[
0
]
=>
$q
))
->
one
();
}
}
return
$query
;
return
$query
;
...
@@ -143,7 +131,7 @@ abstract class ActiveRecord extends Model
...
@@ -143,7 +131,7 @@ abstract class ActiveRecord extends Model
*/
*/
public
static
function
findBySql
(
$sql
,
$params
=
array
())
public
static
function
findBySql
(
$sql
,
$params
=
array
())
{
{
$query
=
static
::
create
Active
Query
();
$query
=
static
::
createQuery
();
$query
->
sql
=
$sql
;
$query
->
sql
=
$sql
;
return
$query
->
params
(
$params
);
return
$query
->
params
(
$params
);
}
}
...
@@ -177,7 +165,7 @@ abstract class ActiveRecord extends Model
...
@@ -177,7 +165,7 @@ abstract class ActiveRecord extends Model
*/
*/
public
static
function
count
(
$q
=
null
)
public
static
function
count
(
$q
=
null
)
{
{
$query
=
static
::
create
Active
Query
();
$query
=
static
::
createQuery
();
if
(
is_array
(
$q
))
{
if
(
is_array
(
$q
))
{
foreach
(
$q
as
$name
=>
$value
)
{
foreach
(
$q
as
$name
=>
$value
)
{
$query
->
$name
=
$value
;
$query
->
$name
=
$value
;
...
@@ -201,7 +189,7 @@ abstract class ActiveRecord extends Model
...
@@ -201,7 +189,7 @@ abstract class ActiveRecord extends Model
public
static
function
updateAll
(
$attributes
,
$condition
=
''
,
$params
=
array
())
public
static
function
updateAll
(
$attributes
,
$condition
=
''
,
$params
=
array
())
{
{
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
update
(
static
::
model
()
->
tableName
(),
$attributes
,
$condition
,
$params
);
$query
->
update
(
static
::
tableName
(),
$attributes
,
$condition
,
$params
);
return
$query
->
createCommand
(
static
::
getDbConnection
())
->
execute
();
return
$query
->
createCommand
(
static
::
getDbConnection
())
->
execute
();
}
}
...
@@ -222,7 +210,7 @@ abstract class ActiveRecord extends Model
...
@@ -222,7 +210,7 @@ abstract class ActiveRecord extends Model
$counters
[
$name
]
=
new
Expression
(
$value
>=
0
?
"
$quotedName
+
$value
"
:
"
$quotedName$value
"
);
$counters
[
$name
]
=
new
Expression
(
$value
>=
0
?
"
$quotedName
+
$value
"
:
"
$quotedName$value
"
);
}
}
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
update
(
static
::
model
()
->
tableName
(),
$counters
,
$condition
,
$params
);
$query
->
update
(
static
::
tableName
(),
$counters
,
$condition
,
$params
);
return
$query
->
createCommand
(
$db
)
->
execute
();
return
$query
->
createCommand
(
$db
)
->
execute
();
}
}
...
@@ -236,7 +224,7 @@ abstract class ActiveRecord extends Model
...
@@ -236,7 +224,7 @@ abstract class ActiveRecord extends Model
public
static
function
deleteAll
(
$condition
=
''
,
$params
=
array
())
public
static
function
deleteAll
(
$condition
=
''
,
$params
=
array
())
{
{
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
delete
(
static
::
model
()
->
tableName
(),
$condition
,
$params
);
$query
->
delete
(
static
::
tableName
(),
$condition
,
$params
);
return
$query
->
createCommand
(
static
::
getDbConnection
())
->
execute
();
return
$query
->
createCommand
(
static
::
getDbConnection
())
->
execute
();
}
}
...
@@ -245,7 +233,7 @@ abstract class ActiveRecord extends Model
...
@@ -245,7 +233,7 @@ abstract class ActiveRecord extends Model
* This method is called by [[find()]] and [[findBySql()]] to start a SELECT query.
* This method is called by [[find()]] and [[findBySql()]] to start a SELECT query.
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
* @return ActiveQuery the newly created [[ActiveQuery]] instance.
*/
*/
public
static
function
create
Active
Query
()
public
static
function
createQuery
()
{
{
return
new
ActiveQuery
(
array
(
'modelClass'
=>
get_called_class
()));
return
new
ActiveQuery
(
array
(
'modelClass'
=>
get_called_class
()));
}
}
...
@@ -312,6 +300,7 @@ abstract class ActiveRecord extends Model
...
@@ -312,6 +300,7 @@ abstract class ActiveRecord extends Model
return
null
;
return
null
;
}
elseif
(
method_exists
(
$this
,
$name
))
{
}
elseif
(
method_exists
(
$this
,
$name
))
{
// lazy loading related records
// lazy loading related records
/** @var $query ActiveRelation */
$query
=
$this
->
$name
();
$query
=
$this
->
$name
();
return
$this
->
_attributes
[
$name
]
=
$query
->
multiple
?
$query
->
all
()
:
$query
->
one
();
return
$this
->
_attributes
[
$name
]
=
$query
->
multiple
?
$query
->
all
()
:
$query
->
one
();
}
else
{
}
else
{
...
@@ -465,7 +454,7 @@ abstract class ActiveRecord extends Model
...
@@ -465,7 +454,7 @@ abstract class ActiveRecord extends Model
/**
/**
* Returns the list of all attribute names of the model.
* Returns the list of all attribute names of the model.
* Th
is would
return all column names of the table associated with this AR class.
* Th
e default implementation will
return all column names of the table associated with this AR class.
* @return array list of attribute names.
* @return array list of attribute names.
*/
*/
public
function
attributes
()
public
function
attributes
()
...
@@ -501,6 +490,12 @@ abstract class ActiveRecord extends Model
...
@@ -501,6 +490,12 @@ abstract class ActiveRecord extends Model
$this
->
_attributes
[
$name
]
=
$value
;
$this
->
_attributes
[
$name
]
=
$value
;
}
}
/**
* Returns the attribute values that have been modified since they are loaded or saved most recently.
* @param string[]|null $names the names of the attributes whose values may be returned if they are
* changed recently. If null, [[attributes()]] will be used.
* @return array the changed attribute values (name-value pairs)
*/
public
function
getChangedAttributes
(
$names
=
null
)
public
function
getChangedAttributes
(
$names
=
null
)
{
{
if
(
$names
===
null
)
{
if
(
$names
===
null
)
{
...
@@ -904,4 +899,6 @@ abstract class ActiveRecord extends Model
...
@@ -904,4 +899,6 @@ abstract class ActiveRecord extends Model
{
{
return
$this
->
__isset
(
$offset
);
return
$this
->
__isset
(
$offset
);
}
}
}
}
framework/db/ar/ActiveRelation.php
View file @
36f73025
...
@@ -47,9 +47,16 @@ class ActiveRelation extends BaseActiveQuery
...
@@ -47,9 +47,16 @@ class ActiveRelation extends BaseActiveQuery
*/
*/
public
$via
;
public
$via
;
public
function
get
()
public
function
one
()
{
{
$models
=
$this
->
all
();
return
isset
(
$models
[
0
])
?
$models
[
0
]
:
null
;
}
public
function
all
()
{
$models
=
array
();
return
$models
;
}
}
public
function
findWith
(
$name
,
&
$primaryRecords
)
public
function
findWith
(
$name
,
&
$primaryRecords
)
...
...
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