public function testModelLoadingCacheInformation() { $models = Doctrine::getLoadedModels(); $this->assertTrue(in_array('AggressiveModelLoadingUser', $models)); $this->assertTrue(in_array('ConservativeModelLoadingProfile', $models)); $this->assertTrue(in_array('ConservativeModelLoadingContact', $models)); $modelFiles = Doctrine::getLoadedModelFiles(); $this->assertTrue(file_exists($modelFiles['ConservativeModelLoadingUser'])); $this->assertTrue(file_exists($modelFiles['ConservativeModelLoadingProfile'])); $this->assertTrue(file_exists($modelFiles['ConservativeModelLoadingContact'])); }
function find_table_with_column($colname) { // check the last table looked at global $last_tbl_cache; if ($last_tbl_cache && $last_tbl_cache->hasColumn($colname)) { return $last_tbl_cache; } // search all the models $models = Doctrine::getLoadedModelFiles(); foreach ($models as $name => $loc) { if ($name != 'TankSource') { $t = Doctrine::getTable($name); if ($t->hasColumn($colname)) { $last_tbl_cache = $t; return $t; } } } throw new Exception("Unable to find table with column '{$colname}'"); }
public function setUp() { $thisClass = $this->getInvoker(); $thisClassName = $thisClass->getTable()->getComponentName(); if (!empty($thisClass->relationType)) { $this->relationType = $thisClass->relationType; $mode = ' hasMany '; } else { $mode = ' hasOne '; } // If this class is extending Bluebox_Class directly, then load any possibly related models and setup our subclasses if (get_parent_class($thisClass) == 'Bluebox_Record') { // Go through every model and figure out if it is an extension of this model. If so, relate it via subclasses // Note that for efficiency, we assume that we only need to inspect classes that end in the same name as our base class // i.e. if the base class is named 'Device' we would only look at other models named things like 'SipDevice' or 'IaxDevice' //kohana::log('debug', '*** We are setting up the base class ' . $thisClassName . '...'); foreach (Doctrine::getLoadedModelFiles() as $model => $dir) { if (substr($model, strlen($model) - strlen($thisClassName), strlen($thisClassName)) == $thisClassName) { if (is_subclass_of($model, $thisClassName)) { Doctrine::initializeModels($model); } } } } else { // We are extending some other class. Figure out the pieces $baseClassName = get_parent_class($thisClass); $relatesTo = substr($thisClassName, 0, strlen($thisClassName) - strlen($baseClassName)); if (self::DEBUG) { kohana::log('debug', '*** Setting up extended version of ' . $baseClassName . ' (' . $thisClassName . ")"); } if (class_exists($relatesTo, TRUE)) { $relatedPrimaryKey = Doctrine::getTable($relatesTo)->getIdentifier(); if (self::DEBUG) { kohana::log('debug', $thisClassName . $mode . $relatesTo); } // Relate this class to the foreign model. So if this was DeviceNumber, this would relate us to Device if ($this->relationType == Doctrine_Relation::MANY) { $thisClass->hasMany($relatesTo, array('local' => 'foreign_id', 'foreign' => $relatedPrimaryKey), $this->relationType); } else { $thisClass->hasOne($relatesTo, array('local' => 'foreign_id', 'foreign' => $relatedPrimaryKey), $this->relationType); } // Also relate the base class to the foreign model. if (self::DEBUG) { kohana::log('debug', $baseClassName . $mode . $relatesTo); } $foreignTable = Doctrine::getTable($baseClassName); $foreignTable->bind(array($relatesTo, array('local' => 'foreign_id', 'foreign' => $relatedPrimaryKey)), $this->relationType); // Unfortunately, we can't turn off individual constraints in Doctrine. So we must turn off constratints for the // entire table, because foreign_id may map to multiple different other tables. This is only necessary on // concrete tables, since the inheritance tables aren't "real" tables and don't get created anyway. // TODO: Find a better solution to keeping constraint checks ! $thisClass->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL ^ Doctrine::EXPORT_CONSTRAINTS); $foreignTable->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL ^ Doctrine::EXPORT_CONSTRAINTS); // Create the relation on the other side, too if (self::DEBUG) { kohana::log('debug', $relatesTo . $mode . $thisClassName . " (as {$baseClassName})"); } $foreignTable = Doctrine::getTable($relatesTo); $foreignTable->bind(array($thisClassName . ' as ' . $baseClassName, array('local' => $relatedPrimaryKey, 'foreign' => 'foreign_id')), $this->relationType); // Now also relate to subclasses of the foreign model. So if there were other types of Devices, relate us to them, too foreach (Doctrine::getLoadedModelFiles() as $model => $dir) { if (substr($model, strlen($model) - strlen($relatesTo), strlen($relatesTo)) == $relatesTo) { if (is_subclass_of($model, $relatesTo)) { if (self::DEBUG) { kohana::log('debug', $model . $mode . $thisClassName . " (searched for instances of {$relatesTo})\n"); } Doctrine::getTable($thisClassName)->addRecordListener(new PolymorphicRecordListener($model)); $foreignTable = Doctrine::getTable($model); $foreignTable->bind(array($thisClassName . ' as ' . $baseClassName, array('local' => $relatedPrimaryKey, 'foreign' => 'foreign_id')), $this->relationType); } } } } } }
/** * Cleanup temporary generated models after a diff is performed * * @return void */ protected function _cleanup() { $modelFiles = Doctrine::getLoadedModelFiles(); $filesToClean = array_diff($modelFiles, $this->_startingModelFiles); foreach ($filesToClean as $file) { if (file_exists($file)) { unlink($file); } } // clean up tmp directories Doctrine_Lib::removeDirectories(sys_get_temp_dir() . DIRECTORY_SEPARATOR . strtolower(self::$_fromPrefix) . '_doctrine_tmp_dirs'); Doctrine_Lib::removeDirectories(sys_get_temp_dir() . DIRECTORY_SEPARATOR . strtolower(self::$_toPrefix) . '_doctrine_tmp_dirs'); }