/** * Merges queries * * @param array|QueryBuilder|\T4\Core\IArrayable $prototype * @param string $operator AND, OR * @return self * @throws \InvalidArgumentException * @throws \DomainException */ public function merge($prototype, $operator = 'and') { if ($prototype instanceof QueryBuilder) { $params = $prototype->getParams(); $prototype = $prototype->toArray(); $prototype['params'] = $params; } elseif ($prototype instanceof \T4\Core\IArrayable) { $prototype = $prototype->toArray(); } if (!is_array($prototype) && !$prototype instanceof \ArrayAccess) { throw new \InvalidArgumentException('Invalid builder type!'); } if (!empty($this->mode) && !empty($prototype['mode']) && $this->mode !== $prototype['mode']) { throw new \DomainException('Query mode is not much!'); } if (!empty($prototype['select'])) { $this->select($prototype['select']); } if (!empty($prototype['from'])) { $this->from($prototype['from']); } if (!empty($prototype['where'])) { $this->where = empty($this->where) ? $prototype['where'] : "({$this->where}) {$operator} ({$prototype['where']})"; } foreach (['group', 'order', 'limit', 'offset'] as $property) { if (!empty($prototype[$property])) { $this->{$property} = $prototype[$property]; } } foreach (['joins', 'params', 'insertTables', 'updateTables', 'deleteTables', 'values'] as $arrayProperty) { if (!empty($prototype[$arrayProperty])) { $this->{$arrayProperty} = array_merge($this->{$arrayProperty} ?? [], $prototype[$arrayProperty]); } } return $this; }
public function countAllByColumn($class, $column, $value, $options = []) { $query = new QueryBuilder(); $query->select('COUNT(*)')->from($class::getTableName())->where('`' . $column . '`=:value')->params([':value' => $value]); return $class::getDbConnection()->query($query->getQuery($this), $query->getParams())->fetchScalar(); }
/** * @param string|\T4\Dbal\QueryBuilder|\T4\Dbal\Query $query * @param array $params * @return \T4\Dbal\Statement */ public function query($query, array $params = []) { if ($query instanceof QueryBuilder) { $params = array_merge($params, $query->getParams()); $query = $query->getQuery($this->getDriver()); } if ($query instanceof Query) { $params = array_merge($params, $query->params); $query = $this->getDriver()->makeQueryString($query); } $statement = $this->pdo->prepare($query); $statement->execute($params); return $statement; }