Commit d0175672 by Vladimir Zbrailov

override create

parent 903f5618
...@@ -275,6 +275,26 @@ class ActiveRecord extends BaseActiveRecord ...@@ -275,6 +275,26 @@ class ActiveRecord extends BaseActiveRecord
} }
/** /**
* @param array $row
* @return static
*/
public static function create($row)
{
$record = static::instantiate($row);
$columns = array_flip($record->attributes());
$schema = static::getTableSchema();
foreach ($row as $name => $value) {
if (isset($columns[$name])) {
$record->setAttribute($name, $schema->getColumn($name)->typecast($value));
} else {
$record->$name = $value;
}
}
$record->setOldAttributes($record->getAttributes());
return $record;
}
/**
* Inserts a row into the associated database table using the attribute values of this record. * Inserts a row into the associated database table using the attribute values of this record.
* *
* This method performs the following steps in order: * This method performs the following steps in order:
......
...@@ -1002,11 +1002,8 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -1002,11 +1002,8 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
{ {
$record = static::instantiate($row); $record = static::instantiate($row);
$columns = array_flip($record->attributes()); $columns = array_flip($record->attributes());
/* @var $schema TableSchema */
$schema = static::getDb()->getTableSchema(static::tableName());
foreach ($row as $name => $value) { foreach ($row as $name => $value) {
if (isset($columns[$name])) { if (isset($columns[$name])) {
$value = $schema->getColumn($name)->typecast($value);
$record->_attributes[$name] = $value; $record->_attributes[$name] = $value;
} else { } else {
$record->$name = $value; $record->$name = $value;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment