Skip to content

Commit

Permalink
Fix foreach error when useTable = false.
Browse files Browse the repository at this point in the history
When calling model->create() with useTable = false, an error should not
be triggered.

Fixes #3480
  • Loading branch information
markstory committed Dec 20, 2012
1 parent d70730d commit 08cde9f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/Cake/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1304,10 +1304,8 @@ public function schema($field = false) {
if ($this->useTable !== false && (!is_array($this->_schema) || $field === true)) {
$db = $this->getDataSource();
$db->cacheSources = ($this->cacheSources && $db->cacheSources);
if (method_exists($db, 'describe') && $this->useTable !== false) {
if (method_exists($db, 'describe')) {
$this->_schema = $db->describe($this);
} elseif ($this->useTable === false) {
$this->_schema = array();
}
}
if (is_string($field)) {
Expand Down Expand Up @@ -1477,7 +1475,8 @@ public function create($data = array(), $filterKey = false) {
$this->validationErrors = array();

if ($data !== null && $data !== false) {
foreach ($this->schema() as $field => $properties) {
$schema = (array)$this->schema();
foreach ($schema as $field => $properties) {
if ($this->primaryKey !== $field && isset($properties['default']) && $properties['default'] !== '') {
$defaults[$field] = $properties['default'];
}
Expand Down
14 changes: 14 additions & 0 deletions lib/Cake/Test/Case/Model/ModelIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,20 @@ public function testSchema() {
$this->assertEquals($Post->getColumnTypes(), array_combine($columns, $types));
}

/**
* Check schema() on a model with useTable = false;
*
* @return void
*/
public function testSchemaUseTableFalse() {
$model = new TheVoid();
$result = $model->schema();
$this->assertNull($result);

$result = $model->create();
$this->assertEmpty($result);
}

/**
* data provider for time tests.
*
Expand Down

0 comments on commit 08cde9f

Please sign in to comment.