示例#1
0
 /**
  * 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;
 }
示例#2
0
 /**
  * Checks if the value to be set matches the type set in the field map
  * @param epObject|string $value The value to be set
  * @param epFieldMap $fm The field map for the value
  * @return bool
  */
 protected function _typeMatches($value, $fm)
 {
     // no check if no manager
     if (!$this->ep_m) {
         return true;
     }
     // no checking on primitve, ignore false|null|empty, and non-epObject
     if ($fm->isPrimitive() || !$value) {
         // always true
         return true;
     }
     // epObject
     if ($value instanceof epObject) {
         return $this->_isClassOf($this->ep_m->getClass($value), $fm->getClass());
     } else {
         if (is_array($value) || $value instanceof epArray) {
             foreach ($value as $k => $v) {
                 if (!$v instanceof epObject) {
                     continue;
                 }
                 if (!$this->_isClassOf($this->ep_m->getClass($v), $fm->getClass())) {
                     return false;
                 }
             }
             return true;
         }
     }
     return true;
 }
 /**
  * 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->getClassMap()->getName();
     $class_a = $cm->getName();
     $var_a = $fm->getName();
     $base_b = $fm->getClass();
     // 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);
 }
示例#4
0
 /**
  * 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->getClassMap()->getName();
     $class_a = $cm->getName();
     $var_a = $fm->getName();
     $base_b = $fm->getClass();
     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
     $rs =& parent::find($eo, EP_GET_FROM_DB, false);
     // find from db only, false no cache
     // 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 = $this->_encodeClassOid($rs[0]->get_class_b(), $rs[0]->get_oid_b());
         }
     } else {
         if ($fm->isMany()) {
             $oids_b = array();
             if ($rs) {
                 foreach ($rs as $r) {
                     $oids_b[] = $this->_encodeClassOid($r->get_class_b(), $r->get_oid_b());
                 }
             }
         }
     }
     return $oids_b;
 }
 /**
  * Builds SQL statement from 'contains' or alias assignment node
  * @param epQueryNode &$node either a 'contains' node or an assignment node
  * @param epFieldMap $fm the starting field map
  * @param array $var_exprs expressions for var
  * @param array|epObject $arg the argument|value for the operation
  * @return false|string
  * @throws epExceptionQueryBuilder
  */
 protected function _buildSqlRelationship(epQueryNode &$node, $fm, $var_exprs, $arg)
 {
     // is arg a string
     if (is_string($arg)) {
         // go through each where (notice &)
         foreach ($var_exprs as $alias => &$expr) {
             // remove last alias
             $expr = str_replace($alias, $arg, $expr);
             // remove this alias (important)
             unset($this->aliases[$alias]);
         }
         return implode(' OR ', $var_exprs);
     } else {
         if (is_array($arg) || $arg instanceof epObject) {
             // convert array to an object
             $o = $arg;
             if (is_array($arg)) {
                 // false: no event dispatching, true: null vars if no value
                 $o =& $this->em->createFromArray($fm->getClass(), $arg, false, true);
                 // object and children not to be committed
                 $o->epSetCommittable(false, true);
             }
             // array to keep all expressions
             $exprs = array();
             // go through each var exprs
             foreach ($var_exprs as $var_alias => $var_expr) {
                 // build sql for array or object
                 $obj_exprs = $this->_buildSqlObject($o, $fm, $var_alias);
                 // go through each returned expr from the array or object
                 foreach ($obj_exprs as $obj_expr) {
                     $exprs[] = $obj_expr . ' AND ' . $var_expr;
                 }
             }
             return implode(' OR ', $exprs);
         }
     }
     // unrecognized
     throw new epExceptionQueryBuilder($this->_e("Unrecognized argument in 'contains()'", $node));
     return false;
 }