Build and return a query capable of populating the given inverse-side reference.

@param PropertyMetadata $mapping
@param object           $owner

@return Query|Scan
181696
public function createReferenceInverseSideQuery(PropertyMetadata $mapping, $owner)
    {
        $targetClass = $this->dm->getClassMetadata($mapping->targetDocument);
        $mappedBy    = $targetClass->getPropertyMetadata($mapping->mappedBy);
        $qb          = $this->dm->createQueryBuilder($mapping->targetDocument);

        //add the owning document's identifier as criteria for the mapped reference
        $qb->attr($mappedBy->name)->equals($owner);

        if ($mapping->mappedIndex !== null) {
            $qb->index($mapping->mappedIndex);

            //if mappedIndex is not set, requireIndexes is in effect and the
            //querybuilder cannot find a usable index then the query will throw
            //an exception on execution
        }

        if ($mapping->requireIndexes !== null) {
            //if configured this will override any setting on the dm or class
            $qb->requireIndexes($mapping->requireIndexes);
        }

        $qb->addCriteria((array)$mapping->criteria);

        foreach ((array)$mapping->prime as $attribute) {
            $qb->attr($attribute)->prime(true);
        }

        $query = $qb->getQuery();

        $query->reverse($mapping->reverse);
        $query->limit($mapping->association === PropertyMetadata::ASSOCIATION_TYPE_REFERENCE_ONE ? 1 : $mapping->limit);

        return $query;
    }
197211
protected function getFieldBuilder(ClassMetadataBuilder $builder, array $mapping)
    {
        return Field::make(
            $builder,
            $mapping['type'],
            $this->name
        );
    }
1200474
public function fetch($mappingType = null) {
		list($query, $args) = $this->build();

		//generate a mapper instance
		if (is_null($mappingType))
			$mapper = empty($this->config) ? $this->fluent->getMapper() : $this->fluent->getMapper()->merge($this->config);
		else {
			$config = empty($this->config) ? ['map.type' => $mappingType] : array_merge($this->config, ['map.type' => $mappingType]);
			$mapper = $this->fluent->getMapper()->merge($config);
		}
		
		return empty($args) ? $mapper->query($query) : $mapper->execute($query, $args);
	}
1085565
protected function retrieveUser($email, User $owner)
    {
        $user = $this->objectManager->getRepository('ClarolineCoreBundle:User')->findOneByEmail($email);

        if (null === $user) {
            $user = $owner;
        }

        return $user;
    }
116834
public function get($class, $mapping)
    {
        if (is_object($class)) {
            $class = get_class($class);
        }
        if (array_key_exists($class, $this->handlers)) {
            $handler = $this->handlers[$class];

            $field = call_user_func($handler, $mapping, $this->em);
        } else {
            $field = new $class($mapping, $this->em);
        }

        if ($field instanceof SanitiserAwareInterface) {
            $field->setSanitiser($this->sanitiser);
        }

        return $field;
    }
885220
private function _initMany($property, $method, $mode, $sourcePrimary) {
		$collection = $this->getCollection();
		
		$data = $collection->__call($method);
		if ($data === null) return;

		if ($data instanceof Collection) {
			if ($data->hasMetadata()) {
				$meta = $data->getMetadata();

				$onProperty     = $meta->joinOnProperty;
				$sourceProperty = $meta->joinOnSource;
			} else {
				$onProperty     = null;
				$sourceProperty = null;
			}

			if ($mode === self::MODE_CASCADE) {
				// If not specified in meta, using a requested property
				if ($onProperty === null) {
					$onProperty = $property;
				}

				// If not specified in meta:
				if ($sourceProperty === null) {
					if ($sourcePrimary === null) {
						// Attempt to get from source collection
						$sourceProperty = $data->getPrimary();

						if ($sourceProperty === null) {
							throw new Exception('Unable to determine a source property for join');
						}
					} else {
						// Manually specified
						$sourceProperty = $sourcePrimary;
					}
				}
			}

			if ($mode === self::MODE_JOIN) {
				$collection->join($data, $onProperty, $sourceProperty);
			}
			else if ($mode === self::MODE_CASCADE || $mode === self::MODE_CASCADE_MANY) {
				$collection->setList($onProperty, $data->toArray(), $sourceProperty);
			}
			else {
				throw new Exception('Uncnown loading mode');
			}
		} else {
			throw new Exception('Data must be represented as a collection');
		}
	}
