Skip to content

Commit

Permalink
Fixing habtm with uuid join tables with an auto model. Tests & model …
Browse files Browse the repository at this point in the history
…classes added. model test runs slightly more smoothly on postgres as well. Fixes #5940

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7966 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
markstory committed Dec 30, 2008
1 parent afe9970 commit 94b9bca
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
21 changes: 13 additions & 8 deletions cake/libs/model/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1282,14 +1282,26 @@ function __saveMulti($joined, $id) {
|| ($this->{$join}->_schema[$this->{$join}->primaryKey]['type'] === 'binary' && $this->{$join}->_schema[$this->{$join}->primaryKey]['length'] === 16));

$newData = $newValues = array();
$primaryAdded = false;

$fields = array(
$db->name($this->hasAndBelongsToMany[$assoc]['foreignKey']),
$db->name($this->hasAndBelongsToMany[$assoc]['associationForeignKey'])
);

$idField = $db->name($this->{$join}->primaryKey);
if ($isUUID && !in_array($idField, $fields)) {
$fields[] = $idField;
$primaryAdded = true;
}

foreach ((array)$data as $row) {
if ((is_string($row) && (strlen($row) == 36 || strlen($row) == 16)) || is_numeric($row)) {
$values = array(
$db->value($id, $this->getColumnType($this->primaryKey)),
$db->value($row)
);
if ($isUUID) {
if ($isUUID && $primaryAdded) {
$values[] = $db->value(String::uuid());
}
$values = join(',', $values);
Expand Down Expand Up @@ -1318,13 +1330,6 @@ function __saveMulti($joined, $id) {
}

if (!empty($newValues)) {
$fields = array(
$db->name($this->hasAndBelongsToMany[$assoc]['foreignKey']),
$db->name($this->hasAndBelongsToMany[$assoc]['associationForeignKey'])
);
if ($isUUID) {
$fields[] = $db->name($this->{$join}->primaryKey);
}
$fields = join(',', $fields);
$db->insertMulti($this->{$join}, $fields, $newValues);
}
Expand Down
48 changes: 46 additions & 2 deletions cake/tests/cases/libs/model/model.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ function testHabtmUuidWithUuidId() {
$id = $TestModel->id;
$result = $TestModel->read(null, $id);
$this->assertEqual(1, count($result['Uuiditem']));
$this->assertEqual(strlen($result['Uuiditem'][0]['UuiditemsUuidportfolio']['id']), 36);
}
/**
* test HABTM saving when join table has no primary key and only 2 columns.
Expand All @@ -334,6 +335,29 @@ function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() {
);
$this->assertTrue($Fruit->save($data));
}
/**
* test HABTM saving when join table has no primary key and only 2 columns, no with model is used.
*
* @return void
**/
function testHabtmSavingWithNoPrimaryKeyUuidJoinTableNoWith() {
$this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag');
$Fruit =& new FruitNoWith();
$data = array(
'Fruit' => array(
'color' => 'Red',
'shape' => 'Heart-shaped',
'taste' => 'sweet',
'name' => 'Strawberry',
),
'UuidTag' => array(
'UuidTag' => array(
'481fc6d0-b920-43e0-e50f-6d1740cf8569'
)
)
);
$this->assertTrue($Fruit->save($data));
}
/**
* testHabtmUuidWithNumericId method
*
Expand All @@ -344,7 +368,7 @@ function testHabtmUuidWithNumericId() {
$this->loadFixtures('Uuidportfolio', 'Uuiditem', 'UuiditemsUuidportfolioNumericid');
$TestModel =& new Uuiditem();

$data = array('Uuiditem' => array('name' => 'Item 7'));
$data = array('Uuiditem' => array('name' => 'Item 7', 'published' => 0));
$data['Uuidportfolio']['Uuidportfolio'] = array('480af662-eb8c-47d3-886b-230540cf8569');
$TestModel->create($data);
$TestModel->save();
Expand Down Expand Up @@ -1765,8 +1789,19 @@ function testFindCount() {

$result = $TestModel->find('count', array('fields' => 'DISTINCT Project.name'));
$this->assertEqual($result, 4);

}
/**
* Test find(count) with Db::expression
*
* @return void
**/
function testFindCountWithDbExpressions() {
if ($this->skipif($this->db->config['driver'] == 'postgres', 'testFindCountWithExpressions is not compatible with Postgres')) {
return;
}
$this->loadFixtures('Project');
$db = ConnectionManager::getDataSource('test_suite');
$TestModel =& new Project();

$result = $TestModel->find('count', array('conditions' => array(
$db->expression('Project.name = \'Project 3\'')
Expand Down Expand Up @@ -6009,10 +6044,19 @@ function testUnderscoreFieldSave() {
/**
* testGroupBy method
*
* These tests will never pass with Postgres or Oracle as all fields in a select must be
* part of an aggregate function or in the GROUP BY statement.
*
* @access public
* @return void
*/
function testGroupBy() {
$db = ConnectionManager::getDataSource('test_suite');
$isStrictGroupBy = in_array($db->config['driver'], array('postgres', 'oracle'));
if ($this->skipif($isStrictGroupBy, 'Postgresql and Oracle have strict GROUP BY and are incompatible with this test.')) {
return;
}

$this->loadFixtures('Project', 'Product', 'Thread', 'Message', 'Bid');
$Thread =& new Thread();
$Product =& new Product();
Expand Down
26 changes: 26 additions & 0 deletions cake/tests/cases/libs/model/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -2929,4 +2929,30 @@ class UuidTag extends CakeTestModel {
)
);
}

class FruitNoWith extends CakeTestModel {
var $name = 'Fruit';
var $useTable = 'fruits';
var $hasAndBelongsToMany = array(
'UuidTag' => array(
'className' => 'UuidTagNoWith',
'joinTable' => 'fruits_uuid_tags',
'foreignKey' => 'fruit_id',
'associationForeignKey' => 'uuid_tag_id',
)
);
}
class UuidTagNoWith extends CakeTestModel {
var $name = 'UuidTag';
var $useTable = 'uuid_tags';
var $hasAndBelongsToMany = array(
'Fruit' => array(
'className' => 'FruitNoWith',
'joinTable' => 'fruits_uuid_tags',
'foreign_key' => 'uuid_tag_id',
'associationForeignKey' => 'fruit_id',
)
);
}

?>

0 comments on commit 94b9bca

Please sign in to comment.