diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index aa6ead0be..a9258cc37 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -882,14 +882,18 @@ class Model extends Overloadable { $defaults = array(); $cols = $this->loadInfo(); - if (array_key_exists('default', $cols->value[0])) { - $count = count($cols->value); + $names = $cols->extract('{n}.name'); + $values = $cols->extract('{n}.default'); + + if (!empty($names) && !empty($values)) { + $count = count($names); for ($i = 0; $i < $count; $i++) { - if ($cols->value[$i]['name'] != $this->primaryKey) { - $defaults[$cols->value[$i]['name']] = $cols->value[$i]['default']; + if ($names[$i] != $this->primaryKey) { + $defaults[$names[$i]] = $values[$i]; } } } + $this->validationErrors = array(); $this->set(array_filter($defaults)); $this->set($data); @@ -971,7 +975,7 @@ class Model extends Overloadable { * @return boolean True on success save */ function saveField($name, $value, $validate = false) { - return $this->save(array($this->name => array($name => $value)), $validate); + return $this->save(array($this->name => array($name => $value)), $validate, array($name)); } /** diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 92e1254f4..ddbfb94be 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -34,6 +34,17 @@ class Test extends Model { var $useTable = false; var $name = 'Test'; + + function loadInfo() { + return new Set(array( + array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '1', 'length' => '8'), + array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + array('name' => 'email', 'type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'), + array('name' => 'notes', 'type' => 'text', 'null' => '1', 'default' => 'write some notes here', 'length' => ''), + array('name' => 'created', 'type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), + array('name' => 'updated', 'type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) + )); + } } /** * Short description for class. @@ -44,13 +55,19 @@ class ModelTest extends UnitTestCase { function setUp() { - $this->test =& new Test(); + $this->Test =& new Test(); } function testIdentity() { - $result = $this->test->name; + $result = $this->Test->name; $expected = 'Test'; $this->assertEqual($result, $expected); } + + function testCreation() { + $expected = array('Test' => array('notes' => 'write some notes here')); + $this->assertEqual($this->Test->create(), $expected); + } } + ?> \ No newline at end of file