Skip to content

Commit

Permalink
Merge pull request #15 from dreanmer/master
Browse files Browse the repository at this point in the history
Duplicated bind exception
  • Loading branch information
magroski authored Dec 29, 2017
2 parents b1335ce + 25fe7f5 commit 964428c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/Exceptions/DuplicatedBindException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: dreanmer
* Date: 29/12/17
* Time: 11:53
*/

namespace Frogg\Exceptions;

class DuplicatedBindException extends \Exception
{
/**
* DuplicatedBindException constructor.
*
* @param string $bind
*/
public function __construct($bind)
{
parent::__construct('`'.$bind.'` bind was already added to this query, to overwriting it (skiping this check), pass bindType [`skipBindCheck` => true]');
}
}
45 changes: 44 additions & 1 deletion src/Model/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Frogg\Model;

use Frogg\Exceptions\DuplicatedBindException;
use Phalcon\Mvc\Model as PhalconModel;

/**
Expand Down Expand Up @@ -44,6 +45,41 @@ public function softDelete($column = 'deleted', $activeValue = 0)
return $this->andWhere($column.'='.$activeValue);
}

/**
* add duplicated bind check.
*
* eg: given a criteria with an 'alreadyAddedBind' in an previously `andWhere` call. When you try:
* $criteria->andWhere('column = :alreadyAddedBind:', ['alreadyAddedBind' => 'other value'])
* it will @throws DuplicatedBindException;
*
* but if you want to reassign this bind to another value, you can skip this check using a bind type 'skipBindCheck' = true:
* $criteria->andWhere('column = :alreadyAddedBind:', ['alreadyAddedBind' => 'other value'], ['skipBindCheck' => true])
*
* I'm not proud of it, but some times we will need to skip it and we can't add more
* parameters to this function cuz it's interfaced...
*
* @param string $conditions
* @param null $bindParams
* @param null $bindTypes
*
* @return PhalconModel\Criteria
*/
public function where($conditions, $bindParams = null, $bindTypes = null)
{
if (is_array($bindParams)) {
$params = $this->getParams();
if (is_array($params['bind'])) {
foreach ($bindParams as $bind => $value) {
if (array_key_exists($bind, $params['bind'])) {
throw new DuplicatedBindException($bind);
}
}
}
}

return parent::where($conditions, $bindParams, $bindTypes);
}

/**
* alias to make more sense when calling it on query building.
*
Expand Down Expand Up @@ -118,7 +154,7 @@ public function getActiveCriterias()

public function findFirst($conditions = false, $bindParams = null, $bindTypes = null)
{
if($conditions){
if ($conditions) {
$this->andWhere($conditions, $bindParams, $bindTypes);
}

Expand Down Expand Up @@ -178,6 +214,13 @@ public function bindTypes(array $bindTypes, $merge = true)
parent::bindTypes($bindTypes);
}

/**
* @param $name
* @param array $arguments
*
* @return $this
* @throws \Exception
*/
public function addCriteria($name, $arguments = [])
{
if (method_exists($this, $name)) {
Expand Down

0 comments on commit 964428c

Please sign in to comment.