/** * Find all. * @param any $params * @param array $paramsParams * @param array|int $limit * @return Oppa\ActiveRecord\EntityCollection */ public final function findAll($params = null, array $paramsParams = null, $limit = null) : EntityCollection { $query = new QueryBuilder($this->getDatabase()->getLink()); $query->setTable($this->table); $query->select("{$this->table}.*"); if (method_exists($this, 'onFind')) { $query = $this->onFind($query); } // fetch all rows, oh ohh.. // e.g: findAll() if (empty($params)) { // nothing to do.. } elseif (!empty($params) && empty($paramsParams)) { $query->where("{$this->table}.{$this->tablePrimary} IN(?)", [$params]); } elseif (!empty($params) && !empty($paramsParams)) { // now, it is user's responsibility to append table(s) before field(s) $query->where($params, $paramsParams); } @(list($limitStart, $limitStop) = (array) $limit); if ($limitStart !== null) { $query->limit((int) $limitStart, $limitStop); } $result = $query->execute(); $entityCollection = new EntityCollection(); foreach ($result as $result) { $entityCollection->add($this, (array) $result); } return $entityCollection; }
$db->connect(); $qb = new QueryBuilder($db->getLink()); $qb->setTable('users'); // $qb->select('id,name'); // $qb->select('id,name')->where('id=?', [1]); // $qb->select('id,name')->where('id=?', [1])->limit(1); // $qb->select('id,name')->whereLike('name LIKE ?', ['%Ker"em%']); // $qb->select('id,name')->whereLike('(id LIKE ? OR name LIKE ?)', ['2%', '%Ke_rem%']); // $qb->select('id,name') // ->where('id=?', [1]) // ->where('(name=? OR name=? OR old BETWEEN %d AND %d)', ['Kerem', 'Murat', 30, 40], $qb::OP_AND) // ; // $qb->select()->aggregate('count'); // pre($qb->get()); $qb->setTable('users u'); $qb->select('u.*, us.score, ul.login')->aggregate('sum', 'us.score', 'sum_score')->join('users_score us', 'us.user_id = u.id')->joinLeft('users_login ul', 'ul.user_id = u.id')->whereIn('u.id', [1, 2, 3])->whereBetween('u.old', [30, 50])->whereNotNull('ul.login')->groupBy('u.id')->orderBy('old')->having('sum_score <= ?', [30])->limit(0, 10); pre($qb->toString()); pre($qb->get()); // pre($qb->getAll()); // pre($qb->execute()); // insert // $qb->insert(['name' => 'Veli', 'old' => 25]); // $qb->insert([['name' => 'Veli', 'old' => 25], ['name' => 'Deli', 'old' => 29]]); // pre($qb->toString()); // $result = $qb->execute(); // pre($result); // pre($result->getId()); // pre($result->getId(true)); // // // update // $qb->update(['old' => 100])->where('id > ?', [30])->limit(1); // $qb->update(['old' => 100])->where('id > ?', [30])->orderBy('id DESC')->limit(1);