84820
public function createReferenceManyInverseSideQuery(PersistentCollection $collection)
    {
        $hints = $collection->getHints();
        $mapping = $collection->getMapping();
        $owner = $collection->getOwner();
        $ownerClass = $this->dm->getClassMetadata(get_class($owner));
        $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']);
        $mappedByMapping = isset($targetClass->fieldMappings[$mapping['mappedBy']]) ? $targetClass->fieldMappings[$mapping['mappedBy']] : array();
        $mappedByFieldName = isset($mappedByMapping['simple']) && $mappedByMapping['simple'] ? $mapping['mappedBy'] : $mapping['mappedBy'] . '.$id';
        $criteria = $this->cm->merge(
            array($mappedByFieldName => $ownerClass->getIdentifierObject($owner)),
            $this->dm->getFilterCollection()->getFilterCriteria($targetClass),
            isset($mapping['criteria']) ? $mapping['criteria'] : array()
        );
        $criteria = $this->uow->getDocumentPersister($mapping['targetDocument'])->prepareQueryOrNewObj($criteria);
        $qb = $this->dm->createQueryBuilder($mapping['targetDocument'])
            ->setQueryArray($criteria);

        if (isset($mapping['sort'])) {
            $qb->sort($mapping['sort']);
        }
        if (isset($mapping['limit'])) {
            $qb->limit($mapping['limit']);
        }
        if (isset($mapping['skip'])) {
            $qb->skip($mapping['skip']);
        }
        if ( ! empty($hints[Query::HINT_SLAVE_OKAY])) {
            $qb->slaveOkay(true);
        }
        if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) {
            $qb->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]);
        }

        return $qb->getQuery();
    }
410623
def get_by_owner(cls, owner):
        
        return cls.query(cls.owner==cls._get_key(owner))
1278143
protected static function setRelationships(array &$mappedClass, Mapping $mapping, string $className)
    {
        if (!empty($mappedClass[static::RELATIONSHIPS_KEY])) {
            foreach ($mappedClass[static::RELATIONSHIPS_KEY] as $propertyName => $urls) {
                if (false === \in_array($propertyName, static::getClassProperties($className))) {
                    throw new MappingException(
                        \sprintf(
                            'Could not find property %s in class %s because it does not exist.',
                            $propertyName,
                            $className
                        )
                    );
                }

                $mapping->setRelationshipUrls($propertyName, $urls);
            }
        }
    }
861308
public static function create($mapping): Mapping
    {
        if (\is_array($mapping)) {
            $mappingObject = new self();
            $mappingObject->setProperties($mapping);

            return $mappingObject;
        }

        if ($mapping instanceof self) {
            return $mapping;
        }

        throw new InvalidException('Invalid object type');
    }
