Skip to content

Commit

Permalink
Removing use of cakeError in Model and replacing it with an Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 16, 2010
1 parent 6fb930c commit b8b4647
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
57 changes: 53 additions & 4 deletions cake/libs/model/model.php
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
11 changes: 11 additions & 0 deletions cake/tests/cases/libs/model/model_integration.test.php
Expand Up @@ -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
*
Expand Down

0 comments on commit b8b4647

Please sign in to comment.