Skip to content
Permalink
Browse files

Removing use of cakeError in Model and replacing it with an Exception

  • Loading branch information...
lorenzo committed Jul 16, 2010
1 parent 6fb930c commit b8b46473558ff2cca400981c152d59b80231ba9c
Showing with 64 additions and 4 deletions.
  1. +53 −4 cake/libs/model/model.php
  2. +11 −0 cake/tests/cases/libs/model/model_integration.test.php
@@ -784,6 +784,7 @@ function __generateAssociation($type, $assocKey) {
* Sets a custom table for your controller class. Used by your controller to select a database table.
*
* @param string $tableName Name of the custom table
* @throws MissingTableException when database table $tableName is not found on data source
* @return void
*/
public function setSource($tableName) {
@@ -794,10 +795,7 @@ public function setSource($tableName) {
if ($db->isInterfaceSupported('listSources')) {
$sources = $db->listSources();
if (is_array($sources) && !in_array(strtolower($this->tablePrefix . $tableName), array_map('strtolower', $sources))) {
return $this->cakeError('missingTable', array(array(
'className' => $this->alias,
'table' => $this->tablePrefix . $tableName
)));
throw new MissingTableException($this->alias, $this->tablePrefix . $tableName);
}
$this->_schema = null;
}
@@ -3043,3 +3041,54 @@ function __sleep() {
function __wakeup() {
}
}
/**
* Exception class to be thrown when a database table is not found in the datasource
*
*/
class MissingTableException extends RuntimeException {
/**
* The name of the model wanting to load the database table
*
* @var string
*/
protected $model;
/**
* The name of the missing table
*
* @var string
*/
protected $table;
/**
* Exception costructor
*
* @param string $model The name of the model wanting to load the database table
* @param string $table The name of the missing table
* @return void
*/
public function __construct($model, $table) {
$this->model = $model;
$this->$table = $table;
$message = sprintf(__('Database table %s for model %s was not found.'), $table, $model);
parent::__construct($message);
}
/**
* Returns the name of the model wanting to load the database table
*
* @return string
*/
public function getModel() {
return $this->model;
}
/**
* Returns the name of the missing table
*
* @return string
*/
public function getTable() {
return $this->table;
}
}
@@ -132,6 +132,17 @@ public function testAssociationLazyLoadWithBindModel() {
$this->assertType('User', $Article->User);
}
/**
* Tests that creating a model with no existent database table associated will throw an exception
*
* @expectedException MissingTableException
* @return void
*/
public function testMissingTable() {
$Article = new ArticleB();
$Article->schema();
}
/**
* testPkInHAbtmLinkModelArticleB
*

0 comments on commit b8b4647

Please sign in to comment.
You can’t perform that action at this time.