Skip to content

Commit

Permalink
Extracting the primaryKey reading code into a new function to prevent…
Browse files Browse the repository at this point in the history
… errors
  • Loading branch information
lorenzo committed Jul 12, 2014
1 parent 48e457e commit 4492cdc
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/Model/Behavior/TreeBehavior.php
Expand Up @@ -41,6 +41,8 @@ class TreeBehavior extends Behavior {
*/
protected $_table;

protected $_primaryKey;

/**
* Default config
*
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
));
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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' => '_'
];
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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.