/** * Notify the object change has been made to the var this array is associtated to * @param string $event (either 'onPreChange' or 'onPostChange') * @return bool */ protected function _notifyChange($event) { // get var name $var = ''; if ($this->fm) { $var = $this->fm->getName(); } // call object to notify the manager this change return $this->o->epNotifyChange($event, array($var => '')); }
/** * Validate related class and inverse on a field map * @param epFieldMap $fm the field map to be checked * @param string $class the name of the class that the field belongs to * @return array (errors) */ protected function _validateRelationshipField(&$fm, $class) { // array to hold error messages $errors = array(); // // 1. check the opposite class for the field // // string for class and field $class_field = '[' . $class . '::' . $fm->getName() . ']'; // does the relation field have the related class defined? if (!($rclass = $fm->getClass())) { // shouldn't happend $errors[] = $class_field . ' does not have opposite class specified'; return $errors; } // does the related class exist? if (!isset($this->class_maps[$rclass])) { // alert if not $errors[] = 'Class [' . $rclass . '] for ' . $class_field . ' does not exist'; return $errors; } // // 2. check inverse of the field // // does this field have an inverse? if (!($inverse = $fm->getInverse())) { return $errors; } // get the related class map $rcm = $this->class_maps[$rclass]; // get all fields point to the current class in the related class $rfields = $rcm->getFieldsOfClass($class); // 2.a. default inverse (that is, set to true) if (true === $inverse) { // the related class must have only one relationship var to the current class if (!$rfields) { $errors[] = 'No inverse found for ' . $class_field; } else { if (count($rfields) > 1) { $errors[] = 'Ambiguilty in the inverse of ' . $class_field; } else { $rfms = array_values($rfields); $fm->setInverse($rfms[0]->getName()); $rfms[0]->setInverse($fm->getName()); } } return $errors; } // 2.b. inverse is specified // check if inverse exists if (!isset($rfields[$fm->getClass() . ':' . $inverse]) || !$rfields[$fm->getClass() . ':' . $inverse]) { $errors[] = 'Inverse of ' . $class_field . ' (' . $fm->getClass() . '::' . $inverse . ') does not exist'; return $errors; } // get the field map for the inverse $rfm = $rfields[$fm->getClass() . ':' . $inverse]; // set up the inverse on the other side -only if- inverse on the other side // is -not- already set or set to default if (!($rinverse = $rfm->getInverse()) || $rinverse === true) { $rfm->setInverse($fm->getName()); return $errors; } // if specified, check duality if ($class != $rfm->getClass() || $rinverse != $fm->getName()) { $errors[] = 'Inverse of [' . $rcm->getName() . '::' . $fm->getName() . '] is not specified as ' . $class_field; } return $errors; }
/** * Generates SQL statement for -this- node and set up * aliases for all classes (root class and subclasses) * @return false|string * @throws epExceptionQueryPath */ protected function _generateSql() { // -no- sql for primitive field if ($this->fm->isPrimitive()) { // only pass aliases from the parent to children $this->class2alias = $this->getParent()->getAliases(); return ''; } // get base a $base_a = $this->fm->getBase_a(); // get base_b $base_b = $this->fm->getBase_b(); // get class alias from parent if (!($class2alias_a = $this->getParent()->getAliases())) { throw new epExceptionQueryPath("no aliases found from parent node"); return false; } // check if spcific class is set. use it only if so. if ($sc = $this->specificClass()) { // get class map of specific class if (!($cm_b = $this->_em()->getClassMap($sc))) { throw new epExceptionQueryPath("no class map for classes and '{$sc}'"); return false; } } else { // get class map of base b if (!($cm_b = $this->_em()->getClassMap($base_b))) { throw new epExceptionQueryPath("no class map for classes and '{$base_b}'"); return false; } } // get relationship table with base_a and base_b if (!($rt = $this->_em()->getRelationTable($base_a, $base_b))) { throw new epExceptionQueryPath("no relationship table for classes '{$base_a}' and '{$base_b}'"); return false; } $aliases = array(); $aliases[] = ''; // check if we have any children contained $children_contained = false; if ($children = $this->getChildren()) { foreach ($children as $child) { if ($child->isRoot() && $child->isContained()) { $aliases[] = $child->getAlias(); $children_contained = true; } } } $sql = ''; // go through each contained child foreach ($aliases as $alias) { // get alias for relationship table if (!isset($this->table2alias[$rt . '.' . $alias])) { $rt_alias = $this->_am()->getTableAlias($rt, true); $this->table2alias[$rt . '.' . $alias] = $rt_alias; } else { $rt_alias = $this->table2alias[$rt . '.' . $alias]; } // generate sql for class_a (including its subclasses) if ($alias || !$children_contained) { $sql .= $this->_generateSqlClassA($rt, $rt_alias, $this->fm->getName(), $class2alias_a); } // assemble sql for class b and subclasses $cms_b = array(); // collect all children if no specific class set if (!$this->specificClass()) { $cms_b = $cm_b->getChildren(true); } array_unshift($cms_b, $cm_b); foreach ($cms_b as $cm_b_) { // skip abstract if ($cm_b_->isAbstract()) { continue; } // get class b name and make alias $class_b = $cm_b_->getName(); if ($children_contained) { // @@@ alias key may be in the form of 'ClassName.alias' // @@@ to differentiate multiple aliases of the same class if (!isset($this->class2alias[$class_b . '.' . $alias])) { if (!$alias) { $alias_b = $this->_am()->getClassAlias($class_b, true); } else { $alias_b = $this->class2alias[$class_b . '.']; $this->_am()->setClassAlias($class_b, $alias_b . $alias); } $this->class2alias[$class_b . '.' . $alias] = $alias_b . $alias; } $alias_b = $this->class2alias[$class_b . '.' . $alias]; } else { if (!isset($this->class2alias[$class_b])) { $alias_b = $this->_am()->getClassAlias($class_b, true); $this->class2alias[$class_b] = $alias_b; } else { $alias_b = $this->class2alias[$class_b]; } } // generate sql for class b if ($alias || !$children_contained) { $sql .= $this->_generateSqlClassB($rt_alias, $cm_b_->getTable(), $base_b, $class_b, $alias_b, $cm_b_->getOidColumn()); } } } return $sql; }
/** * Returns the relation ids for the variable specified of the object * @param epObject $o the object * @param epFieldMap $fm the field map of the variable * @param epClassMap $cm the class map of the object * @return false|string|array */ public function getRelationIds(&$o, $fm, $cm) { // make sure we are dealing with valid object and non-primitive field if (!$o || !$fm || !$cm) { return false; } // object needs to have a valid id and has to be non-primitive if (!($oid_a = $o->epGetObjectId())) { return false; } // get class_a, var_a, and the related class $base_a = $fm->getBase_a(); $class_a = $cm->getName(); $var_a = $fm->getName(); $base_b = $fm->getBase_b(); if (!$base_a || !$class_a || !$var_a || !$base_b) { throw new epExceptionManager('Cannot find related class for var [' . $class_a . '::' . $var_a . ']'); return false; } // switch relations table $this->_setRelationTable($base_a, $base_b); // make an example relation objects if (!($eo =& $this->_relationExample($class_a, $oid_a, $var_a, $base_b))) { return false; } // find all relation objects using the example object // (find from db only, false: no cache, false: don't convert to objects) $rs =& parent::find($eo, EP_GET_FROM_DB, false, false); // convert result into oids $oids_b = null; if ($fm->isSingle()) { if (is_array($rs) && count($rs) > 1) { throw new epExceptionManager('Field ' . $fm->getName() . ' mapped as composed_of_/has_one but is associated with > 1 objects'); return false; } if ($rs) { $oids_b = $rs[0]; } } else { if ($fm->isMany()) { $oids_b = array(); if ($rs) { $oids_b = $rs; } } } return $oids_b; }
/** * Make where part of a SQL select for relationship fields * @param epDbObject $db the db connection * @param epFieldMap $fm the field map * @param epClassMap $cm the child object for query * @param string $alias the alias of this table in the previous part * @return array('from', 'where') * @author Oak Nauhygon <*****@*****.**> * @author Trevan Richins <*****@*****.**> */ public static function sqlSelectRelations($db, $fm, $cm, $table, $alias, $parentTable) { $base_a = $fm->getBase_a(); $class_a = $cm->getName(); $var_a = $fm->getName(); $base_b = $fm->getBase_b(); // call manager to get relation table for base class a and b $rt = epManager::instance()->getRelationTable($base_a, $base_b); // the alias of the table we are dealing with right now $tbAlias = '_' . $alias; $rtAlias = 'rt' . $alias; // quoted aliases (avoid repeating) $tbAlias_q = $db->quoteId($tbAlias); $rtAlias_q = $db->quoteId($rtAlias); // compute 'from' parts: tables with aliases $from = array(); $from[] = $db->quoteId($table) . ' AS ' . $tbAlias_q; $from[] = $db->quoteId($rt) . ' AS ' . $rtAlias_q; // compute expressions 'where' $where = array(); // rt.class_a = $where[] = $rtAlias_q . '.' . $db->quoteId('class_a') . ' = ' . $db->quote($class_a); // rt.var_a = $where[] = $rtAlias_q . '.' . $db->quoteId('var_a') . ' = ' . $db->quote($var_a); // rt.base_b = $where[] = $rtAlias_q . '.' . $db->quoteId('base_b') . ' = ' . $db->quote($base_b); // rt.class_b = TODO: doesn't look like it is used //$where .= 'rt.'.$db->quoteId('class_b') . ' = ' . $db->quote($val->getClass()); // A.oid = rt.oid_a $where[] = $db->quoteId($parentTable) . '.' . $db->quoteId($cm->getOidColumn()) . ' = ' . $rtAlias_q . '.' . $db->quoteId('oid_a'); // Child.oid = rt.oid_b $where[] = $tbAlias_q . '.' . $db->quoteId($fm->getClassMap()->getOidColumn()) . ' = ' . $rtAlias_q . '.' . $db->quoteId('oid_b'); return array('from' => $from, 'where' => $where); }
/** * Builds SQL statements for a relationship field map. * @param epFieldMap $fm * @param string $alias_a the alias of the parent class * @return string|array (error message if string) */ protected function _buildSqlFieldMapRelationship($fm, $alias_a = self::DUMMY_ALIAS) { // get cm_a, base_a, var_a, base_b $cm_a = $fm->getClassMap(); $base_a = $cm_a->getName(); $var_a = $fm->getName(); $base_b = $fm->getClass(); // call manager to get relation table for base class a and b if (!($rt = $this->em->getRelationTable($base_a, $base_b))) { return "[Internal] No relationship table for [{$base_a}] and [{$base_b}]"; } // change base_a to the actual child being used if ($base_a == $this->root_class) { $base_a = $this->aliases[$this->root_alias]; } // alias for rt relation table $rta = $this->qid($this->generateRtAlias($rt)); // get class map for base_b if (!($cm_base_b = $this->em->getClassMap($base_b))) { return "[Internal] No class map for [{$base_b}]"; } // get all subclasses of base_b (true: recursive) if (!($cms_b = $cm_base_b->getChildren(true))) { // make sure we have an array even if no subclasses $cms_b = array(); } // add base_b into array $cms_b[] = $cm_base_b; // quote value or id (avoid repeating!) $alias_a = $this->qid($alias_a); $class_a = $this->q($base_a); $var_a = $this->q($var_a); $oid_a = $this->qid($cm_a->getOidColumn()); $base_b = $this->q($base_b); // array to keep OR conditions $wheres = array(); // go through each subclass foreach ($cms_b as $cm_b) { // get class_b and generate an alias $class_b = $this->q($cm_b->getName()); $alias_b = $this->qid($alias_b_ = $this->generateAlias($class_b)); $oid_b = $this->qid($cm_b->getOidColumn()); // array to keep all AND where conditions $where = array(); $where[] = $rta . '.class_a = ' . $class_a; $where[] = $rta . '.var_a = ' . $var_a; $where[] = $rta . '.oid_a = ' . $alias_a . '.' . $oid_a; $where[] = $rta . '.base_b = ' . $base_b; $where[] = $rta . '.class_b = ' . $class_b; $where[] = $rta . '.oid_b = ' . $alias_b . '.' . $oid_b; // put into AND array $wheres[$alias_b_] = implode(' AND ', $where); // $alias_b_: alias_b before quoting } return $wheres; }