Skip to content
Permalink
Browse files

Extracting the primaryKey reading code into a new function to prevent…

… errors
  • Loading branch information...
lorenzo committed Jul 12, 2014
1 parent 48e457e commit 4492cdcc3778958d243b896624572944e9a4489e
Showing with 17 additions and 7 deletions.
  1. +17 −7 src/Model/Behavior/TreeBehavior.php
@@ -41,6 +41,8 @@ class TreeBehavior extends Behavior {
*/
protected $_table;
protected $_primaryKey;
/**
* Default config
*
@@ -92,7 +94,7 @@ public function beforeSave(Event $event, Entity $entity) {
$isNew = $entity->isNew();
$config = $this->config();
$parent = $entity->get($config['parent']);
$primaryKey = (array)$this->_table->primaryKey();
$primaryKey = $this->_getPrimaryKey();
$dirty = $entity->dirty($config['parent']);
if ($isNew && $parent) {
@@ -169,7 +171,7 @@ protected function _setParent($entity, $parent) {
throw new \RuntimeException(sprintf(
'Cannot use node "%s" as parent for entity "%s"',
$parent,
$entity->get($this->_table->primaryKey())
$entity->get($this->_getPrimaryKey())
));
}
@@ -296,7 +298,7 @@ public function childCount(Entity $node, $direct = false) {
if ($direct) {
return $this->_scope($this->_table->find())
->where([$parent => $node->get($this->_table->primaryKey())])
->where([$parent => $node->get($this->_getPrimaryKey())])
->count();
}
@@ -368,7 +370,7 @@ public function findTreeList(Query $query, array $options) {
->find('threaded', ['parentField' => $this->config()['parent']])
->formatResults(function($results) use ($options) {
$options += [
'keyPath' => $this->_table->primaryKey(),
'keyPath' => $this->_getPrimaryKey(),
'valuePath' => $this->_table->displayField(),
'spacer' => '_'
];
@@ -415,7 +417,7 @@ protected function _removeFromTree($node) {
return $this->_table->save($node);
}
$primary = $this->_table->primaryKey();
$primary = $this->_getPrimaryKey();
$this->_table->updateAll(
[$config['parent'] => $parent],
[$config['parent'] => $node->get($primary)]
@@ -599,7 +601,7 @@ protected function _moveDown($node, $number) {
protected function _getNode($id) {
$config = $this->config();
list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
$primaryKey = $this->_table->primaryKey();
$primaryKey = $this->_getPrimaryKey();
$node = $this->_scope($this->_table->find())
->select([$parent, $left, $right])
@@ -746,12 +748,20 @@ protected function _ensureFields($entity) {
return;
}
$fresh = $this->_table->get($entity->get($this->_table->primaryKey()), $fields);
$fresh = $this->_table->get($entity->get($this->_getPrimaryKey()), $fields);
$entity->set($fresh->extract($fields), ['guard' => false]);
foreach ($fields as $field) {
$entity->dirty($field, false);
}
}
protected function _getPrimaryKey() {
if (!$this->_primaryKey) {
$this->_primaryKey = (array)$this->_table->primaryKey();
$this->_primaryKey = $this->_primaryKey[0];
}
return $this->_primaryKey;
}
}

0 comments on commit 4492cdc

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