Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Y
yii2bootstrap
Project
Project
Details
Activity
Releases
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
夏彬
yii2bootstrap
Commits
a92acf65
Commit
a92acf65
authored
Jan 26, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
2f37d093
Changes
10
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
1300 additions
and
1296 deletions
+1300
-1296
Component.php
framework/base/Component.php
+95
-44
Model.php
framework/base/Model.php
+31
-97
ActiveMetaData.php
framework/db/ar/ActiveMetaData.php
+76
-0
ActiveQuery.php
framework/db/ar/ActiveQuery.php
+233
-0
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+47
-453
ActiveRelation.php
framework/db/ar/ActiveRelation.php
+43
-0
BaseQuery.php
framework/db/dao/BaseQuery.php
+715
-0
Query.php
framework/db/dao/Query.php
+19
-697
Text.php
framework/util/Text.php
+21
-2
BehaviorTest.php
tests/unit/framework/base/BehaviorTest.php
+20
-3
No files found.
framework/base/Component.php
View file @
a92acf65
...
@@ -29,12 +29,12 @@ namespace yii\base;
...
@@ -29,12 +29,12 @@ namespace yii\base;
* Event names are case-insensitive.
* Event names are case-insensitive.
*
*
* An event can be attached with one or multiple PHP callbacks, called *event handlers*.
* An event can be attached with one or multiple PHP callbacks, called *event handlers*.
* One can call [[raiseEvent]] to raise an event. When an event is raised, the attached
* One can call [[raiseEvent
()
]] to raise an event. When an event is raised, the attached
* event handlers will be invoked automatically in the order they are attached to the event.
* event handlers will be invoked automatically in the order they are attached to the event.
*
*
* To attach an event handler to an event, call [[attachEventHandler]]. Alternatively,
* To attach an event handler to an event, call [[attachEventHandler]]. Alternatively,
* you can use the assignment syntax: `$component->onClick = $callback;`,
* you can use the assignment syntax: `$component->onClick = $callback;`,
* where `$callback` refers to a valid PHP callback
,
which can be one of the followings:
* where `$callback` refers to a valid PHP callback which can be one of the followings:
*
*
* - global function: `'handleOnClick'`
* - global function: `'handleOnClick'`
* - object method: `array($object, 'handleOnClick')`
* - object method: `array($object, 'handleOnClick')`
...
@@ -59,21 +59,25 @@ namespace yii\base;
...
@@ -59,21 +59,25 @@ namespace yii\base;
* ~~~
* ~~~
*
*
*
*
* A behavior is an instance of [[Behavior]] or its child class. When a behavior is
* A behavior is an instance of [[Behavior]] or its child class. A component can be attached
* attached to a component, its public properties and methods can be accessed via the
* with one or multiple behaviors. When a behavior is attached to a component, its public
* component directly, as if the component owns those properties and methods.
* properties and methods can be accessed via the component directly, as if the component owns
* those properties and methods.
*
*
* Multiple behaviors can be attached to the same component.
* To attach a behavior to a component, declare it in [[behaviors()]], or explicitly call [[attachBehavior]].
*
* To attach a behavior to a component, call [[attachBehavior]]; to detach a behavior
* from the component, call [[detachBehavior]].
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
Component
extends
Object
class
Component
extends
Object
{
{
/**
* @var Vector[] the attached event handlers (event name => handlers)
*/
private
$_e
;
private
$_e
;
/**
* @var Behavior[] the attached behaviors (behavior name => behavior)
*/
private
$_b
;
private
$_b
;
/**
/**
...
@@ -104,12 +108,11 @@ class Component extends Object
...
@@ -104,12 +108,11 @@ class Component extends Object
$this
->
_e
[
$name
]
=
new
Vector
;
$this
->
_e
[
$name
]
=
new
Vector
;
}
}
return
$this
->
_e
[
$name
];
return
$this
->
_e
[
$name
];
}
elseif
(
isset
(
$this
->
_b
[
$name
]))
{
// behavior
}
else
{
// behavior property
return
$this
->
_b
[
$name
];
$this
->
ensureBehaviors
();
}
elseif
(
is_array
(
$this
->
_b
))
{
// a behavior property
foreach
(
$this
->
_b
as
$behavior
)
{
foreach
(
$this
->
_b
as
$object
)
{
if
(
$behavior
->
canGetProperty
(
$name
))
{
if
(
$object
->
canGetProperty
(
$name
))
{
return
$behavior
->
$name
;
return
$object
->
$name
;
}
}
}
}
}
}
...
@@ -143,10 +146,11 @@ class Component extends Object
...
@@ -143,10 +146,11 @@ class Component extends Object
$this
->
_e
[
$name
]
=
new
Vector
;
$this
->
_e
[
$name
]
=
new
Vector
;
}
}
return
$this
->
_e
[
$name
]
->
add
(
$value
);
return
$this
->
_e
[
$name
]
->
add
(
$value
);
}
elseif
(
is_array
(
$this
->
_b
))
{
// behavior
}
else
{
// behavior property
foreach
(
$this
->
_b
as
$object
)
{
$this
->
ensureBehaviors
();
if
(
$object
->
canSetProperty
(
$name
))
{
foreach
(
$this
->
_b
as
$behavior
)
{
return
$object
->
$name
=
$value
;
if
(
$behavior
->
canSetProperty
(
$name
))
{
return
$behavior
->
$name
=
$value
;
}
}
}
}
}
}
...
@@ -178,12 +182,11 @@ class Component extends Object
...
@@ -178,12 +182,11 @@ class Component extends Object
}
elseif
(
method_exists
(
$this
,
$name
)
&&
strncasecmp
(
$name
,
'on'
,
2
)
===
0
)
{
// has event handler
}
elseif
(
method_exists
(
$this
,
$name
)
&&
strncasecmp
(
$name
,
'on'
,
2
)
===
0
)
{
// has event handler
$name
=
strtolower
(
$name
);
$name
=
strtolower
(
$name
);
return
isset
(
$this
->
_e
[
$name
])
&&
$this
->
_e
[
$name
]
->
getCount
();
return
isset
(
$this
->
_e
[
$name
])
&&
$this
->
_e
[
$name
]
->
getCount
();
}
elseif
(
isset
(
$this
->
_b
[
$name
]))
{
// has behavior
}
else
{
// behavior property
return
true
;
$this
->
ensureBehaviors
();
}
elseif
(
is_array
(
$this
->
_b
))
{
foreach
(
$this
->
_b
as
$behavior
)
{
foreach
(
$this
->
_b
as
$object
)
{
if
(
$behavior
->
canGetProperty
(
$name
))
{
if
(
$object
->
canGetProperty
(
$name
))
{
return
$behavior
->
$name
!==
null
;
return
$object
->
$name
!==
null
;
}
}
}
}
}
}
...
@@ -207,16 +210,17 @@ class Component extends Object
...
@@ -207,16 +210,17 @@ class Component extends Object
{
{
$setter
=
'set'
.
$name
;
$setter
=
'set'
.
$name
;
if
(
method_exists
(
$this
,
$setter
))
{
// write property
if
(
method_exists
(
$this
,
$setter
))
{
// write property
return
$this
->
$setter
(
null
);
$this
->
$setter
(
null
);
return
;
}
elseif
(
method_exists
(
$this
,
$name
)
&&
strncasecmp
(
$name
,
'on'
,
2
)
===
0
)
{
// event
}
elseif
(
method_exists
(
$this
,
$name
)
&&
strncasecmp
(
$name
,
'on'
,
2
)
===
0
)
{
// event
unset
(
$this
->
_e
[
strtolower
(
$name
)]);
unset
(
$this
->
_e
[
strtolower
(
$name
)]);
return
;
return
;
}
else
if
(
isset
(
$this
->
_b
[
$name
]))
{
// behavior
}
else
{
// behavior property
return
$this
->
detachBehavior
(
$name
);
$this
->
ensureBehaviors
(
);
}
elseif
(
is_array
(
$this
->
_b
))
{
// behavior property
foreach
(
$this
->
_b
as
$behavior
)
{
foreach
(
$this
->
_b
as
$object
)
{
if
(
$behavior
->
canSetProperty
(
$name
)
)
{
if
(
$object
->
canSetProperty
(
$name
))
{
$behavior
->
$name
=
null
;
return
$object
->
$name
=
null
;
return
;
}
}
}
}
}
}
...
@@ -247,14 +251,42 @@ class Component extends Object
...
@@ -247,14 +251,42 @@ class Component extends Object
}
}
}
}
if
(
$this
->
_b
!==
null
)
{
$this
->
ensureBehaviors
();
foreach
(
$this
->
_b
as
$object
)
{
foreach
(
$this
->
_b
as
$object
)
{
if
(
method_exists
(
$object
,
$name
))
{
if
(
method_exists
(
$object
,
$name
))
{
return
call_user_func_array
(
array
(
$object
,
$name
),
$params
);
return
call_user_func_array
(
array
(
$object
,
$name
),
$params
);
}
}
}
}
throw
new
Exception
(
'Calling unknown method: '
.
get_class
(
$this
)
.
"::
$name
()"
);
}
}
throw
new
Exception
(
'Unknown method: '
.
get_class
(
$this
)
.
"::
$name
()"
);
/**
* Returns a list of behaviors that this component should behave as.
*
* Child classes may override this method to specify the behaviors they want to behave as.
*
* The return value of this method should be an array of behavior objects or configurations
* indexed by behavior names. A behavior configuration can be either a string specifying
* the behavior class or an array of the following structure:
*
* ~~~
* 'behaviorName' => array(
* 'class' => 'BehaviorClass',
* 'property1' => 'value1',
* 'property2' => 'value2',
* )
* ~~~
*
* Note that a behavior class must extend from [[Behavior]].
*
* Behaviors declared in this method will be attached to the component on demand.
*
* @return array the behavior configurations.
*/
public
function
behaviors
()
{
return
array
();
}
}
/**
/**
...
@@ -270,12 +302,13 @@ class Component extends Object
...
@@ -270,12 +302,13 @@ class Component extends Object
}
}
/**
/**
* Returns a value indicating whether there is any handler attached to the event.
* Returns a value indicating whether there is any handler attached to the
named
event.
* @param string $name the event name
* @param string $name the event name
* @return boolean whether there is any handler attached to the event.
* @return boolean whether there is any handler attached to the event.
*/
*/
public
function
hasEventHandlers
(
$name
)
public
function
hasEventHandlers
(
$name
)
{
{
$this
->
ensureBehaviors
();
$name
=
strtolower
(
$name
);
$name
=
strtolower
(
$name
);
return
isset
(
$this
->
_e
[
$name
])
&&
$this
->
_e
[
$name
]
->
getCount
();
return
isset
(
$this
->
_e
[
$name
])
&&
$this
->
_e
[
$name
]
->
getCount
();
}
}
...
@@ -365,6 +398,7 @@ class Component extends Object
...
@@ -365,6 +398,7 @@ class Component extends Object
*/
*/
public
function
raiseEvent
(
$name
,
$event
)
public
function
raiseEvent
(
$name
,
$event
)
{
{
$this
->
ensureBehaviors
();
$name
=
strtolower
(
$name
);
$name
=
strtolower
(
$name
);
if
(
$event
instanceof
Event
)
{
if
(
$event
instanceof
Event
)
{
$event
->
name
=
$name
;
$event
->
name
=
$name
;
...
@@ -406,6 +440,7 @@ class Component extends Object
...
@@ -406,6 +440,7 @@ class Component extends Object
*/
*/
public
function
asa
(
$behavior
)
public
function
asa
(
$behavior
)
{
{
$this
->
ensureBehaviors
();
return
isset
(
$this
->
_b
[
$behavior
])
?
$this
->
_b
[
$behavior
]
:
null
;
return
isset
(
$this
->
_b
[
$behavior
])
?
$this
->
_b
[
$behavior
]
:
null
;
}
}
...
@@ -415,11 +450,11 @@ class Component extends Object
...
@@ -415,11 +450,11 @@ class Component extends Object
* configuration. After that, the behavior object will be attached to
* configuration. After that, the behavior object will be attached to
* this component by calling the [[Behavior::attach]] method.
* this component by calling the [[Behavior::attach]] method.
* @param string $name the behavior's name. It should uniquely identify this behavior.
* @param string $name the behavior's name. It should uniquely identify this behavior.
* @param
mixed
$behavior the behavior configuration. This can be one of the following:
* @param
string|array|Behavior
$behavior the behavior configuration. This can be one of the following:
*
*
* - a [[Behavior]] object
* - a [[Behavior]] object
* - a string specifying the behavior class
* - a string specifying the behavior class
* - an object configuration array that will be passed to [[\Yii::createObject]] to create the behavior object.
* - an object configuration array that will be passed to [[\Yii::createObject
()
]] to create the behavior object.
*
*
* @return Behavior the behavior object
* @return Behavior the behavior object
* @see detachBehavior
* @see detachBehavior
...
@@ -457,10 +492,12 @@ class Component extends Object
...
@@ -457,10 +492,12 @@ class Component extends Object
public
function
detachBehavior
(
$name
)
public
function
detachBehavior
(
$name
)
{
{
if
(
isset
(
$this
->
_b
[
$name
]))
{
if
(
isset
(
$this
->
_b
[
$name
]))
{
$this
->
_b
[
$name
]
->
detach
(
$this
);
$behavior
=
$this
->
_b
[
$name
];
$behavior
=
$this
->
_b
[
$name
];
unset
(
$this
->
_b
[
$name
]);
unset
(
$this
->
_b
[
$name
]);
$behavior
->
detach
(
$this
);
return
$behavior
;
return
$behavior
;
}
else
{
return
null
;
}
}
}
}
...
@@ -470,10 +507,24 @@ class Component extends Object
...
@@ -470,10 +507,24 @@ class Component extends Object
public
function
detachBehaviors
()
public
function
detachBehaviors
()
{
{
if
(
$this
->
_b
!==
null
)
{
if
(
$this
->
_b
!==
null
)
{
foreach
(
$this
->
_b
as
$name
=>
$behavior
)
{
$behaviors
=
$this
->
_b
;
$this
->
_b
=
null
;
foreach
(
$behaviors
as
$name
=>
$behavior
)
{
$this
->
detachBehavior
(
$name
);
$this
->
detachBehavior
(
$name
);
}
}
$this
->
_b
=
null
;
}
}
/**
* Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
*/
public
function
ensureBehaviors
()
{
if
(
$this
->
_b
===
null
)
{
$this
->
_b
=
array
();
foreach
(
$this
->
behaviors
()
as
$name
=>
$behavior
)
{
$this
->
attachBehavior
(
$name
,
$behavior
);
}
}
}
}
}
}
}
framework/base/Model.php
View file @
a92acf65
This diff is collapsed.
Click to expand it.
framework/db/ar/ActiveMetaData.php
0 → 100644
View file @
a92acf65
<?php
namespace
yii\db\ar
;
use
yii\db\Exception
;
/**
* ActiveMetaData represents the meta-data for an Active Record class.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ActiveMetaData
{
/**
* @var TableSchema the table schema information
*/
public
$table
;
/**
* @var array list of relations
*/
public
$relations
=
array
();
/**
* Constructor.
* @param string $modelClass the model class name
*/
public
function
__construct
(
$modelClass
)
{
$tableName
=
$modelClass
::
tableName
();
$table
=
$modelClass
::
getDbConnection
()
->
getDriver
()
->
getTableSchema
(
$tableName
);
if
(
$table
===
null
)
{
throw
new
Exception
(
"Unable to find table '
$tableName
' for ActiveRecord class '
$modelClass
'."
);
}
if
(
$table
->
primaryKey
===
null
)
{
$primaryKey
=
$modelClass
::
primaryKey
();
if
(
$primaryKey
!==
null
)
{
$table
->
fixPrimaryKey
(
$primaryKey
);
}
else
{
throw
new
Exception
(
"The table '
$tableName
' for ActiveRecord class '
$modelClass
' does not have a primary key."
);
}
}
$this
->
table
=
$table
;
foreach
(
$modelClass
::
relations
()
as
$name
=>
$config
)
{
$this
->
addRelation
(
$name
,
$config
);
}
}
/**
* Adds a relation.
*
* $config is an array with three elements:
* relation type, the related active record class and the foreign key.
*
* @throws Exception
* @param string $name $name Name of the relation.
* @param array $config $config Relation parameters.
* @return void
*/
public
function
addRelation
(
$name
,
$config
)
{
if
(
preg_match
(
'/^(\w+)\s*:\s*\\\\?([\w\\\\]+)(\[\])?$/'
,
$name
,
$matches
))
{
if
(
is_string
(
$config
))
{
$config
=
array
(
'on'
=>
$config
);
}
$relation
=
ActiveRelation
::
newInstance
(
$config
);
$relation
->
name
=
$matches
[
1
];
$relation
->
modelClass
=
'\\'
.
$matches
[
2
];
$relation
->
hasMany
=
isset
(
$matches
[
3
]);
$this
->
relations
[
$relation
->
name
]
=
$relation
;
}
else
{
throw
new
Exception
(
"Relation name in bad format:
$name
"
);
}
}
}
\ No newline at end of file
framework/db/ar/ActiveQuery.php
0 → 100644
View file @
a92acf65
<?php
/**
* ActiveQuery class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\db\ar
;
/**
* ActiveFinder.php is ...
* todo: add SQL monitor
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ActiveQuery
extends
\yii\db\dao\BaseQuery
implements
\IteratorAggregate
,
\ArrayAccess
,
\Countable
{
public
$modelClass
;
public
$with
;
public
$alias
;
public
$index
;
private
$_count
;
private
$_sql
;
private
$_countSql
;
private
$_asArray
;
private
$_records
;
public
function
all
()
{
return
$this
->
performQuery
();
}
public
function
one
()
{
$this
->
limit
=
1
;
$records
=
$this
->
performQuery
();
if
(
isset
(
$records
[
0
]))
{
$this
->
_count
=
1
;
return
$records
[
0
];
}
else
{
$this
->
_count
=
0
;
return
null
;
}
}
public
function
asArray
(
$value
=
true
)
{
$this
->
_asArray
=
$value
;
}
protected
function
performQuery
()
{
$class
=
$this
->
modelClass
;
$db
=
$class
::
getDbConnection
();
$this
->
_sql
=
$this
->
getSql
(
$db
);
$command
=
$db
->
createCommand
(
$this
->
_sql
);
$command
->
bindValues
(
$this
->
params
);
$rows
=
$command
->
queryAll
();
if
(
$this
->
_asArray
)
{
$records
=
$rows
;
}
else
{
$records
=
array
();
foreach
(
$rows
as
$row
)
{
$records
[]
=
$class
::
populateRecord
(
$row
);
}
}
$this
->
_count
=
count
(
$records
);
return
$records
;
}
public
function
with
()
{
}
//
// public function getSql($connection = null)
// {
//
// }
public
function
setSql
(
$value
)
{
$this
->
_sql
=
$value
;
}
public
function
getCountSql
()
{
}
public
function
getOneSql
()
{
}
/**
* Returns the number of items in the vector.
* @return integer the number of items in the vector
*/
public
function
getCount
()
{
if
(
$this
->
_count
!==
null
)
{
return
$this
->
_count
;
}
else
{
return
$this
->
_count
=
$this
->
performCountQuery
();
}
}
protected
function
performCountQuery
()
{
$select
=
$this
->
select
;
$this
->
select
=
'COUNT(*)'
;
$class
=
$this
->
modelClass
;
$command
=
$this
->
createCommand
(
$class
::
getDbConnection
());
$this
->
_countSql
=
$command
->
getSql
();
$count
=
$command
->
queryScalar
();
$this
->
select
=
$select
;
return
$count
;
}
/**
* Sets the parameters about query caching.
* This is a shortcut method to {@link CDbConnection::cache()}.
* It changes the query caching parameter of the {@link dbConnection} instance.
* @param integer $duration the number of seconds that query results may remain valid in cache.
* If this is 0, the caching will be disabled.
* @param CCacheDependency $dependency the dependency that will be used when saving the query results into cache.
* @param integer $queryCount number of SQL queries that need to be cached after calling this method. Defaults to 1,
* meaning that the next SQL query will be cached.
* @return ActiveRecord the active record instance itself.
*/
public
function
cache
(
$duration
,
$dependency
=
null
,
$queryCount
=
1
)
{
$this
->
connection
->
cache
(
$duration
,
$dependency
,
$queryCount
);
return
$this
;
}
/**
* Returns an iterator for traversing the items in the vector.
* This method is required by the SPL interface `IteratorAggregate`.
* It will be implicitly called when you use `foreach` to traverse the vector.
* @return Iterator an iterator for traversing the items in the vector.
*/
public
function
getIterator
()
{
$records
=
$this
->
performQuery
();
return
new
\yii\base\VectorIterator
(
$records
);
}
/**
* Returns the number of items in the vector.
* This method is required by the SPL `Countable` interface.
* It will be implicitly called when you use `count($vector)`.
* @return integer number of items in the vector.
*/
public
function
count
()
{
return
$this
->
getCount
();
}
/**
* Returns a value indicating whether there is an item at the specified offset.
* This method is required by the SPL interface `ArrayAccess`.
* It is implicitly called when you use something like `isset($vector[$offset])`.
* @param integer $offset the offset to be checked
* @return boolean whether there is an item at the specified offset.
*/
public
function
offsetExists
(
$offset
)
{
if
(
$this
->
_records
===
null
)
{
$this
->
_records
=
$this
->
performQuery
();
}
return
isset
(
$this
->
_records
[
$offset
]);
}
/**
* Returns the item at the specified offset.
* This method is required by the SPL interface `ArrayAccess`.
* It is implicitly called when you use something like `$value = $vector[$offset];`.
* This is equivalent to [[itemAt]].
* @param integer $offset the offset to retrieve item.
* @return mixed the item at the offset
* @throws Exception if the offset is out of range
*/
public
function
offsetGet
(
$offset
)
{
if
(
$this
->
_records
===
null
)
{
$this
->
_records
=
$this
->
performQuery
();
}
return
isset
(
$this
->
_records
[
$offset
])
?
$this
->
_records
[
$offset
]
:
null
;
}
/**
* Sets the item at the specified offset.
* This method is required by the SPL interface `ArrayAccess`.
* It is implicitly called when you use something like `$vector[$offset] = $item;`.
* If the offset is null or equal to the number of the existing items,
* the new item will be appended to the vector.
* Otherwise, the existing item at the offset will be replaced with the new item.
* @param integer $offset the offset to set item
* @param mixed $item the item value
* @throws Exception if the offset is out of range, or the vector is read only.
*/
public
function
offsetSet
(
$offset
,
$item
)
{
if
(
$this
->
_records
===
null
)
{
$this
->
_records
=
$this
->
performQuery
();
}
$this
->
_records
[
$offset
]
=
$item
;
}
/**
* Unsets the item at the specified offset.
* This method is required by the SPL interface `ArrayAccess`.
* It is implicitly called when you use something like `unset($vector[$offset])`.
* This is equivalent to [[removeAt]].
* @param integer $offset the offset to unset item
* @throws Exception if the offset is out of range, or the vector is read only.
*/
public
function
offsetUnset
(
$offset
)
{
if
(
$this
->
_records
===
null
)
{
$this
->
_records
=
$this
->
performQuery
();
}
unset
(
$this
->
_records
[
$offset
]);
}
}
framework/db/ar/ActiveRecord.php
View file @
a92acf65
This diff is collapsed.
Click to expand it.
framework/db/ar/ActiveRelation.php
0 → 100644
View file @
a92acf65
<?php
namespace
yii\db\ar
;
class
ActiveRelation
extends
\yii\db\dao\BaseQuery
{
public
$name
;
public
$modelClass
;
public
$hasMany
;
public
$joinType
;
public
$alias
;
public
$on
;
public
$via
;
public
$index
;
public
$with
;
public
$scopes
;
public
function
mergeWith
(
$relation
)
{
parent
::
mergeWith
(
$relation
);
if
(
$relation
->
joinType
!==
null
)
{
$this
->
joinType
=
$relation
->
joinType
;
}
if
(
$relation
->
alias
!==
null
)
{
$this
->
alias
=
$relation
->
alias
;
}
if
(
$relation
->
on
!==
null
)
{
if
(
!
empty
(
$this
->
on
))
{
$this
->
on
=
"(
{
$this
->
on
}
) AND (
{
$relation
->
on
}
)"
;
}
else
{
$this
->
on
=
$relation
->
on
;
}
}
if
(
$relation
->
via
!==
null
)
{
$this
->
via
=
$relation
->
via
;
}
if
(
$relation
->
index
!==
null
)
{
$this
->
index
=
$relation
->
index
;
}
// todo: with, scopes
}
}
framework/db/dao/BaseQuery.php
0 → 100644
View file @
a92acf65
This diff is collapsed.
Click to expand it.
framework/db/dao/Query.php
View file @
a92acf65
This diff is collapsed.
Click to expand it.
framework/util/Text.php
View file @
a92acf65
...
@@ -44,8 +44,27 @@ class Text
...
@@ -44,8 +44,27 @@ class Text
return
$name
.
's'
;
return
$name
.
's'
;
}
}
public
static
function
dd
(
$value
)
/**
* Converts a class name into space-separated words.
* For example, 'PostTag' will be converted as 'Post Tag'.
* @param string $name the string to be converted
* @param boolean $ucwords whether to capitalize the first letter in each word
* @return string the resulting words
*/
public
static
function
name2words
(
$name
,
$ucwords
=
true
)
{
$label
=
trim
(
strtolower
(
str_replace
(
array
(
'-'
,
'_'
,
'.'
),
' '
,
preg_replace
(
'/(?<![A-Z])[A-Z]/'
,
' \0'
,
$name
))));
return
$ucwords
?
ucwords
(
$label
)
:
$label
;
}
/**
* Converts a class name into a HTML ID.
* For example, 'PostTag' will be converted as 'post-tag'.
* @param string $name the string to be converted
* @return string the resulting ID
*/
public
static
function
name2id
(
$name
)
{
{
return
trim
(
strtolower
(
str_replace
(
array
(
'-'
,
'_'
,
'.'
),
' '
,
preg_replace
(
'/(?<![A-Z])[A-Z]/'
,
' \0'
,
$value
)))
);
return
trim
(
strtolower
(
str_replace
(
'_'
,
'-'
,
preg_replace
(
'/(?<![A-Z])[A-Z]/'
,
'-\0'
,
$name
))),
'-'
);
}
}
}
}
tests/unit/framework/base/BehaviorTest.php
View file @
a92acf65
...
@@ -7,6 +7,16 @@ class BarClass extends \yii\base\Component
...
@@ -7,6 +7,16 @@ class BarClass extends \yii\base\Component
}
}
class
FooClass
extends
\yii\base\Component
{
public
function
behaviors
()
{
return
array
(
'foo'
=>
__NAMESPACE__
.
'\BarBehavior'
,
);
}
}
class
BarBehavior
extends
\yii\base\Behavior
class
BarBehavior
extends
\yii\base\Behavior
{
{
public
$behaviorProperty
=
'behavior property'
;
public
$behaviorProperty
=
'behavior property'
;
...
@@ -21,12 +31,19 @@ class BehaviorTest extends \yiiunit\TestCase
...
@@ -21,12 +31,19 @@ class BehaviorTest extends \yiiunit\TestCase
{
{
public
function
testAttachAndAccessing
()
public
function
testAttachAndAccessing
()
{
{
$bar
=
BarClass
::
newInstance
();
$bar
=
new
BarClass
();
$behavior
=
new
BarBehavior
();
$behavior
=
new
BarBehavior
();
$bar
->
attachBehavior
(
'bar'
,
$behavior
);
$bar
->
attachBehavior
(
'bar'
,
$behavior
);
$this
->
assertEquals
(
'behavior property'
,
$bar
->
behaviorProperty
);
$this
->
assertEquals
(
'behavior property'
,
$bar
->
behaviorProperty
);
$this
->
assertEquals
(
'behavior method'
,
$bar
->
behaviorMethod
());
$this
->
assertEquals
(
'behavior method'
,
$bar
->
behaviorMethod
());
$this
->
assertEquals
(
'behavior property'
,
$bar
->
bar
->
behaviorProperty
);
$this
->
assertEquals
(
'behavior property'
,
$bar
->
asa
(
'bar'
)
->
behaviorProperty
);
$this
->
assertEquals
(
'behavior method'
,
$bar
->
bar
->
behaviorMethod
());
$this
->
assertEquals
(
'behavior method'
,
$bar
->
asa
(
'bar'
)
->
behaviorMethod
());
}
public
function
testAutomaticAttach
()
{
$foo
=
new
FooClass
();
$this
->
assertEquals
(
'behavior property'
,
$foo
->
behaviorProperty
);
$this
->
assertEquals
(
'behavior method'
,
$foo
->
behaviorMethod
());
}
}
}
}
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