Browse Source

Preventing datasource creationa and access on models having $useTable = false;

Jose Lorenzo Rodriguez 14 years ago
parent
commit
6aa08b5f52
2 changed files with 13 additions and 1 deletions
  1. 1 1
      lib/Cake/Model/Model.php
  2. 12 0
      lib/Cake/Test/Case/Model/ModelIntegrationTest.php

+ 1 - 1
lib/Cake/Model/Model.php

@@ -1219,7 +1219,7 @@ class Model extends Object {
  * @return array Array of table metadata
  */
 	public function schema($field = false) {
-		if (!is_array($this->_schema) || $field === true) {
+		if ($this->useTable !== false && (!is_array($this->_schema) || $field === true)) {
 			$db = $this->getDataSource();
 			$db->cacheSources = ($this->cacheSources && $db->cacheSources);
 			if (method_exists($db, 'describe') && $this->useTable !== false) {

+ 12 - 0
lib/Cake/Test/Case/Model/ModelIntegrationTest.php

@@ -2106,4 +2106,16 @@ class ModelIntegrationTest extends BaseModelTest {
 		ConnectionManager::drop('mock');
 	}
 
+/**
+ * Tests that calling schema() on a model that is not supposed to use a table
+ * does not trigger any calls on any datasource
+ *
+ * @return void
+ **/
+	public function testSchemaNoDB() {
+		$model = $this->getMock('Article', array('getDataSource'));
+		$model->useTable = false;
+		$model->expects($this->never())->method('getDataSource');
+		$this->assertEmpty($model->schema());
+	}
 }