diff --git a/Cake/ORM/Association/BelongsToMany.php b/Cake/ORM/Association/BelongsToMany.php index 241a99f7942..cce508ce70d 100644 --- a/Cake/ORM/Association/BelongsToMany.php +++ b/Cake/ORM/Association/BelongsToMany.php @@ -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(); diff --git a/Cake/ORM/Table.php b/Cake/ORM/Table.php index bbcc6e19118..b8e002988a1 100644 --- a/Cake/ORM/Table.php +++ b/Cake/ORM/Table.php @@ -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 @@ -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; } @@ -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; } @@ -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; diff --git a/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php b/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php index f725fc3ea57..e9e07b623da 100644 --- a/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php +++ b/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php @@ -43,6 +43,9 @@ 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']] @@ -50,6 +53,9 @@ public function setUp() { $this->article->schema([ 'id' => ['type' => 'integer'], 'name' => ['type' => 'string'], + '_constraints' => [ + 'primary' => ['type' => 'primary', 'columns' => ['id']] + ] ]); TableRegistry::set('Articles', $this->article); } @@ -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); diff --git a/Cake/Test/TestCase/ORM/Association/BelongsToTest.php b/Cake/Test/TestCase/ORM/Association/BelongsToTest.php index 7e8ab0a8528..6876915a323 100644 --- a/Cake/Test/TestCase/ORM/Association/BelongsToTest.php +++ b/Cake/Test/TestCase/ORM/Association/BelongsToTest.php @@ -40,6 +40,9 @@ public function setUp() { 'schema' => [ 'id' => ['type' => 'integer'], 'company_name' => ['type' => 'string'], + '_constraints' => [ + 'primary' => ['type' => 'primary', 'columns' => ['id']] + ] ] ]); $this->client = TableRegistry::get('Clients', [ @@ -47,6 +50,9 @@ public function setUp() { 'id' => ['type' => 'integer'], 'client_name' => ['type' => 'string'], 'company_id' => ['type' => 'integer'], + '_constraints' => [ + 'primary' => ['type' => 'primary', 'columns' => ['id']] + ] ] ]); } diff --git a/Cake/Test/TestCase/ORM/Association/HasManyTest.php b/Cake/Test/TestCase/ORM/Association/HasManyTest.php index 2f090f18618..1d0811219d4 100644 --- a/Cake/Test/TestCase/ORM/Association/HasManyTest.php +++ b/Cake/Test/TestCase/ORM/Association/HasManyTest.php @@ -40,6 +40,9 @@ public function setUp() { 'schema' => [ 'id' => ['type' => 'integer'], 'username' => ['type' => 'string'], + '_constraints' => [ + 'primary' => ['type' => 'primary', 'columns' => ['id']] + ] ] ]); $this->article = $this->getMock( @@ -51,6 +54,9 @@ public function setUp() { 'id' => ['type' => 'integer'], 'title' => ['type' => 'string'], 'author_id' => ['type' => 'integer'], + '_constraints' => [ + 'primary' => ['type' => 'primary', 'columns' => ['id']] + ] ]); } diff --git a/Cake/Test/TestCase/ORM/Association/HasOneTest.php b/Cake/Test/TestCase/ORM/Association/HasOneTest.php index e229915f0bb..ccedab86aaf 100644 --- a/Cake/Test/TestCase/ORM/Association/HasOneTest.php +++ b/Cake/Test/TestCase/ORM/Association/HasOneTest.php @@ -39,6 +39,9 @@ public function setUp() { 'schema' => [ 'id' => ['type' => 'integer'], 'username' => ['type' => 'string'], + '_constraints' => [ + 'primary' => ['type' => 'primary', 'columns' => ['id']] + ] ] ]); $this->profile = TableRegistry::get('Profiles', [ @@ -46,6 +49,9 @@ public function setUp() { 'id' => ['type' => 'integer'], 'first_name' => ['type' => 'string'], 'user_id' => ['type' => 'integer'], + '_constraints' => [ + 'primary' => ['type' => 'primary', 'columns' => ['id']] + ] ] ]); } diff --git a/Cake/Test/TestCase/ORM/QueryTest.php b/Cake/Test/TestCase/ORM/QueryTest.php index 5453369c065..d1c7f5c84d6 100644 --- a/Cake/Test/TestCase/ORM/QueryTest.php +++ b/Cake/Test/TestCase/ORM/QueryTest.php @@ -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]); diff --git a/Cake/Test/TestCase/ORM/TableTest.php b/Cake/Test/TestCase/ORM/TableTest.php index bea789fc34d..90a759f321d 100644 --- a/Cake/Test/TestCase/ORM/TableTest.php +++ b/Cake/Test/TestCase/ORM/TableTest.php @@ -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()); @@ -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()); @@ -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());