Browse Source

Added an element to warn users about the use of auto-tables

Jose Lorenzo Rodriguez 11 years ago
parent
commit
22e699121b

+ 1 - 1
src/ORM/TableRegistry.php

@@ -226,7 +226,7 @@ class TableRegistry {
  * @return array
  */
 	public static function genericInstances() {
-		return $_fallbacked;
+		return static::$_fallbacked;
 	}
 
 }

+ 41 - 0
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>

+ 1 - 1
src/Template/Error/missing_connection.ctp

@@ -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');

+ 1 - 1
src/Template/Error/missing_table.ctp

@@ -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 - 0
src/Template/Error/pdo_error.ctp

@@ -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'); ?>

+ 13 - 0
tests/TestCase/ORM/TableRegistryTest.php

@@ -284,4 +284,17 @@ class TableRegistryTest extends TestCase {
 		$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());
+	}
+
 }