Skip to content

Commit

Permalink
Reading primary key from schema and a little work towards supporting
Browse files Browse the repository at this point in the history
tables with no primary key
  • Loading branch information
lorenzo committed Nov 28, 2013
1 parent 86a1fa8 commit cfe3752
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 15 deletions.
12 changes: 7 additions & 5 deletions Cake/ORM/Association/BelongsToMany.php
Expand Up @@ -226,12 +226,14 @@ public function eagerLoader(array $options) {
* @return boolean Success.
*/
public function cascadeDelete(Entity $entity, $options = []) {
$foreignKey = $this->foreignKey();
$foreignKey = (array)$this->foreignKey();
$primaryKey = $this->source()->primaryKey();
$conditions = [
$foreignKey => $entity->get($primaryKey)
];
// TODO fix multi-column primary keys.
$conditions = [];

if ($primaryKey) {
$conditions = array_combine($foreignKey, $entity->extract((array)$primaryKey));
}

$conditions = array_merge($conditions, $this->conditions());

$table = $this->pivot();
Expand Down
23 changes: 20 additions & 3 deletions Cake/ORM/Table.php
Expand Up @@ -95,7 +95,7 @@ class Table {
*
* @var string
*/
protected $_primaryKey = 'id';
protected $_primaryKey;

/**
* The name of the field that represents a human readable representation of a row
Expand Down Expand Up @@ -303,9 +303,22 @@ public function schema($schema = null) {
}
return $this->_schema;
}

if (is_array($schema)) {
$constraints = [];

if (isset($schema['_constraints'])) {
$constraints = $schema['_constraints'];
unset($schema['_constraints']);
}

$schema = new Schema($this->table(), $schema);

foreach ($constraints as $name => $value) {
$schema->addConstraint($name, $value);
}
}

return $this->_schema = $schema;
}

Expand Down Expand Up @@ -333,6 +346,10 @@ public function primaryKey($key = null) {
if ($key !== null) {
$this->_primaryKey = $key;
}
if ($this->_primaryKey === null) {
$key = current((array)$this->schema()->primaryKey());
$this->_primaryKey = $key ?: null;
}
return $this->_primaryKey;
}

Expand Down Expand Up @@ -1051,10 +1068,10 @@ protected function _insert($entity, $data) {

$success = false;
if ($statement->rowCount() > 0) {
if (!isset($data[$primary])) {
if ($primary && !isset($data[$primary])) {
$id = $statement->lastInsertId($this->table(), $primary);
}
if ($id !== null) {
if ($primary && $id !== null) {
$entity->set($primary, $id);
}
$success = $entity;
Expand Down
11 changes: 10 additions & 1 deletion Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -43,13 +43,19 @@ public function setUp() {
$this->tag->schema([
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
]);
$this->article = $this->getMock(
'Cake\ORM\Table', ['find', 'delete'], [['alias' => 'Articles', 'table' => 'articles']]
);
$this->article->schema([
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
]);
TableRegistry::set('Articles', $this->article);
}
Expand Down Expand Up @@ -171,7 +177,10 @@ public function testAttachTo() {
'table' => 'articles_tags',
'schema' => [
'article_id' => ['type' => 'integer'],
'tag_id' => ['type' => 'integer']
'tag_id' => ['type' => 'integer'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['article_id', 'tag_id']]
]
]
]);
$association = new BelongsToMany('Tags', $config);
Expand Down
6 changes: 6 additions & 0 deletions Cake/Test/TestCase/ORM/Association/BelongsToTest.php
Expand Up @@ -40,13 +40,19 @@ public function setUp() {
'schema' => [
'id' => ['type' => 'integer'],
'company_name' => ['type' => 'string'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
]
]);
$this->client = TableRegistry::get('Clients', [
'schema' => [
'id' => ['type' => 'integer'],
'client_name' => ['type' => 'string'],
'company_id' => ['type' => 'integer'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
]
]);
}
Expand Down
6 changes: 6 additions & 0 deletions Cake/Test/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -40,6 +40,9 @@ public function setUp() {
'schema' => [
'id' => ['type' => 'integer'],
'username' => ['type' => 'string'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
]
]);
$this->article = $this->getMock(
Expand All @@ -51,6 +54,9 @@ public function setUp() {
'id' => ['type' => 'integer'],
'title' => ['type' => 'string'],
'author_id' => ['type' => 'integer'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
]);
}

Expand Down
6 changes: 6 additions & 0 deletions Cake/Test/TestCase/ORM/Association/HasOneTest.php
Expand Up @@ -39,13 +39,19 @@ public function setUp() {
'schema' => [
'id' => ['type' => 'integer'],
'username' => ['type' => 'string'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
]
]);
$this->profile = TableRegistry::get('Profiles', [
'schema' => [
'id' => ['type' => 'integer'],
'first_name' => ['type' => 'string'],
'user_id' => ['type' => 'integer'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
]
]);
}
Expand Down
17 changes: 14 additions & 3 deletions Cake/Test/TestCase/ORM/QueryTest.php
Expand Up @@ -49,16 +49,27 @@ class QueryTest extends TestCase {
public function setUp() {
parent::setUp();
$this->connection = ConnectionManager::get('test');
$schema = ['id' => ['type' => 'integer']];
$schema = [
'id' => ['type' => 'integer'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
];
$schema1 = [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
'phone' => ['type' => 'string']
'phone' => ['type' => 'string'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
];
$schema2 = [
'id' => ['type' => 'integer'],
'total' => ['type' => 'string'],
'placed' => ['type' => 'datetime']
'placed' => ['type' => 'datetime'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
];

$this->table = $table = TableRegistry::get('foo', ['schema' => $schema]);
Expand Down
14 changes: 11 additions & 3 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -128,7 +128,13 @@ public function testConnection() {
* @return void
*/
public function testPrimaryKey() {
$table = new Table(['table' => 'users']);
$table = new Table([
'table' => 'users',
'schema' => [
'id' => ['type' => 'integer'],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
]
]);
$this->assertEquals('id', $table->primaryKey());
$table->primaryKey('thingID');
$this->assertEquals('thingID', $table->primaryKey());
Expand Down Expand Up @@ -176,7 +182,8 @@ public function testDisplayFallback() {
'table' => 'users',
'schema' => [
'id' => ['type' => 'string'],
'foo' => ['type' => 'string']
'foo' => ['type' => 'string'],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
]
]);
$this->assertEquals('id', $table->displayField());
Expand All @@ -192,7 +199,8 @@ public function testDisplaySet() {
'table' => 'users',
'schema' => [
'id' => ['type' => 'string'],
'foo' => ['type' => 'string']
'foo' => ['type' => 'string'],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
]
]);
$this->assertEquals('id', $table->displayField());
Expand Down

0 comments on commit cfe3752

Please sign in to comment.