Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Warning BC Break: `data\Model::_classes` and `data\Model_autoConfig` …

…are no more static.
  • Loading branch information...
commit 4e2ecd2b01fc0956bb053518d06104fbe31ae575 1 parent 62b8b7a
@jails jails authored
Showing with 33 additions and 14 deletions.
  1. +33 −12 data/Model.php
  2. +0 −2  tests/mocks/data/MockPost.php
View
45 data/Model.php
@@ -8,6 +8,7 @@
namespace lithium\data;
+use lithium\core\Libraries;
use lithium\util\Set;
use lithium\util\Inflector;
use lithium\core\ConfigException;
@@ -151,7 +152,7 @@ class Model extends \lithium\core\StaticObject {
*
* @var array
*/
- protected static $_classes = array(
+ protected $_classes = array(
'connections' => 'lithium\data\Connections',
'query' => 'lithium\data\model\Query',
'validator' => 'lithium\util\Validator',
@@ -324,7 +325,7 @@ class Model extends \lithium\core\StaticObject {
* @see lithium\data\Model::config()
* @var array
*/
- protected static $_autoConfig = array(
+ protected $_autoConfig = array(
'meta',
'finders',
'query',
@@ -355,7 +356,7 @@ public static function config(array $config = array()) {
}
$self = static::$_instances[$class];
- foreach (static::$_autoConfig as $key) {
+ foreach ($self->_autoConfig as $key) {
if (isset($config[$key])) {
$_key = "_{$key}";
$val = $config[$key];
@@ -388,13 +389,13 @@ protected static function _initialize($class) {
$meta = array();
$schema = array();
$source = array();
- $classes = static::$_classes;
+ $classes = $self->_classes;
$initializers = array();
foreach (static::_parents() as $parent) {
$parentConfig = get_class_vars($parent);
- foreach (static::$_autoConfig as $key) {
+ foreach ($self->_autoConfig as $key) {
if (isset($parentConfig["_{$key}"])) {
$val = $parentConfig["_{$key}"];
${$key} = is_array($val) ? ${$key} + $val : $val;
@@ -412,7 +413,7 @@ protected static function _initialize($class) {
$conn = $classes['connections']::get($tmp['connection']);
$source = (($conn) ? $conn->configureClass($class) : array()) + $source;
}
- static::$_classes = $classes;
+ $self->_classes = $classes;
$local = compact('class') + $self->_meta;
$self->_meta = ($local + $source['meta'] + $meta);
@@ -446,6 +447,23 @@ protected static function _initialize($class) {
}
/**
+ * Returns an instance of a class with given `config`. The `name` could be a key from the
+ * `classes` array, a fully-namespaced class name, or an object. Typically this method is used
+ * in `_init` to create the dependencies used in the current class.
+ *
+ * @param string|object $name A `classes` alias or fully-namespaced class name.
+ * @param array $options The configuration passed to the constructor.
+ * @return object
+ */
+ protected static function _instance($name, array $options = array()) {
+ $self = static::_object();
+ if (is_string($name) && isset($self->_classes[$name])) {
+ $name = $self->_classes[$name];
+ }
+ return Libraries::instance(null, $name, $options);
+ }
+
+ /**
* Allows the use of syntactic-sugar like `Model::all()` instead of `Model::find('all')`.
*
* @see lithium\data\Model::find()
@@ -664,9 +682,11 @@ public static function key($values = null) {
return $key;
}
+ $self = static::_object();
+ $entity = $self->_classes['entity'];
if (is_object($values) && is_string($key)) {
- return static::_key($key, $values);
- } elseif ($values instanceof static::$_classes['entity']) {
+ return static::_key($key, $values, $entity);
+ } elseif ($values instanceof $entity) {
$values = $values->to('array');
}
@@ -691,13 +711,14 @@ public static function key($values = null) {
* @see lithium\data\Model::key()
* @param string $key The key
* @param object $values Object with attributes.
+ * @param string $entity The fully-namespaced entity class name.
* @return mixed The key value array or `null` if the `$values` object has no attribute
* named `$key`
*/
- protected static function _key($key, $values) {
+ protected static function _key($key, $values, $entity) {
if (isset($values->$key)) {
return array($key => $values->$key);
- } elseif (!$values instanceof static::$_classes['entity']) {
+ } elseif (!$values instanceof $entity) {
return array($key => $values);
}
return null;
@@ -1080,7 +1101,7 @@ public function validates($entity, array $options = array()) {
);
$options += $defaults;
$self = static::_object();
- $validator = static::$_classes['validator'];
+ $validator = $self->_classes['validator'];
$params = compact('entity', 'options');
$filter = function($parent, $params) use (&$self, $validator) {
@@ -1179,7 +1200,7 @@ public static function remove($conditions = array(), array $options = array()) {
*/
public static function &connection() {
$self = static::_object();
- $connections = static::$_classes['connections'];
+ $connections = $self->_classes['connections'];
$name = isset($self->_meta['connection']) ? $self->_meta['connection'] : null;
if ($conn = $connections::get($name)) {
View
2  tests/mocks/data/MockPost.php
@@ -8,8 +8,6 @@
namespace lithium\tests\mocks\data;
-use lithium\data\Schema;
-
class MockPost extends \lithium\tests\mocks\data\MockBase {
public $hasMany = array('MockComment');
Please sign in to comment.
Something went wrong with that request. Please try again.