Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Warning BC Break: `data\Model::_classes` and `data\Model_autoConfig` are no more static #746

Merged
merged 1 commit into from

3 participants

@jails
Collaborator

Unlike all other variables in data\Model_autoConfig, data\Model::_classes is the only static one. I don't really understand this inconsistency.
Moreover this specificity forces us to redefine all entries in _classes when some custom dependencies is needed.

With with PR you can write:

class MyModel extends \lithium\data\Model {
    protected $_classes = array(
        'custom' => 'my\custom\Dependency'
    );
}

Instead of:

class MyModel extends \lithium\data\Model {
    protected static $_classes = array(
        'connections' => 'lithium\data\Connections',
        'query'       => 'lithium\data\model\Query',
        'validator'   => 'lithium\util\Validator',
        'entity'      => 'lithium\data\Entity'
        'custom' => 'my/custom/Dependency'
    );
}

when you need a custom dependency.

@d1rk

What a relief. Had to deal with that several times... Thanks for that.

@nateabele
Owner

I don't see what being static has to do with whether or not $_classes is properly inherited. It's always been static, and adding dependencies without redefining the whole array definitely used to work. It was probably broken in one of the model initialization refactors.

@jails
Collaborator

Sorry, my explanation is confusing. You're right $_classes is properly inherited but only when the instance have been initialized.

e.g.:

public static function test1() {
    print_r(static::$_classes); // display only the overrided dependency
}
public static function test2() {
    static::_object();
    print_r(static::$_classes); // display all the dependencies
}

this means:

  • every time a static function use static::$_classes without a kind of static::_object(); it will fail (unless redefining the whole array).
  • you need to know the internal of the framework to correctly use this static variable.

So for me $_classes should not be static.

@nateabele
Owner

Fair enough. :-)

@nateabele nateabele merged commit 806ba12 into UnionOfRAD:dev

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 17, 2012
  1. @jails
This page is out of date. Refresh to see the latest.
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');
Something went wrong with that request. Please try again.