Skip to content
Permalink
Browse files

Making Table::primaryKey optionally return string.

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 ed5486a0505bae77e6f9f77fadae7fef41caf0ee
@@ -331,7 +331,7 @@ public function eagerLoader(array $options) {
*/
public function cascadeDelete(Entity $entity, $options = []) {
$foreignKey = (array)$this->foreignKey();
$primaryKey = $this->source()->primaryKey();
$primaryKey = (array)$this->source()->primaryKey();
$conditions = [];
if ($primaryKey) {
@@ -130,7 +130,7 @@ protected function _joinCondition(array $options) {
$tAlias = $this->target()->alias();
$sAlias = $this->_sourceTable->alias();
$foreignKey = (array)$options['foreignKey'];
$primaryKey = $this->_sourceTable->primaryKey();
$primaryKey = (array)$this->_sourceTable->primaryKey();
if (count($foreignKey) !== count($primaryKey)) {
$msg = 'Cannot match provided foreignKey, got %d columns expected %d';
@@ -161,7 +161,7 @@ protected function _resultInjector($fetchQuery, $resultMap) {
$tAlias = $this->target()->alias();
$sourceKeys = [];
foreach ($source->primaryKey() as $key) {
foreach ((array)$source->primaryKey() as $key) {
$sourceKeys[] = key($fetchQuery->aliasField($key, $sAlias));
}
@@ -142,7 +142,7 @@ protected function _joinCondition(array $options) {
$tAlias = $this->target()->alias();
$sAlias = $this->_sourceTable->alias();
$foreignKey = (array)$options['foreignKey'];
$primaryKey = $this->_sourceTable->primaryKey();
$primaryKey = (array)$this->_sourceTable->primaryKey();
if (count($foreignKey) !== count($primaryKey)) {
$msg = 'Cannot match provided foreignKey, got %d columns expected %d';
@@ -198,7 +198,7 @@ protected function _loadBelongsToMany($assoc, $ids) {
}
$filter = new TupleComparison($primaryKey, $ids, [], 'IN');
} else {
$filter = [$primaryKey[0] . 'IN' => $ids];
$filter = [$primaryKey[0] . ' IN' => $ids];
}
return $assoc->find()->where($filter)->toArray();
@@ -122,7 +122,7 @@ class Table implements EventListener {
/**
* The name of the field that represents the primary key in the table
*
* @var array
* @var string|array
*/
protected $_primaryKey;
@@ -375,14 +375,17 @@ public function hasField($field) {
* 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
* @return array
* @return string|array
*/
public function primaryKey($key = null) {
if ($key !== null) {
$this->_primaryKey = (array)$key;
$this->_primaryKey = $key;
}
if ($this->_primaryKey === null) {
$key = (array)$this->schema()->primaryKey();
if (count($key) === 1) {
$key = $key[0];
}
$this->_primaryKey = $key;
}
return $this->_primaryKey;
@@ -400,7 +403,7 @@ public function displayField($key = null) {
}
if ($this->_displayField === null) {
$schema = $this->schema();
$primary = $this->primaryKey();
$primary = (array)$this->primaryKey();
$this->_displayField = array_shift($primary);
if ($schema->column('title')) {
$this->_displayField = 'title';
@@ -25,7 +25,7 @@
/**
* Test entity for mass assignment.
*/
class OpenEntity extends Entity {
class OpenArticleEntity extends Entity {
protected $_accessible = [
'*' => true
@@ -297,7 +297,7 @@ public function testDeleteDependent() {
*/
public function testOneGenerateBelongsToManyEntitiesFromIds() {
$articles = TableRegistry::get('SiteArticles');
$articles->entityClass(__NAMESPACE__ . '\OpenEntity');
$articles->entityClass(__NAMESPACE__ . '\OpenArticleEntity');
$tags = TableRegistry::get('SiteTags');
$junction = TableRegistry::get('SiteArticlesTags');
$articles->belongsToMany('SiteTags', [
@@ -134,9 +134,9 @@ public function testPrimaryKey() {
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
]
]);
$this->assertEquals(['id'], $table->primaryKey());
$this->assertEquals('id', $table->primaryKey());
$table->primaryKey('thingID');
$this->assertEquals(['thingID'], $table->primaryKey());
$this->assertEquals('thingID', $table->primaryKey());
$table->primaryKey(['thingID', 'user_id']);
$this->assertEquals(['thingID', 'user_id'], $table->primaryKey());

0 comments on commit ed5486a

Please sign in to comment.
You can’t perform that action at this time.