/** * Z $findBy prevede do $where v dibi formatu, $findBy vyprazdni. * @param BaseDibiCollection * @param DibiMapper * @param IDatabaseConventional * @param array reference * @param array reference * @param string * @param string * @see DataSourceCollection::getDataSource * @see DibiCollection::__toString * @see DibiCollection::join */ public static function dibiProcess(BaseDibiCollection $collection, DibiMapper $mapper, IDatabaseConventional $conventional, array &$where, array &$findBy, $tableAlias, $prefix = NULL) { foreach ($findBy as $tmp) { foreach ($tmp as $key => $value) { if ($prefix) { $key = $prefix . '->' . $key; } if ($join = $mapper->getJoinInfo($key)) { $collection->join($key); $key = $join->key; } else { $key = $conventional->formatEntityToStorage(array($key => NULL)); $key = $tableAlias . key($key); } if ($value instanceof IEntityCollection) { try { $value = $value->fetchPairs(NULL, 'id'); } catch (EntityNotPersistedException $e) { $value = $value->fetchAll(); } } if ($value instanceof IEntity) { $where[] = array('%n = %s', $key, isset($value->id) ? $value->id : NULL); // `= NULL` never be true } else { if (is_array($value)) { $tmp = array(); foreach ($value as $v) { if ($v instanceof IEntity) { if (!isset($v->id)) { continue; } $v = $v->id; } $tmp[] = $v; } $where[] = array('%n IN %in', $key, array_unique($tmp)); } else { if ($value === NULL) { $where[] = array('%n IS NULL', $key); } else { if ($value instanceof DateTime) { $where[] = array('%n = %t', $key, $value); } else { $where[] = array('%n = %s', $key, $value); } } } } } } $findBy = array(); }
/** * @param string * @return array */ private function getInfo($sourceKey) { if (!isset($this->cache[$sourceKey])) { $tmp = array(); $mappper = $this->mapper; $conventional = $this->mapper->getConventional(); $model = $this->mapper->getModel(); if (!isset($this->relationships[$sourceKey])) { throw new MapperJoinException(get_class($this->mapper->getRepository()) . ": has no joinable relationship on `{$sourceKey}`. It is not possible to execute join."); } $joinRepository = $model->getRepository($this->relationships[$sourceKey][0]); $tmp['mapper'] = $joinRepository->getMapper(); if (!$tmp['mapper'] instanceof DibiMapper) { throw new MapperJoinException(get_class($joinRepository) . " ({$sourceKey}) not using Orm\\DibiMapper. It is not possible to execute join."); } $tmp['conventional'] = $tmp['mapper']->getConventional(); $manyToManyJoin = NULL; if (isset($this->relationships[$sourceKey][3])) { $manyToManyJoin = $this->relationships[$sourceKey][3]; $manyToManyJoin = array('alias' => 'm2m__' . $sourceKey, 'xConventionalKey' => $this->format($manyToManyJoin[1], $conventional), 'yConventionalKey' => $this->format($manyToManyJoin[2], $tmp['conventional']), 'table' => $manyToManyJoin[0], 'findBy' => array()); } $tmp['table'] = NULL; // lazy load v DibiMapper::getJoinInfo() ($tmp['mapper']->getTableName()) // todo brat table z collection? if ($tmp['mapper']->getConnection() !== $this->mapper->getConnection()) { // todo porovnavat connection na collection? throw new MapperJoinException(get_class($joinRepository) . " ({$sourceKey}) has used different instance of Orm\\DibiConnection than " . get_class($this->mapper->getRepository()) . ". It is not possible to execute join."); } $tmp['sourceKey'] = $sourceKey; $tmp['xConventionalKey'] = $this->format($this->relationships[$sourceKey][1], $conventional, $sourceKey); $tmp['yConventionalKey'] = $this->format($this->relationships[$sourceKey][2], $tmp['conventional']); $tmp['alias'] = $sourceKey; $collection = $tmp['mapper']->findAll(); if (!$collection instanceof DibiCollection) { throw new MapperJoinException(get_class($joinRepository) . " ({$sourceKey}) not using Orm\\DibiCollection. It is not possible to execute join."); } $collectionArray = (array) $collection; // hack if ($collectionArray["*where"]) { throw new MapperJoinException(get_class($joinRepository) . " ({$sourceKey}) Orm\\DibiCollection has used where() at findAll. It is not possible to execute join."); } $tmp['findBy'] = $collectionArray["*findBy"]; $this->cache[$sourceKey] = $manyToManyJoin ? array($manyToManyJoin, $tmp) : array($tmp); } return $this->cache[$sourceKey]; }