Skip to content

Commit

Permalink
Return bool
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Apr 2, 2017
1 parent 4222bb4 commit d56589b
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Model/Behavior/ToggleBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function afterSave(Event $event, EntityInterface $entity, ArrayObject $op
/**
* @param \Cake\Datasource\EntityInterface $entity
*
* @return mixed
* @return \Cake\Datasource\EntityInterface|null
*/
protected function getCurrent(EntityInterface $entity) {
$conditions = $this->buildConditions($entity);
Expand All @@ -148,7 +148,7 @@ protected function removeFromOthers(EntityInterface $entity) {
/**
* @param \Cake\Datasource\EntityInterface $entity
*
* @return mixed
* @return array
*/
protected function buildConditions(EntityInterface $entity) {
$conditions = $this->config('scope');
Expand All @@ -162,15 +162,17 @@ protected function buildConditions(EntityInterface $entity) {
/**
* @param \Cake\Datasource\EntityInterface $entity
*
* @return void
* @return bool
*/
public function toggleField(EntityInterface $entity) {
$field = $this->getConfig('field');
$id = $entity->get('id');
$conditions = $this->buildConditions($entity);

$this->_table->updateAll([$field => true], ['id' => $id] + $conditions);
$this->_table->updateAll([$field => false], ['id !=' => $id] + $conditions);
$primary = $this->_table->updateAll([$field => true], ['id' => $id] + $conditions);
$others = $this->_table->updateAll([$field => false], ['id !=' => $id] + $conditions);

return $primary + $others > 0;
}

}

0 comments on commit d56589b

Please sign in to comment.