private function _findPk(array $args = [])
{
$iterator = $this->getIterator();
$pk = $iterator->getPrimaryKey();
if (empty($pk)) {
throw new ModelException(sprintf("Primary key has not been defined with @Id tag for %s", get_class($this)));
}
if (count($args) != count($pk)) {
throw new InvalidArgumentException(sprintf("The number of arguments passed does not match the primary key fields (%s)", join(', ', $pk)));
}
$pkValues = array_combine($pk, $args);
$stmtFields = '';
$stmtWhere = '';
$arguments = array();
foreach ($iterator->fields() as $field) {
$stmtFields .= ',' . $field->getColumnName();
if (isset($field->id)) {
if (!isset($pkValues[$field->name]) && $field->column->nullable) {
$stmtWhere .= 'AND ' . $field->getColumnName() . ' IS NULL ';
} else {
$stmtWhere .= 'AND ' . $field->getColumnName() . ' = ' . $field->type->wh() . ' ';
$arguments[] = $field->type->toDb($pkValues[$field->name]);
}
}
}
$stmtFields = substr($stmtFields, 1);
$stmtWhere = substr($stmtWhere, 4);
$item = $this->db()->GetRow("\n SELECT {$stmtFields} FROM {$this->table()}\n WHERE {$stmtWhere} LIMIT 1\n ", $arguments);
if ($item) {
$this->load($item);
return $this;
}
return null;
}