1281896
private function mapTranslation(ClassMetadata $mapping)
    {
        $metadata = $this->getTranslatableMetadata($mapping->name);

        // Map translatable relation
        if ($metadata->targetEntity
            && $metadata->translatable
            && !$mapping->hasAssociation($metadata->translatable->name)
        ) {
            $targetMetadata = $this->getTranslatableMetadata($metadata->targetEntity);

            $mapping->mapManyToOne(array(
                'fieldName'    => $metadata->translatable->name,
                'targetEntity' => $metadata->targetEntity,
                'inversedBy'   => $targetMetadata->translations->name,
                'joinColumns'  => array(array(
                    'name'                 => 'translatable_id',
                    'referencedColumnName' => 'id',
                    'onDelete'             => 'CASCADE',
                    'nullable'             => false,
                )),
            ));
        }

        if (!$metadata->translatable) {
            return;
        }

        // Map locale field
        if (!$mapping->hasField($metadata->locale->name)) {
            $mapping->mapField(array(
                'fieldName' => $metadata->locale->name,
                'type' => 'string',
            ));
        }

        // Map unique index
        $columns = array(
            $mapping->getSingleAssociationJoinColumnName($metadata->translatable->name),
            $metadata->locale->name,
        );

        if (!$this->hasUniqueConstraint($mapping, $columns)) {
            $constraints = isset($mapping->table['uniqueConstraints']) ? $mapping->table['uniqueConstraints']: array();
            $constraints[$mapping->getTableName() . '_uniq_trans'] = array(
                'columns' => $columns,
            );

            $mapping->setPrimaryTable(array(
                'uniqueConstraints' => $constraints,
            ));
        }
    }
474656
private function update(PropertyMetadata $propertyMetadata, array $metadata): PropertyMetadata
    {
        $metadataAccessors = [
            'description' => 'get',
            'readable' => 'is',
            'writable' => 'is',
            'writableLink' => 'is',
            'readableLink' => 'is',
            'required' => 'is',
            'identifier' => 'is',
            'iri' => 'get',
            'attributes' => 'get',
        ];

        foreach ($metadataAccessors as $metadataKey => $accessorPrefix) {
            if (null === $metadata[$metadataKey]) {
                continue;
            }

            $propertyMetadata = $propertyMetadata->{'with'.ucfirst($metadataKey)}($metadata[$metadataKey]);
        }

        if ($propertyMetadata->hasSubresource()) {
            return $propertyMetadata;
        }

        return $propertyMetadata->withSubresource($this->createSubresourceMetadata($metadata['subresource'], $propertyMetadata));
    }
783346
public function merge(PropertyMetadata $sourceMapping, PropertyMetadata $targetMapping)
    {
        foreach (get_object_vars($sourceMapping) as $key => $value) {
            if ($value !== null) {
                $targetMapping->{$key} = $value;
            }
        }

        return $targetMapping;
    }
1266062
protected function getOnConditionSQL($mapping)
    {
        $targetClass = $this->em->getClassMetadata($mapping['targetEntity']);
        $association = ( ! $mapping['isOwningSide'])
            ? $targetClass->associationMappings[$mapping['mappedBy']]
            : $mapping;

        $joinColumns = $mapping['isOwningSide']
            ? $association['joinTable']['inverseJoinColumns']
            : $association['joinTable']['joinColumns'];

        $conditions = array();

        foreach ($joinColumns as $joinColumn) {
            $joinColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
            $refColumnName  = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);

            $conditions[] = ' t.' . $joinColumnName . ' = ' . 'te.' . $refColumnName;
        }

        return $conditions;
    }
404460
public function createInherited(ClassMetadata $parentClass, PropertyMetadata $parentMapping)
    {
        $mapping = clone $parentMapping;

        if ($mapping->isInherited === null && !$parentClass->isMappedSuperclass) {
            $mapping->isInherited = true;
        }

        return $mapping;
    }
209304
public static function fromOwnerAndName(string $owner, string $name): self
    {
        if (1 !== \preg_match(self::ownerRegEx(), $owner)) {
            throw new Exception\InvalidArgumentException(\sprintf(
                'Owner "%s" does not appear to be a valid owner.',
                $owner
            ));
        }

        if (1 !== \preg_match(self::nameRegEx(), $name)) {
            throw new Exception\InvalidArgumentException(\sprintf(
                'Name "%s" does not appear to be a valid name.',
                $name
            ));
        }

        return new self(
            $owner,
            $name
        );
    }
1337876
protected function getPropertiesFromMapping(Mapping $mapping)
    {
        $properties = array_merge(
            array_combine($mapping->getProperties(), $mapping->getProperties()),
            $mapping->getAliasedProperties()
        );

        return $properties;
    }