mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Removing use of cakeError in Model and replacing it with an Exception
This commit is contained in:
parent
6fb930c73b
commit
b8b4647355
2 changed files with 64 additions and 4 deletions
|
@ -784,6 +784,7 @@ class Model extends Object {
|
|||
* 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 @@ class Model extends Object {
|
|||
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 @@ class Model extends Object {
|
|||
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 @@ class ModelIntegrationTest extends BaseModelTest {
|
|||
$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
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue