public function fetchBy($condition = [], $fields = [], $limit = null, $skip = null, $sort = null) { $collection = new Collection(); if (empty($fields)) { $fields = ['*' => 1]; } $fields = implode(',', array_keys($fields)); if (empty($condition)) { $condition = ['condition' => '1=1', 'params' => []]; } $sql = 'SELECT ' . $fields . ' FROM ' . $this->tableName . ' WHERE ' . $condition['condition']; if ($limit) { $sql .= ' LIMIT :limit'; } if ($skip) { $sql .= ' OFFSET :skip'; } if (!empty($sort)) { $sql .= ' ORDER BY ' . $sort; } $stmt = $this->getDbAdapter()->prepare($sql); if ($limit) { $stmt->bindValue(':limit', $limit, \PDO::PARAM_INT); } if ($skip) { $stmt->bindValue(':skip', $skip, \PDO::PARAM_INT); } foreach ($condition['params'] as $key => $value) { if (!is_array($value)) { $value = [$value, \PDO::PARAM_STR]; } $stmt->bindParam($key, $value[0], $value[1]); } $stmt->execute(); $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); if ($result) { foreach ($result as $row) { $collection->add($row, $row[$this->getIdFieldName()]); } } return $collection; }
public function fetchBy($condition = [], $fields = [], $limit = null, $skip = null, $sort = null) { $collection = new Collection(); $buffer = $this->applyCondition($condition); if (!empty($buffer)) { if ($sort) { // not implemented } if ($limit) { if (!$skip) { $skip = 0; } $buffer = array_slice($buffer, $skip, $limit); } foreach ($buffer as $id => $row) { $collection->add($row, $id); } } return $collection; }
/** * @test */ public function implementsArrayAccess() { $collection = new Collection(); $collection['foo'] = 'bar'; $this->assertEquals('bar', $collection->get('foo')); $this->assertEquals('bar', $collection['foo']); $this->assertTrue(isset($collection['foo'])); unset($collection['foo']); $this->assertNull($collection['foo']); }
public function fetchByMultiId($ids = [], $fields = []) { $collection = new Collection(); if (empty($ids)) { return $collection; } $ids = array_unique($ids); $result = $this->getDao()->fetchBy(['_id' => ['$in' => array_values($ids)]], $fields); $data = []; foreach ($result as $row) { $data[(string) $row["_id"]] = $row; } foreach ($ids as $id) { if (isset($data[(string) $id])) { $collection->add($this->hydrate($data[(string) $id]), (string) $id); } } return $collection; }