/** * @param array $cond * @param array $order * @param int|null $limit * @param int|null $offset * * @return static[] * @throws InvalidEntityException if the select query does not contain an `id` column * @throws \InvalidArgumentException if any value in $order is not "ASC" or "DESC" (case insensitive) */ public static function findBy(array $cond, array $order = [], int $limit = null, int $offset = null) : array { static::__load(); $table = EntityMetadataRegistry::get(static::class)->getTable(); $table->project('*')->take($limit)->skip($offset); foreach ($cond as $column => $value) { $table->where($table[$column]->eq($value)); } foreach ($order as $column => $dir) { $_dir = strtolower($dir); if ($_dir === OrderingNode::ORDER_ASC) { $table->order($table[$column]->asc()); } else { if ($_dir === OrderingNode::ORDER_DESC) { $table->order($table[$column]->desc()); } else { throw new \InvalidArgumentException(sprintf('%s is not a recognized order direction', $dir)); } } } $entities = []; foreach (EntitiesConfiguration::getConnection()->fetch($table->from()->getSql()) as $columns) { if (!isset($columns->id)) { throw new InvalidEntityException(static::class); } $entity = EntitiesConfiguration::getCache()->get(static::class, $columns->id); if ($entity === null) { $entity = EntitiesConfiguration::getHydrator()->hydrate(static::class, (array) $columns); EntitiesConfiguration::getCache()->add($entity); } $entities[] = $entity; } return $entities; }
public function reset() { $this->reader = new SqlReader($this->table, EntitiesConfiguration::getConnection()); }