Skip to content
Browse files

Optimise Model::key() and fix some issues

  • Loading branch information...
1 parent 558a9f4 commit 2d25ac1dcece9d8e2bd401aa7abefc273012cda4 @jails jails committed Jul 14, 2012
View
49 data/Model.php
@@ -154,7 +154,8 @@ class Model extends \lithium\core\StaticObject {
protected static $_classes = array(
'connections' => 'lithium\data\Connections',
'query' => 'lithium\data\model\Query',
- 'validator' => 'lithium\util\Validator'
+ 'validator' => 'lithium\util\Validator',
+ 'entity' => 'lithium\data\Entity'
);
/**
@@ -649,26 +650,54 @@ public function title($entity) {
* If no values supplied, returns the name of the `Model` key. If values
* are supplied, returns the key value.
*
- * @param array $values An array of values.
+ * @param mixed $values An array of values or object with values. If `$values` is `null`,
+ * the meta `'key'` of the model is returned.
* @return mixed Key value.
*/
- public static function key($values = array()) {
+ public static function key($values = null) {
$key = static::meta('key');
- if (is_object($values) && method_exists($values, 'to')) {
- $values = $values->to('array');
- } elseif (is_object($values) && is_string($key) && isset($values->{$key})) {
- return $values->{$key};
+ if ($values === null) {
+ return $key;
}
- if (!$values) {
- return $key;
+ if (is_object($values) && is_string($key)) {
+ return static::_key($key, $values);
+ } elseif ($values instanceof static::$_classes['entity']) {
+ $values = $values->to('array');
}
+
if (!is_array($values) && !is_array($key)) {
return array($key => $values);
}
+
$key = (array) $key;
- return array_intersect_key($values, array_combine($key, $key));
+ $result = array();
+ foreach ($key as $value) {
+ if (!isset($values[$value])) {
+ return null;
+ }
+ $result[$value] = $values[$value];
+ }
+ return $result;
+ }
+
+ /**
+ * Helper for the `Model::key()` function
+ *
+ * @see lithium\data\Model::key()
+ * @param object $values Object with attributes.
+ * @param string $key The key
+ * @return mixed The key value array or `null` if the `$values` object has no attribute
+ * named `$key`
+ */
+ protected static function _key($key, $values) {
+ if (isset($values->$key)) {
+ return array($key => $values->$key);
+ } elseif (!$values instanceof static::$_classes['entity']) {
+ return array($key => $values);
+ }
+ return null;
}
/**
View
17 tests/cases/data/ModelTest.php
@@ -25,6 +25,8 @@
class ModelTest extends \lithium\test\Unit {
+ protected $_model = 'lithium\tests\mocks\data\MockModelCompositePk';
+
protected $_database = 'lithium\tests\mocks\data\MockSource';
protected $_altSchema = null;
@@ -404,6 +406,21 @@ public function testKeyGeneration() {
$key->foo = 'bar';
$this->assertEqual(array('id' => $key), MockPost::key($key));
+
+ $this->assertNull(MockPost::key(array()));
+
+ $model = $this->_model;
+ $this->assertNull($model::key(array('client_id' => 3)));
+
+ $result = $model::key(array('invoice_id' => 5, 'payment' => '100'));
+ $this->assertNull($result);
+
+ $expected = array('client_id' => 3, 'invoice_id' => 5);
+ $result = $model::key(array(
+ 'client_id' => 3,
+ 'invoice_id' => 5,
+ 'payment' => '100'));
+ $this->assertEqual($expected, $result);
}
public function testValidatesFalse() {
View
6 tests/cases/data/source/MongoDbTest.php
@@ -68,8 +68,9 @@ public function setUp() {
$model::config(array('meta' => array('key' => '_id')));
$model::$connection = $this->db;
+ $type = 'create';
- $this->query = new Query(compact('model') + array(
+ $this->query = new Query(compact('model', 'type') + array(
'entity' => new Document(compact('model'))
));
}
@@ -377,8 +378,9 @@ public function testDelete() {
$this->assertTrue($this->query->entity()->exists());
$model = $this->_model;
+ $id = new MongoId();
$this->query = new Query(compact('model') + array(
- 'entity' => new Document(compact('model'))
+ 'entity' => new Document(compact('model') + array('data' => array('_id' => $id)))
));
array_push($this->db->connection->results, true);
View
4 tests/cases/data/source/http/adapter/CouchDbTest.php
@@ -9,7 +9,6 @@
namespace lithium\tests\cases\data\source\http\adapter;
use lithium\data\model\Query;
-use lithium\data\Connections;
use lithium\data\entity\Document;
use lithium\data\source\http\adapter\CouchDb;
@@ -40,7 +39,8 @@ public function setUp() {
$model::$connection = $this->db;
$entity = new Document(compact('model'));
- $this->query = new Query(compact('model', 'entity'));
+ $type = 'create';
+ $this->query = new Query(compact('model', 'entity', 'type'));
}
public function testAllMethodsNoConnection() {

0 comments on commit 2d25ac1

Please sign in to comment.
Something went wrong with that request. Please try again.