/** * Lazy load a one-to-many relationship * * @param string $property The property that we are trying to load * * @return BaseEntityAbstract */ protected function loadOneToMany($property) { // Figure out what the object type is on the many side $this->__loadDaoMap(); $thisClass = get_class($this); $cls = DaoMap::$map[strtolower($thisClass)][$property]['class']; DaoMap::loadMap($cls); $alias = DaoMap::$map[strtolower($cls)]['_']['alias']; $field = StringUtilsAbstract::lcFirst($thisClass); $this->{$property} = Dao::findByCriteria(new DaoQuery($cls), sprintf('%s.`%sId`=?', $alias, $field), array($this->getId())); return $this; }
/** * (non-PHPdoc) * @see HydraEntity::__loadDaoMap() */ public function __loadDaoMap() { DaoMap::setStringType('value', 'varchar', 255); DaoMap::setManyToOne(StringUtilsAbstract::lcFirst($this->_entityClass), $this->_entityClass, strtolower(get_class($this)) . '_entity'); DaoMap::setManyToOne('type', get_class($this) . 'Type', strtolower(get_class($this)) . '_info_type'); parent::__loadDaoMap(); }
/** * building up the join query * * @param string $field The field of the focus class * @param string $joinClass The classname of table that we are trying to join * @param string $alias The alias of the classname of left side of the join * @param string $joinType The type of the join * * @return DaoQuery */ private function _buildJoin($field, $joinClass, $alias, $joinType = self::DEFAULT_JOIN_TYPE, $overrideCond = "") { $overrideCond = trim($overrideCond); //load the dao map of the join class DaoMap::loadMap($joinClass); $focus = strtolower($this->_focus); $fClass = strtolower($joinClass); // the fieldclass $fAlias = DaoMap::$map[$fClass]['_']['alias']; // the join class's alias $fieldMap = DaoMap::$map[$fClass][$field]; $ref = DaoMap::$map[strtolower($joinClass)][$field]['rel']; switch ($ref) { case DaoMap::MANY_TO_MANY: $joinTableMap = strtolower($fieldMap['class']); //Join in the many to many join table if ($fieldMap['side'] == DaoMap::RIGHT_SIDE) { $mtmJoinTable = $fClass . '_' . $joinTableMap; } else { $mtmJoinTable = $joinTableMap . '_' . $fClass; } $this->_addJoin($mtmJoinTable, $mtmJoinTable, $fAlias . '.id = ' . $mtmJoinTable . '.' . StringUtilsAbstract::lcFirst($joinClass) . 'Id', $joinType); //join in the target table $joinCondition = $overrideCond === '' ? $fieldMap['alias'] . '.id = ' . $mtmJoinTable . '.' . StringUtilsAbstract::lcFirst($fieldMap['class']) . 'Id' : $overrideCond; $this->_addJoin($joinTableMap, $fieldMap['alias'], $joinCondition, $joinType); break; case DaoMap::ONE_TO_MANY: $joinCondition = $overrideCond === '' ? $fAlias . '.id = ' . $alias . '.' . StringUtilsAbstract::lcFirst($joinClass) . 'Id' : $overrideCond; $this->_addJoin($fieldMap['class'], $alias, $joinCondition, $joinType); break; case DaoMap::MANY_TO_ONE: $joinCondition = $overrideCond === '' ? $fAlias . '.' . $field . 'Id = ' . $alias . '.id' : $overrideCond; $this->_addJoin($fieldMap['class'], $alias, $joinCondition, $joinType); break; case DaoMap::ONE_TO_ONE: if ($fieldMap['owner']) { //like MANY_TO_ONE $joinCondition = $overrideCond === '' ? $fAlias . '.' . $field . 'Id = ' . $alias . '.id' : $overrideCond; } else { //ONE_TO_MANY $joinCondition = $overrideCond === '' ? '.id = ' . $alias . '.' . StringUtilsAbstract::lcFirst($joinClass) . 'Id' : $overrideCond; } $this->_addJoin($joinClass, $alias, $fAlias . '.id = ' . $alias . '.' . StringUtilsAbstract::lcFirst($joinClass) . 'Id', $joinType); break; default: throw new DaoException('Invalid type(' . $ref . ') for buidling up joins in ' . __CLASS__ . '!'); } }