/** * Returns a query builder that could be used for fetching the list of activity entities * associated with the given target entity * * @param string $targetClassName The FQCN of the activity entity * @param mixed $filters Criteria is used to filter activity entities * e.g. ['age' => 20, ...] or \Doctrine\Common\Collections\Criteria * @param array|null $joins Additional associations required to filter activity entities * @param int|null $limit The maximum number of items per page * @param int|null $page The page number * @param string|null $orderBy The ordering expression for the result * * @return SqlQueryBuilder|null SqlQueryBuilder object or NULL if the given entity type has no activity associations */ public function getActivitiesQueryBuilder($targetClassName, $filters, $joins = null, $limit = null, $page = null, $orderBy = null) { $activities = $this->getActivities($targetClassName); if (empty($activities)) { return null; } return $this->associationManager->getMultiAssociationOwnersQueryBuilder($targetClassName, $filters, $joins, $activities, $limit, $page, $orderBy); }
public function testGetMultiAssociationOwnersQueryBuilder() { $targetClass = 'Oro\\Bundle\\EntityExtendBundle\\Tests\\Unit\\Fixtures\\Associations\\TestTarget1'; $ownerClass1 = 'Oro\\Bundle\\EntityExtendBundle\\Tests\\Unit\\Fixtures\\Associations\\TestOwner1'; $ownerClass2 = 'Oro\\Bundle\\EntityExtendBundle\\Tests\\Unit\\Fixtures\\Associations\\TestOwner2'; $filters = ['name' => 'test']; $joins = []; $associationOwners = [$ownerClass1 => 'targets_1', $ownerClass2 => 'targets_1']; $this->aclHelper->expects($this->at(0))->method('apply')->willReturnCallback(function (QueryBuilder $qb) { return $qb->andWhere('target.age = 10')->getQuery(); }); $this->aclHelper->expects($this->at(1))->method('apply')->willReturnCallback(function (QueryBuilder $qb) { return $qb->andWhere('target.age = 100')->getQuery(); }); $this->entityNameResolver->expects($this->at(0))->method('getNameDQL')->with($ownerClass1, 'e')->willReturn('e.name'); $this->entityNameResolver->expects($this->at(1))->method('getNameDQL')->with($ownerClass2, 'e')->willReturn('e.name'); $result = $this->associationManager->getMultiAssociationOwnersQueryBuilder($targetClass, $filters, $joins, $associationOwners, 5, 2, 'title'); $this->assertEquals('SELECT entity.id_1 AS id, entity.sclr_2 AS entity, entity.name_3 AS title ' . 'FROM (' . 'SELECT t0_.id AS id_0, t1_.id AS id_1, ' . '\'' . $ownerClass1 . '\' AS sclr_2, ' . 't1_.name AS name_3 ' . 'FROM test_owner1 t1_ ' . 'INNER JOIN test_owner1_to_target1 t2_ ON t1_.id = t2_.owner_id ' . 'INNER JOIN test_target1 t0_ ON t0_.id = t2_.target_id ' . 'WHERE t1_.name = \'test\' AND t0_.age = 10' . ' UNION ALL ' . 'SELECT t0_.id AS id_0, t1_.id AS id_1, ' . '\'' . $ownerClass2 . '\' AS sclr_2, ' . 't1_.name AS name_3 ' . 'FROM test_owner2 t1_ ' . 'INNER JOIN test_owner2_to_target1 t2_ ON t1_.id = t2_.owner_id ' . 'INNER JOIN test_target1 t0_ ON t0_.id = t2_.target_id ' . 'WHERE t1_.name = \'test\' AND t0_.age = 100' . ') entity ORDER BY title ASC LIMIT 5 OFFSET 5', $result->getSQL()); }
public function testGetMultiAssociationOwnersQueryBuilder() { $targetClass = 'Oro\\Bundle\\EntityExtendBundle\\Tests\\Unit\\Fixtures\\Associations\\TestTarget1'; $ownerClass1 = 'Oro\\Bundle\\EntityExtendBundle\\Tests\\Unit\\Fixtures\\Associations\\TestOwner1'; $ownerClass2 = 'Oro\\Bundle\\EntityExtendBundle\\Tests\\Unit\\Fixtures\\Associations\\TestOwner2'; $filters = ['name' => 'test']; $joins = []; $associationOwners = [$ownerClass1 => 'targets_1', $ownerClass2 => 'targets_1']; $this->eventDispatcher->expects($this->at(0))->method('dispatch')->willReturnCallback(function ($eventName, GetListBefore $event) { $updatedCriteria = $event->getCriteria()->andWhere(Criteria::expr()->eq('target.age', 10)); $event->setCriteria($updatedCriteria); }); $this->eventDispatcher->expects($this->at(1))->method('dispatch')->willReturnCallback(function ($eventName, GetListBefore $event) { $updatedCriteria = $event->getCriteria()->andWhere(Criteria::expr()->eq('target.age', 100)); $event->setCriteria($updatedCriteria); }); $this->entityNameResolver->expects($this->at(0))->method('getNameDQL')->with($ownerClass1, 'e')->willReturn('e.name'); $this->entityNameResolver->expects($this->at(1))->method('getNameDQL')->with($ownerClass2, 'e')->willReturn('e.name'); $result = $this->associationManager->getMultiAssociationOwnersQueryBuilder($targetClass, $filters, $joins, $associationOwners, 5, 2, 'title'); $this->assertEquals('SELECT entity.id1 AS id, entity.sclr2 AS entity, entity.name3 AS title ' . 'FROM (' . 'SELECT t0_.id AS id0, t1_.id AS id1, ' . '\'' . $ownerClass1 . '\' AS sclr2, ' . 't1_.name AS name3 ' . 'FROM test_owner1 t1_ ' . 'INNER JOIN test_owner1_to_target1 t2_ ON t1_.id = t2_.owner_id ' . 'INNER JOIN test_target1 t0_ ON t0_.id = t2_.target_id ' . 'WHERE t1_.name = \'test\' AND t0_.age = 10' . ' UNION ALL ' . 'SELECT t0_.id AS id0, t1_.id AS id1, ' . '\'' . $ownerClass2 . '\' AS sclr2, ' . 't1_.name AS name3 ' . 'FROM test_owner2 t1_ ' . 'INNER JOIN test_owner2_to_target1 t2_ ON t1_.id = t2_.owner_id ' . 'INNER JOIN test_target1 t0_ ON t0_.id = t2_.target_id ' . 'WHERE t1_.name = \'test\' AND t0_.age = 100' . ') entity ORDER BY title ASC LIMIT 5 OFFSET 5', $result->getSQL()); }