asserting that tree behavior generates valid sql in recover #955

Merged
merged 3 commits into from Nov 15, 2012

4 participants

@dereuromark
CakePHP member

$Model->alias . '.' . $Model->primaryKey should better be used to avoid invalid sql statements

see http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3366-treebehavior-recover-invalid-sql

@ceeram
CakePHP member

Looks ok to me

@ADmad
CakePHP member

We might want to consider the option of calling deleteAll() with $cascade and $callbacks params false to avoid the joins.

@renan renan commented on an outdated diff Nov 13, 2012
lib/Cake/Model/Behavior/TreeBehavior.php
foreach ($missingParents as $id => $display) {
$this->errors[] = 'cannot find the parent for ' . $Model->alias . ' with id ' . $id . '(' . $display . ')';
}
return false;
- } elseif ($missingParentAction == 'delete') {
- $Model->deleteAll(array($Model->primaryKey => array_flip($missingParents)));
+ } elseif ($missingParentAction === 'delete') {
+ $Model->deleteAll(array($Model->alias . '.' . $Model->primaryKey => array_flip($missingParents)));
@renan
CakePHP member
renan added a note Nov 13, 2012

Other places on the code uses $Model->escapeField() instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@markstory markstory commented on the diff Nov 14, 2012
...e/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php
$this->Tree->create();
- $this->Tree->save(array('parent_id' => null, 'name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0));
+ $this->Tree->save(array('name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0));
@markstory
CakePHP member

Why did all these tests change?

@ADmad
CakePHP member
ADmad added a note Nov 14, 2012

The var $parentField contains the parent field name so using the string 'parent_id' was redundant / incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@markstory markstory and 1 other commented on an outdated diff Nov 14, 2012
...e/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php
@@ -225,6 +225,74 @@ public function testRecoverUsingParentMode() {
}
/**
+ * testRecoverUsingParentModeAndDelete method
+ *
+ * @return void
+ */
+ public function testRecoverUsingParentModeAndDelete() {
+ extract($this->settings);
+ $this->Tree = new $modelClass();
+ $this->Tree->Behaviors->disable('Tree');
+
+ $this->Tree->save(array('name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0));
+ $node1 = $this->Tree->id;
+
+ $this->Tree->create();
+ $this->Tree->save(array('name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0));
+ $node11 = $this->Tree->id;
@markstory
CakePHP member

Style checker will complain about two spaces before =.

@dereuromark
CakePHP member

darn - the original test file already contained those. it is a tab afaik. I thought I fixed em all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@markstory
CakePHP member

Ok, this looks good to me.

@markstory markstory merged commit 7206254 into cakephp:master Nov 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment