Skip to content

Commit

Permalink
Merge pull request #7242 from tanuck/2.7-no-table-validation-fail
Browse files Browse the repository at this point in the history
2.7 fix PDO exception from Model::exists when useTable is false - #7229
  • Loading branch information
markstory committed Aug 19, 2015
2 parents 979820b + 5b92c90 commit daa795d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lib/Cake/Model/Model.php
Expand Up @@ -2895,6 +2895,10 @@ public function exists($id = null) {
return false;
}

if ($this->useTable === false) {
return false;
}

return (bool)$this->find('count', array(
'conditions' => array(
$this->alias . '.' . $this->primaryKey => $id
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/Case/Model/ModelIntegrationTest.php
Expand Up @@ -1334,7 +1334,7 @@ public function testUseTableFalseExistsCheck() {
$Article->useTable = false;
$Article->id = 1;
$result = $Article->exists();
$this->assertTrue($result);
$this->assertFalse($result);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions lib/Cake/Test/Case/Model/ModelValidationTest.php
Expand Up @@ -554,6 +554,44 @@ public function testValidates() {
$this->assertEquals($expected, $result);
}

/**
* test that validates() still performs correctly when useTable = false on the model.
*
* @return void
*/
public function testValidatesWithNoTable() {
$TestModel = new TheVoid();
$TestModel->validate = array(
'title' => array(
'notEmpty' => array(
'rule' => array('notBlank'),
'required' => true,
),
'tooShort' => array(
'rule' => array('minLength', 10),
),
),
);
$data = array(
'TheVoid' => array(
'title' => 'too short',
),
);
$TestModel->create($data);
$result = $TestModel->validates();
$this->assertFalse($result);

$data = array(
'TheVoid' => array(
'id' => '1',
'title' => 'A good title',
),
);
$TestModel->create($data);
$result = $TestModel->validates();
$this->assertTrue($result);
}

/**
* test that validates() checks all the 'with' associations as well for validation
* as this can cause partial/wrong data insertion.
Expand Down
11 changes: 1 addition & 10 deletions lib/Cake/Test/Case/Model/ModelWriteTest.php
Expand Up @@ -2785,18 +2785,9 @@ public function testRecordExists() {

$TestModel = new TheVoid();
$this->assertFalse($TestModel->exists());
}

/**
* testRecordExistsMissingTable method
*
* @expectedException PDOException
* @return void
*/
public function testRecordExistsMissingTable() {
$TestModel = new TheVoid();
$TestModel->id = 5;
$TestModel->exists();
$this->assertFalse($TestModel->exists());
}

/**
Expand Down

0 comments on commit daa795d

Please sign in to comment.