Source of file Where.php
Size: 3,355 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Db/Query/Traits/Where.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Db\Query\Traits; use Bluz\Db\Query\CompositeBuilder; use Bluz\Db\Query\Delete; use Bluz\Db\Query\Select; use Bluz\Db\Query\Update; /** * Order Trait * * Required for: * - Select Builder * - Update Builder * - Delete Builder * * @package Bluz\Db\Query\Traits * @author Anton Shevchuk * * @method Select|Update|Delete addQueryPart(string $sqlPartName, mixed $sqlPart, $append = 'true') * @method mixed getQueryPart(string $queryPartName) * @method string prepareCondition($args = []) */ trait Where { /** * Set WHERE condition * * Specifies one or more restrictions to the query result * Replaces any previously specified restrictions, if any * <code> * $sb = new SelectBuilder(); * $sb * ->select('u.name') * ->from('users', 'u') * ->where('u.id = ?', $id) * ; * </code> * * @param string[] ...$conditions optional the query restriction predicates * * @return $this */ public function where(...$conditions) { $condition = $this->prepareCondition($conditions); return $this->addQueryPart('where', $condition, false); } /** * Add WHERE .. AND .. condition * * Adds one or more restrictions to the query results, forming a logical * conjunction with any previously specified restrictions. * <code> * $sb = new SelectBuilder(); * $sb * ->select('u') * ->from('users', 'u') * ->where('u.username LIKE ?', '%Smith%') * ->andWhere('u.is_active = ?', 1); * </code> * * @param string[] ...$conditions Optional the query restriction predicates * * @return $this */ public function andWhere(...$conditions) { $condition = $this->prepareCondition($conditions); $where = $this->getQueryPart('where'); if ($where instanceof CompositeBuilder && $where->getType() === 'AND') { $where->add($condition); } else { $where = new CompositeBuilder([$where, $condition]); } return $this->addQueryPart('where', $where, false); } /** * Add WHERE .. OR .. condition * * Adds one or more restrictions to the query results, forming a logical * disjunction with any previously specified restrictions. * <code> * $sb = new SelectBuilder(); * $sb * ->select('u.name') * ->from('users', 'u') * ->where('u.id = 1') * ->orWhere('u.id = ?', 2); * </code> * * @param string[] ...$conditions Optional the query restriction predicates * * @return $this */ public function orWhere(...$conditions) { $condition = $this->prepareCondition($conditions); $where = $this->getQueryPart('where'); if ($where instanceof CompositeBuilder && $where->getType() === 'OR') { $where->add($condition); } else { $where = new CompositeBuilder([$where, $condition], 'OR'); } return $this->addQueryPart('where', $where, false); } } |