/** * Updates schema of selected data source to contain definition for current * model's set. * * @param connection $source data source to use * @throws datasource_exception on updating schema failed * @throws model_exception on incomplete/invalid schema definition */ public static function updateSchema(connection $source) { $dataSet = static::$set_prefix . static::$set; if (!$source->exists($dataSet)) { if (!$source->createDataset($dataSet, static::getSchema(), static::$id)) { throw new datasource_exception($source, 'updating schema failed'); } } foreach (static::$relations as $name => $definition) { $nextModel = static::relation($name)->nodeAtIndex(1)->getModel(); if ($nextModel->isVirtual()) { $nextModel->declareInDatasource($source); } } }
/** * Declares data set of model in provided data source. * * @throws datasource_exception on failing to declare model's data set * @param connection $source * @return model_relation_model current instance */ public function declareInDatasource(connection $source) { $setName = $this->getSetName(); // test runtime cache for including mark on having declared data set in // provided data source before if (array_key_exists($setName, self::$declaredCached)) { foreach (self::$declaredCached[$setName] as $s) { if ($s === $source) { return $this; } } } if ($this->isVirtual()) { // wrapping faked model // -> use provided description to declare model's set in data source if (!$source->createDataset($setName, $this->definition, $this->primaries)) { throw new datasource_exception($source, 'failed to create data set of model ' . $setName); } } else { // wrapping actually existing model // -> call its model::updateSchema() on provided data source $this->reflection->getMethod('updateSchema')->invoke(null, $source); } // add mark to runtime cache on having declared model's data set in // provided data source self::$declaredCached[$setName] = array_merge((array) self::$declaredCached[$setName], array($source)); return $this; }