Ejemplo n.º 1
0
 /**
  * Same as findWithLanguageFallback, but all properties are nullable.
  */
 public function testFindWithLanguageFallbackNullable()
 {
     $doc = new Comment();
     $doc->id = '/functional/fallback-nullable';
     $doc->setText('Un commentaire');
     $doc->locale = 'fr';
     $this->dm->persist($doc);
     $this->dm->flush();
     $this->dm->clear();
     $this->dm->getLocaleChooserStrategy()->setLocale('it');
     $doc = $this->dm->find(null, '/functional/fallback-nullable');
     $this->assertNotNull($doc);
     $this->assertEquals('fr', $doc->locale);
     $this->assertEquals('Un commentaire', $doc->getText());
 }
Ejemplo n.º 2
0
 /**
  * Determine the current locale of a managed document.
  *
  * If the document is not translatable, null is returned.
  *
  * If the document is translatable and the locale is mapped onto a document
  * field, the value of that field is returned. Otherwise the UnitOfWork
  * information on locales for documents without a locale mapping is
  * consulted.
  *
  * If nothing matches (for example when this is a detached document), the
  * default locale of the LocaleChooserStrategy is returned.
  *
  * @param object        $document the managed document to get the locale for
  * @param ClassMetadata $metadata document metadata, optional
  *
  * @return string|null the current locale of $document or null if it is not translatable
  */
 public function getCurrentLocale($document, ClassMetadata $metadata = null)
 {
     if (null === $metadata) {
         $metadata = $this->dm->getClassMetadata(get_class($document));
     }
     if (!$this->isDocumentTranslatable($metadata)) {
         return null;
     }
     if ($metadata->localeMapping && (!$document instanceof Proxy || $document->__isInitialized())) {
         $locale = $metadata->reflFields[$metadata->localeMapping]->getValue($document);
         if ($locale) {
             return $locale;
         }
     }
     $oid = spl_object_hash($document);
     if (isset($this->documentLocales[$oid]['current'])) {
         return $this->documentLocales[$oid]['current'];
     }
     return $this->dm->getLocaleChooserStrategy()->getLocale();
 }
Ejemplo n.º 3
0
 /**
  * Returns an ODM Query object from the given ODM (query) Builder.
  *
  * Dispatches the From, Select, Where and OrderBy nodes. Each of these
  * "root" nodes append or set PHPCR QOM objects to corresponding properties
  * in this class, which are subsequently used to create a PHPCR QOM object which
  * is embedded in an ODM Query object.
  *
  * @param QueryBuilder $builder
  *
  * @return Query
  */
 public function getQuery(QueryBuilder $builder)
 {
     $this->aliasWithTranslatedFields = array();
     $this->locale = $builder->getLocale();
     if (null === $this->locale && $this->dm->hasLocaleChooserStrategy()) {
         $this->locale = $this->dm->getLocaleChooserStrategy()->getLocale();
     }
     $from = $builder->getChildrenOfType(QBConstants::NT_FROM);
     if (!$from) {
         throw new RuntimeException('No From (source) node in query');
     }
     $dispatches = array(QBConstants::NT_FROM, QBConstants::NT_SELECT, QBConstants::NT_WHERE, QBConstants::NT_ORDER_BY);
     foreach ($dispatches as $dispatchType) {
         $this->dispatchMany($builder->getChildrenOfType($dispatchType));
     }
     if (count($this->sourceDocumentNodes) > 1 && null === $builder->getPrimaryAlias()) {
         throw new InvalidArgumentException('You must specify a primary alias when selecting from multiple document sources' . 'e.g. $qb->from(\'a\') ...');
     }
     // for each document source add phpcr:{class,classparents} restrictions
     foreach ($this->sourceDocumentNodes as $sourceNode) {
         $documentFqn = $this->aliasMetadata[$sourceNode->getAlias()]->getName();
         $odmClassConstraints = $this->qomf->orConstraint($this->qomf->comparison($this->qomf->propertyValue($sourceNode->getAlias(), 'phpcr:class'), QOMConstants::JCR_OPERATOR_EQUAL_TO, $this->qomf->literal($documentFqn)), $this->qomf->comparison($this->qomf->propertyValue($sourceNode->getAlias(), 'phpcr:classparents'), QOMConstants::JCR_OPERATOR_EQUAL_TO, $this->qomf->literal($documentFqn)));
         if ($this->constraint) {
             $this->constraint = $this->qomf->andConstraint($this->constraint, $odmClassConstraints);
         } else {
             $this->constraint = $odmClassConstraints;
         }
     }
     foreach (array_keys($this->aliasWithTranslatedFields) as $alias) {
         $this->translator[$alias]->alterQueryForTranslation($this->qomf, $this->from, $this->constraint, $alias, $this->locale);
     }
     $phpcrQuery = $this->qomf->createQuery($this->from, $this->constraint, $this->orderings, $this->columns);
     $query = new Query($phpcrQuery, $this->dm, $builder->getPrimaryAlias());
     if ($firstResult = $builder->getFirstResult()) {
         $query->setFirstResult($firstResult);
     }
     if ($maxResults = $builder->getMaxResults()) {
         $query->setMaxResults($maxResults);
     }
     return $query;
 }
Ejemplo n.º 4
0
 /**
  * Use the LocaleStrategyChooser to return list of fallback locales
  * @param $desiredLocale
  * @return array
  */
 private function getFallbackLocales($document, $metadata, $desiredLocale)
 {
     $strategy = $this->dm->getLocaleChooserStrategy();
     return $strategy->getPreferredLocalesOrder($document, $metadata, $desiredLocale);
 }