/** * Retrieves HTML code of relating elements of initially related or * explicitly selected element. * * @param array $data custom data to be passed to template on rendering * @param string $template name of custom template to use instead of default one on rendering * @param int $listNodeAtIndex index of node to list properties of * (default: node at opposite end of relation) * @return string rendering result */ public function render($data = array(), $template = null, $listNodeAtIndex = -1) { $query = $this->query(); // extend query to fetch all properties of selected node's model $query->addProperty($this->datasource->qualifyDatasetName($this->nodeAtIndex($listNodeAtIndex)->getName()) . '.*'); // process query $matches = $query->execute()->all(); // start variable space initialized using provided set of custom data $data = variable_space::fromArray($data); // add fetched relation instances to variable space $data->update('matches', $matches); // add reference on current relation manager instance $data->update('relation', $this); // render variable space using selected or default template return view::render($template ? $template : 'model/relation/generic', $data); }
/** * Writes data on item to be created into provided datasource. * * The invocation of this method is wrapped in a transaction in context of * linked datasource. Thus this method may throw exceptions for rolling back * partial modifications in datasource. * * The method is considered to return array consisting of all properties * and related values used to identify created instances of current model. * * The method must be defined public for internal use in a closure. By design * it should be considered protected. * * @param connection $link link to datasource * @param array $arrProperties properties of new instance to write into datasource * @return array ID-components of created instance (to be used on loading this instance by model::select()) * @throws datasource_exception * @throws model_exception on missing parts of multidimensional ID */ public static function _onCreate(connection $link, $arrProperties) { $set = static::$set_prefix . static::$set; // auto-assign ID to item unless properties include explicit ID if (count(static::$id) === 1) { $idName = static::idName(0); if (!array_key_exists($idName, $arrProperties)) { $arrProperties[$idName] = $link->nextID($set); } } // validate and extract ID of item to create $item = array(); foreach (static::$id as $name) { if (array_key_exists($name, $arrProperties)) { $item[$name] = $arrProperties[$name]; } else { throw new model_exception('missing properties of created item\'s identifier'); } } // prepare SQL statement for inserting record on new item $columns = array_keys($arrProperties); $values = array_values($arrProperties); $marks = array_pad(array(), count($arrProperties), '?'); $columns = array_map(function ($n) use($link) { return $link->quoteName($n); }, $columns); $qSet = $link->qualifyDatasetName($set); $columns = implode(',', $columns); $marks = implode(',', $marks); // query datasource for inserting record of new item if ($link->test("INSERT INTO {$qSet} ({$columns}) VALUES ({$marks})", $values) === false) { throw new datasource_exception($link, 'failed to create item in datasource, model ' . $set); } return $item; }
/** * Qualifies and/or quotes provided property names. * * @param array $arrNames set of property names to process * @param string|null $strSetOrAlias name/alias of data set, provide for qualified names, omit for unqualified names * @param connection $source used optionally to quote names for use in querying connected data source * @return array set of qualified and/or quoted names of properties */ protected function _qualifyNames($arrNames, $strSetOrAlias = null, connection $source = null) { $parts = array(); if (is_string($strSetOrAlias)) { $parts[] = $source ? $source->qualifyDatasetName(trim($strSetOrAlias)) : $strSetOrAlias; } foreach ($arrNames as $key => $name) { $temp = $parts; $temp[] = $source ? $source->quoteName(trim($name)) : trim($name); $arrNames[$key] = implode('.', $temp); } return $arrNames; }
/** * Retrieves name of data set optionally combined with declared alias. * * On providing datasource connection either part of resulting name is * quoted (and qualified) according to quoting and qualification rules of * connected datasource. * * @param connection $db * @return string */ public function getFullName(connection $db = null) { $name = $this->getName(true); $alias = $this->alias ? $this->alias : null; if ($db) { $name = $db->qualifyDatasetName($name); if ($alias) { $alias = $db->quoteName($alias); } } return $alias ? $name . ' ' . $alias : $name; }