Skip to content

Commit

Permalink
Added an element to warn users about the use of auto-tables
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 4, 2014
1 parent b5efe46 commit 22e6991
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ORM/TableRegistry.php
Expand Up @@ -226,7 +226,7 @@ public static function clear() {
* @return array
*/
public static function genericInstances() {
return $_fallbacked;
return static::$_fallbacked;
}

}
41 changes: 41 additions & 0 deletions src/Template/Element/auto_table_warning.ctp
@@ -0,0 +1,41 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use Cake\ORM\TableRegistry;
$autoTables = TableRegistry::genericInstances();
if (!$autoTables) {
return;
}
?>
<h3>Could this be caused by using Auto-Tables?</h3>
<p>
Some of the Table objects in your application were created by instantiating "<strong>Cake\ORM\Table</strong>"
instead of any other specific subclass.
</p>
<p>This could be the cause for this exception. Auto-Tables are created for you under the following circumstances:</p>
<ul>
<li>The class for the specified table does not exist.</li>
<li>The Table was created with a typo: <strong><em>TableRegistry::get('Atricles');</em></strong></li>
<li>The class file has a typo in the name or incorrect namespace: <strong><em>class Atricles extends Table</em></strong></li>
<li>The file containing the class has a typo or incorrect casing: <strong><em>Atricles.php</em></strong></li>
<li>The Table was used using associations but the association has a typo: <strong><em>$this->belongsTo('Atricles')</em></strong></li>
<li>The table class resides in a Plugin but <strong><em>no plugin notation</em></strong> was used in the association definition.</li>
</ul>
<br/>
<p>Please try correcting the issue for the following table aliases:</p>
<ul>
<?php foreach ($autoTables as $alias => $table) : ?>
<li><strong><?= $alias ?></strong></li>
<?php endforeach; ?>
</ul>
2 changes: 1 addition & 1 deletion src/Template/Error/missing_connection.ctp
Expand Up @@ -34,6 +34,6 @@
<strong>Notice: </strong>
<?= sprintf('If you want to customize this error message, create %s', APP_DIR . DS . 'Template' . DS . 'Error' . DS . basename(__FILE__)); ?>
</p>

<?= $this->element('auto_table_warning'); ?>
<?php
echo $this->element('exception_stack_trace');
2 changes: 1 addition & 1 deletion src/Template/Error/missing_table.ctp
Expand Up @@ -23,5 +23,5 @@
<strong>Notice: </strong>
<?= sprintf('If you want to customize this error message, create %s', APP_DIR . DS . 'Template' . DS . 'Error' . DS . 'missing_table.ctp'); ?>
</p>

<?= $this->element('auto_table_warning'); ?>
<?= $this->element('exception_stack_trace'); ?>
1 change: 1 addition & 0 deletions src/Template/Error/pdo_error.ctp
Expand Up @@ -33,6 +33,7 @@ use Cake\Utility\Debugger;
<strong>SQL Query Params: </strong>
<?= Debugger::dump($error->params); ?>
<?php endif; ?>
<?= $this->element('auto_table_warning'); ?>
<p class="notice">
<strong>Notice: </strong>
<?= sprintf('If you want to customize this error message, create %s', APP_DIR . DS . 'Template' . DS . 'Error' . DS . 'pdo_error.ctp'); ?>
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/ORM/TableRegistryTest.php
Expand Up @@ -284,4 +284,17 @@ public function testSet() {
$this->assertSame($mock, TableRegistry::get('Articles'));
}

/**
* Tests genericInstances
*
* @return void
*/
public function testGenericInstances() {
$foos = TableRegistry::get('Foos');
$bars = TableRegistry::get('Bars');
TableRegistry::get('Articles');
$expected = ['Foos' => $foos, 'Bars' => $bars];
$this->assertEquals($expected, TableRegistry::genericInstances());
}

}

0 comments on commit 22e6991

Please sign in to comment.