Skip to content

Commit

Permalink
Making Table::primaryKey optionally return string.
Browse files Browse the repository at this point in the history
Now that all tests passes with it returning array, changing this back so
it returns a single string in cases the is only one key in the array.
This is so becuase otherwise we would make most people life misserable
when interacting with this method and having a database without
composite keys.
  • Loading branch information
lorenzo committed Jan 15, 2014
1 parent 6c98383 commit ed5486a
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cake/ORM/Association/BelongsToMany.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public function eagerLoader(array $options) {
*/ */
public function cascadeDelete(Entity $entity, $options = []) { public function cascadeDelete(Entity $entity, $options = []) {
$foreignKey = (array)$this->foreignKey(); $foreignKey = (array)$this->foreignKey();
$primaryKey = $this->source()->primaryKey(); $primaryKey = (array)$this->source()->primaryKey();
$conditions = []; $conditions = [];


if ($primaryKey) { if ($primaryKey) {
Expand Down
4 changes: 2 additions & 2 deletions Cake/ORM/Association/ExternalAssociationTrait.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected function _joinCondition(array $options) {
$tAlias = $this->target()->alias(); $tAlias = $this->target()->alias();
$sAlias = $this->_sourceTable->alias(); $sAlias = $this->_sourceTable->alias();
$foreignKey = (array)$options['foreignKey']; $foreignKey = (array)$options['foreignKey'];
$primaryKey = $this->_sourceTable->primaryKey(); $primaryKey = (array)$this->_sourceTable->primaryKey();


if (count($foreignKey) !== count($primaryKey)) { if (count($foreignKey) !== count($primaryKey)) {
$msg = 'Cannot match provided foreignKey, got %d columns expected %d'; $msg = 'Cannot match provided foreignKey, got %d columns expected %d';
Expand Down Expand Up @@ -161,7 +161,7 @@ protected function _resultInjector($fetchQuery, $resultMap) {
$tAlias = $this->target()->alias(); $tAlias = $this->target()->alias();


$sourceKeys = []; $sourceKeys = [];
foreach ($source->primaryKey() as $key) { foreach ((array)$source->primaryKey() as $key) {
$sourceKeys[] = key($fetchQuery->aliasField($key, $sAlias)); $sourceKeys[] = key($fetchQuery->aliasField($key, $sAlias));
} }


Expand Down
2 changes: 1 addition & 1 deletion Cake/ORM/Association/HasOne.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected function _joinCondition(array $options) {
$tAlias = $this->target()->alias(); $tAlias = $this->target()->alias();
$sAlias = $this->_sourceTable->alias(); $sAlias = $this->_sourceTable->alias();
$foreignKey = (array)$options['foreignKey']; $foreignKey = (array)$options['foreignKey'];
$primaryKey = $this->_sourceTable->primaryKey(); $primaryKey = (array)$this->_sourceTable->primaryKey();


if (count($foreignKey) !== count($primaryKey)) { if (count($foreignKey) !== count($primaryKey)) {
$msg = 'Cannot match provided foreignKey, got %d columns expected %d'; $msg = 'Cannot match provided foreignKey, got %d columns expected %d';
Expand Down
2 changes: 1 addition & 1 deletion Cake/ORM/Marshaller.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ protected function _loadBelongsToMany($assoc, $ids) {
} }
$filter = new TupleComparison($primaryKey, $ids, [], 'IN'); $filter = new TupleComparison($primaryKey, $ids, [], 'IN');
} else { } else {
$filter = [$primaryKey[0] . 'IN' => $ids]; $filter = [$primaryKey[0] . ' IN' => $ids];
} }


return $assoc->find()->where($filter)->toArray(); return $assoc->find()->where($filter)->toArray();
Expand Down
11 changes: 7 additions & 4 deletions Cake/ORM/Table.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Table implements EventListener {
/** /**
* The name of the field that represents the primary key in the table * The name of the field that represents the primary key in the table
* *
* @var array * @var string|array
*/ */
protected $_primaryKey; protected $_primaryKey;


Expand Down Expand Up @@ -375,14 +375,17 @@ public function hasField($field) {
* Returns the primary key field name or sets a new one * Returns the primary key field name or sets a new one
* *
* @param string|array $key sets a new name to be used as primary key * @param string|array $key sets a new name to be used as primary key
* @return array * @return string|array
*/ */
public function primaryKey($key = null) { public function primaryKey($key = null) {
if ($key !== null) { if ($key !== null) {
$this->_primaryKey = (array)$key; $this->_primaryKey = $key;
} }
if ($this->_primaryKey === null) { if ($this->_primaryKey === null) {
$key = (array)$this->schema()->primaryKey(); $key = (array)$this->schema()->primaryKey();
if (count($key) === 1) {
$key = $key[0];
}
$this->_primaryKey = $key; $this->_primaryKey = $key;
} }
return $this->_primaryKey; return $this->_primaryKey;
Expand All @@ -400,7 +403,7 @@ public function displayField($key = null) {
} }
if ($this->_displayField === null) { if ($this->_displayField === null) {
$schema = $this->schema(); $schema = $this->schema();
$primary = $this->primaryKey(); $primary = (array)$this->primaryKey();
$this->_displayField = array_shift($primary); $this->_displayField = array_shift($primary);
if ($schema->column('title')) { if ($schema->column('title')) {
$this->_displayField = 'title'; $this->_displayField = 'title';
Expand Down
4 changes: 2 additions & 2 deletions Test/TestCase/ORM/CompositeKeysTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/** /**
* Test entity for mass assignment. * Test entity for mass assignment.
*/ */
class OpenEntity extends Entity { class OpenArticleEntity extends Entity {


protected $_accessible = [ protected $_accessible = [
'*' => true '*' => true
Expand Down Expand Up @@ -297,7 +297,7 @@ public function testDeleteDependent() {
*/ */
public function testOneGenerateBelongsToManyEntitiesFromIds() { public function testOneGenerateBelongsToManyEntitiesFromIds() {
$articles = TableRegistry::get('SiteArticles'); $articles = TableRegistry::get('SiteArticles');
$articles->entityClass(__NAMESPACE__ . '\OpenEntity'); $articles->entityClass(__NAMESPACE__ . '\OpenArticleEntity');
$tags = TableRegistry::get('SiteTags'); $tags = TableRegistry::get('SiteTags');
$junction = TableRegistry::get('SiteArticlesTags'); $junction = TableRegistry::get('SiteArticlesTags');
$articles->belongsToMany('SiteTags', [ $articles->belongsToMany('SiteTags', [
Expand Down
4 changes: 2 additions & 2 deletions Test/TestCase/ORM/TableTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ public function testPrimaryKey() {
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]] '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
] ]
]); ]);
$this->assertEquals(['id'], $table->primaryKey()); $this->assertEquals('id', $table->primaryKey());
$table->primaryKey('thingID'); $table->primaryKey('thingID');
$this->assertEquals(['thingID'], $table->primaryKey()); $this->assertEquals('thingID', $table->primaryKey());


$table->primaryKey(['thingID', 'user_id']); $table->primaryKey(['thingID', 'user_id']);
$this->assertEquals(['thingID', 'user_id'], $table->primaryKey()); $this->assertEquals(['thingID', 'user_id'], $table->primaryKey());
Expand Down

0 comments on commit ed5486a

Please sign in to comment.