Example #1
0
 public function testGetSetConditionsParams()
 {
     $query = new Query(Query::SELECT);
     $this->object->setConditions(function (Query $query) {
         $query->where('status', 1);
     });
     $this->assertEquals([], $query->getWhere()->getParams());
     $query->bindCallback($this->object->getConditions(), $this->object);
     $this->assertEquals([new Expr('status', '=', 1)], $query->getWhere()->getParams());
 }
Example #2
0
 /**
  * Filter out all soft deleted records from a select query if $filterDeleted is true.
  * Only apply the filter if the field being filtered on is not part of the original query.
  *
  * @param \Titon\Event\Event $event
  * @param \Titon\Db\Query $query
  * @param string $finder
  * @return bool
  */
 public function preFind(Event $event, Query $query, $finder)
 {
     $config = $this->allConfig();
     if ($config['filterDeleted']) {
         $where = $query->getWhere();
         if ($config['useFlag']) {
             if (!$where->hasParam($config['flagField'])) {
                 $query->where($config['flagField'], false);
             }
         } else {
             if (!$where->hasParam($config['deleteField'])) {
                 $query->where($config['deleteField'], null);
             }
         }
     }
     return true;
 }
Example #3
0
 /**
  * Find the a primary key value within a query. Begin by looping through the where clause and match any value
  * that equates to the PK field. If none can be found, do a select query for a list of IDs.
  *
  * @param \Titon\Db\Query $query
  * @return int|int[]
  */
 public function findID(Query $query)
 {
     $pk = $this->getPrimaryKey();
     // Gather ID from where clause
     foreach ($query->getWhere()->getParams() as $param) {
         if ($param instanceof Expr && $param->getField() === $pk && in_array($param->getOperator(), ['=', 'in'])) {
             return $param->getValue();
         }
     }
     // Query for the ID then
     $select = clone $query;
     $results = array_values($select->setType(Query::SELECT)->fields($pk)->lists($pk, $pk, ['before' => false, 'after' => false]));
     if (count($results) > 1) {
         return $results;
     } else {
         if (count($results) === 1) {
             return $results[0];
         }
     }
     return null;
 }
Example #4
0
 /**
  * Build the DELETE query.
  *
  * @param \Titon\Db\Query $query
  * @return string
  */
 public function buildDelete(Query $query)
 {
     return $this->renderStatement(Query::DELETE, ['table' => $this->formatTable($query->getTable(), $query->getAlias()), 'joins' => $this->formatJoins($query->getJoins()), 'where' => $this->formatWhere($query->getWhere()), 'orderBy' => $this->formatOrderBy($query->getOrderBy()), 'limit' => $this->formatLimit($query->getLimit())] + $this->formatAttributes($query->getAttributes()));
 }