public function find(\Modler\Model $model, array $where = array(), $multiple = false)
{
$properties = $model->getProperties();
list($columns, $bind) = $this->setup($where);
$update = array();
foreach ($bind as $column => $name) {
// See if we keep to transfer it over to a column name
if (array_key_exists($column, $properties)) {
$column = $properties[$column]['column'];
}
$update[] = $column . ' = ' . $name;
}
$sql = 'select * from ' . $model->getTableName();
if (!empty($update)) {
$sql .= ' where ' . implode(' and ', $update);
}
$result = $this->fetch($sql, $where);
if ($result !== false && count($result) == 1 && $multiple === false) {
$model->load($result[0]);
return $model;
} elseif (count($result) > 1 || $multiple === true) {
// Make a collection instead
$modelClass = get_class($model);
$collectionNs = str_replace('Model', 'Collection', $modelClass);
if (!class_exists($collectionNs)) {
throw new \InvalidArgumentException('Collection "' . $collectionNs . '" is invalid!');
}
$collection = new $collectionNs($this);
foreach ($result as $item) {
$itemModel = new $modelClass($this, $item);
$collection->add($itemModel);
}
return $collection;
}
return $model;
}