mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Fixing custom hasMany associations, fixes #3918
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6512 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
0ee0324e20
commit
1a69866df1
2 changed files with 51 additions and 40 deletions
|
@ -687,7 +687,7 @@ class DboSource extends DataSource {
|
||||||
}
|
}
|
||||||
$count = count($resultSet);
|
$count = count($resultSet);
|
||||||
|
|
||||||
if ($type === 'hasMany' && (!isset($assocData['limit']) || empty($assocData['limit']))) {
|
if ($type === 'hasMany' && empty($assocData['limit']) && !empty($assocData['foreignKey'])) {
|
||||||
$ins = $fetch = array();
|
$ins = $fetch = array();
|
||||||
for ($i = 0; $i < $count; $i++) {
|
for ($i = 0; $i < $count; $i++) {
|
||||||
if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) {
|
if ($in = $this->insertQueryData('{$__cakeID__$}', $resultSet[$i], $association, $assocData, $model, $linkModel, $stack)) {
|
||||||
|
@ -1119,11 +1119,12 @@ class DboSource extends DataSource {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'hasMany':
|
case 'hasMany':
|
||||||
$assocData['fields'] = array_unique(array_merge(
|
$assocData['fields'] = $this->fields($linkModel, $alias, $assocData['fields']);
|
||||||
$this->fields($linkModel, $alias, $assocData['fields']),
|
if (!empty($assocData['foreignKey'])) {
|
||||||
$this->fields($linkModel, $alias, array("{$alias}.{$assocData['foreignKey']}"))
|
$assocData['fields'] = array_unique(array_merge(
|
||||||
));
|
$assocData['fields'], $this->fields($linkModel, $alias, array("{$alias}.{$assocData['foreignKey']}"))
|
||||||
|
));
|
||||||
|
}
|
||||||
$query = array(
|
$query = array(
|
||||||
'conditions' => $this->__mergeConditions($this->getConstraint('hasMany', $model, $linkModel, $alias, $assocData), $assocData['conditions']),
|
'conditions' => $this->__mergeConditions($this->getConstraint('hasMany', $model, $linkModel, $alias, $assocData), $assocData['conditions']),
|
||||||
'fields' => $assocData['fields'],
|
'fields' => $assocData['fields'],
|
||||||
|
|
|
@ -220,6 +220,42 @@ class ModelTest extends CakeTestCase {
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testDynamicAssociations() {
|
||||||
|
$this->loadFixtures('Article', 'Comment');
|
||||||
|
$this->model =& new Article();
|
||||||
|
|
||||||
|
$this->model->belongsTo = $this->model->hasAndBelongsToMany = $this->model->hasOne = array();
|
||||||
|
$this->model->hasMany['Comment'] = am($this->model->hasMany['Comment'], array(
|
||||||
|
'foreignKey' => false,
|
||||||
|
'conditions' => array('Comment.user_id' => '= 2')
|
||||||
|
));
|
||||||
|
$result = $this->model->find('all');
|
||||||
|
$expected = array(
|
||||||
|
array(
|
||||||
|
'Article' => array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
|
||||||
|
'Comment' => array(
|
||||||
|
array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
|
||||||
|
array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31')
|
||||||
|
)
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'Article' => array('id' => '2', 'user_id' => '3', 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
|
||||||
|
'Comment' => array(
|
||||||
|
array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
|
||||||
|
array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31')
|
||||||
|
)
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
|
||||||
|
'Comment' => array(
|
||||||
|
array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
|
||||||
|
array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31')
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$this->assertEqual($result, $expected);
|
||||||
|
}
|
||||||
|
|
||||||
function testSaveMultipleHabtm() {
|
function testSaveMultipleHabtm() {
|
||||||
$this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
|
$this->loadFixtures('JoinA', 'JoinB', 'JoinC', 'JoinAB', 'JoinAC');
|
||||||
$this->model = new JoinA();
|
$this->model = new JoinA();
|
||||||
|
@ -1760,18 +1796,9 @@ class ModelTest extends CakeTestCase {
|
||||||
|
|
||||||
// Parent data after HABTM data
|
// Parent data after HABTM data
|
||||||
|
|
||||||
$data = array(
|
$data = array('Tag' => array('Tag' => array(1, 2)), 'Article' => array('id' => '2', 'title' => 'New Second Article'));
|
||||||
'Tag' => array(
|
$this->assertTrue($this->model->set($data));
|
||||||
'Tag' => array( 1, 2 )
|
$this->assertTrue($this->model->save());
|
||||||
),
|
|
||||||
'Article' => array('id' => '2', 'title' => 'New Second Article' ),
|
|
||||||
);
|
|
||||||
|
|
||||||
$result = $this->model->set($data);
|
|
||||||
$this->assertTrue($result);
|
|
||||||
|
|
||||||
$result = $this->model->save();
|
|
||||||
$this->assertTrue($result);
|
|
||||||
|
|
||||||
$this->model->unbindModel(array(
|
$this->model->unbindModel(array(
|
||||||
'belongsTo' => array('User'),
|
'belongsTo' => array('User'),
|
||||||
|
@ -1789,18 +1816,10 @@ class ModelTest extends CakeTestCase {
|
||||||
);
|
);
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
$data = array(
|
$data = array('Tag' => array('Tag' => array(1, 2)), 'Article' => array('id' => '2', 'title' => 'New Second Article Title'));
|
||||||
'Tag' => array(
|
|
||||||
'Tag' => array( 1, 2 )
|
|
||||||
),
|
|
||||||
'Article' => array('id' => '2', 'title' => 'New Second Article Title' ),
|
|
||||||
);
|
|
||||||
|
|
||||||
$result = $this->model->set($data);
|
$result = $this->model->set($data);
|
||||||
$this->assertTrue($result);
|
$this->assertTrue($result);
|
||||||
|
$this->assertTrue($this->model->save());
|
||||||
$result = $this->model->save();
|
|
||||||
$this->assertTrue($result);
|
|
||||||
|
|
||||||
$this->model->unbindModel(array(
|
$this->model->unbindModel(array(
|
||||||
'belongsTo' => array('User'),
|
'belongsTo' => array('User'),
|
||||||
|
@ -1818,18 +1837,9 @@ class ModelTest extends CakeTestCase {
|
||||||
);
|
);
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
|
|
||||||
$data = array(
|
$data = array('Tag' => array('Tag' => array(2, 3)), 'Article' => array('id' => '2', 'title' => 'Changed Second Article'));
|
||||||
'Tag' => array(
|
$this->assertTrue($this->model->set($data));
|
||||||
'Tag' => array( 2, 3 )
|
$this->assertTrue($this->model->save());
|
||||||
),
|
|
||||||
'Article' => array('id' => '2', 'title' => 'Changed Second Article' ),
|
|
||||||
);
|
|
||||||
|
|
||||||
$result = $this->model->set($data);
|
|
||||||
$this->assertTrue($result);
|
|
||||||
|
|
||||||
$result = $this->model->save();
|
|
||||||
$this->assertTrue($result);
|
|
||||||
|
|
||||||
$this->model->unbindModel(array(
|
$this->model->unbindModel(array(
|
||||||
'belongsTo' => array('User'),
|
'belongsTo' => array('User'),
|
||||||
|
|
Loading…
Add table
Reference in a new issue