Left join with Pagerfanta Doctrine - symfony

I'd like show results of two tables but I have this Exception: "Cannot count query which selects two FROM components, cannot make distinction".
My query is:
$queryBuilder = $em->createQueryBuilder()
->select('e, e.id AS id, e.f14010101 AS f14010101, e.f14010105 AS f14010105')
->from('T140101Bundle\Entity\T140101', 'e')
->leftJoin('T140102Bundle\Entity\T140102', 'c', 'WITH', 'c.f14010201= e.id')
->groupBy('e.id');
Can you help me?

Related

Using left join with query builder doctrine

Having some problems retrieving ManytoOne relationships when using left join.
Before was using this query to query for conferences
$qb = $this->createQueryBuilder('u')
->select('u.id,u.comment,
IDENTITY(u.place) AS place_id,
IDENTITY(u.sponsor) AS sponsor_id,
IDENTITY(u.tour) AS tour_id,
u.startat
');
Now I'm trying to left join with diffusion which is tied to the diffusion in a many to many relationship.
$qbt = $this->createQueryBuilder('u')
->select('u','c')
->from('AppBundle:Conference', 'p')
->leftJoin('p.diffusion', 'c');
However this query doesn't return the u.place, u.sponsor and u.tour which are ManyToOne relationships.
leftJoin must be followed by 'WITH'. So for example:
->leftJoin('p.diffusion', 'p', 'WITH', 'p.user=u.id', 'u.id');
But i think it's better to post both your entities so i can give you the exact answer.
Found the issue , I had to add
->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true)
to the getQuery , because getArrayResults by default doesn't return foreign keys( the place, sponsor and tour respectivly).
Here is my final query in the conference repository
$qbt = $this->_em->createQueryBuilder();
$qbt->select('conference','diffusion')
->from('AppBundle:Conference', 'conference')
->leftJoin('conference.diffusion', 'diffusion');
return $qbt
->getQuery()
->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true)
->useQueryCache(true)
->useResultCache(true,3600)
->getArrayResult();

Symfony 2 query builder join without relation (cross join)

I am trying to use the query builder to join 2 tables which have no relation.
Desired end result:
SELECT x, y
FROM x
JOIN y
Query builder code:
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('x');
$qb->from('Test1', 'x');
$qb->join('Test2', 'y');
$qb->orderBy('x.name', 'ASC');
Produces the following DQL:
SELECT x FROM Test1 x INNER JOIN Test2 y ORDER BY x.name ASC
Which results in a syntax error:
[Syntax Error] line 0, col 137: Error: Expected Literal, got 'BY'
The entities Test1 and Test2 don't have a relation (not in the code, nor in the database).
Is there any way to accomplish this?
I would like to use the query builder, because I have a lot of other functionality for the query that depends on the query builder (for filtering and sorting etc.).
I know this is possible with plain SQL, or DQL queries (not produced by the query builder).
You can try the following possibility:
public function getYourData($users) {
$qb = $this->entityManager->createQueryBuilder();
$qb
->select('x', 'y')
->from('Test1', 'x')
->leftJoin(
'Test2',
'y',
\Doctrine\ORM\Query\Expr\Join::WITH,
'x.id = y.reference_id'
)
->orderBy('x.name', 'ASC');
return $qb->getQuery()->getResult();
}

Convert SQL inner join query to Doctrine

Hey I have problem with Convert this SQL
SELECT DISTINCT izo_client.name
FROM izo_client
INNER JOIN izo_measurement
ON izo_client.id=izo_measurement.client_id;
into Doctrine query builder
$qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from('AppBundle:Client', 'u')
->innerJoin('AppBundle:Measurement', 'm', 'WITH', 'u.id = m.client_id')
->distinct();
return $qb->getQuery()->getResult();
The error is:
[Semantical Error] line 0, col 91 near 'client_id': Error: Class AppBundle\Entity\Measurement has no field or association named client_id
DQL is not an SQL. In DQL you operate on objects, not database relations. Properties are used as opposed to database columns.
Since your field is called $clientsBelongsTo, your query should look more like:
$qb = $this->_em->createQueryBuilder();
$qb->select('u')
->from('AppBundle:Client', 'u')
->innerJoin('AppBundle:Measurement', 'm', 'WITH', 'u.id = m.clientsBelongsTo')
->distinct();

Doctrine Query Builder Bug?

I have run into an issue in Doctrine. I have built the following query with queryBuilder
$qb = $query = $this->repoLibrary->createQueryBuilder('l');
$query = $qb
->innerJoin('l.productVariant', 'v')
->innerJoin('v.product', 'p')
->innerJoin('p.taxons', 't', 'WITH', 't.id IN (:array)')
->where('l.user = :user')
->groupBy('l.id HAVING count(DISTINCT t.id) >= :count')
->setParameter('user', $user)
->setParameter('array', $s)
->setParameter('count', count($taxons))
->getQuery();
Here is the query that is logged prior to execution:
SELECT s0_.id AS id0, s0_.consumed_at AS consumed_at1, s0_.created_at AS created_at2, s0_.updated_at AS updated_at3, s0_.user_id AS user_id4, s0_.variant_id AS variant_id5
FROM src_library s0_
INNER JOIN src_variant s1_ ON s0_.variant_id = s1_.id
INNER JOIN src_product s2_ ON s1_.product_id = s2_.id
INNER JOIN src_taxon_product s4_
ON s2_.id = s4_.product_id
INNER JOIN src_taxon s3_ ON s3_.id = s4_.taxon_id
AND (s3_.id IN (1,4))
WHERE s0_.user_id = 1
GROUP BY s0_.id HAVING count(DISTINCT s3_.id) = ? ["1,4",1,2]
When I execute this query (after inserting the parameters seen above) directly in MySQL it works perfectly, returning the 2 results that I'm looking for.
However, when it is executed by Doctrine it returns an empty array.
Any Ideas??
After scouring the interwebs I found the following answer. The problem is with the 'IN' clause. As articulated here:
https://groups.google.com/forum/#!topic/doctrine-dev/-_cINyk2dvs
My problem was being caused by the fact that I was building the 'IN' array as a string.
$s = "1,4"
instead of
$s = array(1,4);
This made all the difference in the world, and also made me feel like a n00b.
Your code looks fine and should work. I can't see your whole code but I'm guessing that it is returning an empty array because you have not actually executed the prepared sql statement yet. You should call the "getResult()" to do this.
Try this:
$qb = $query = $this->repoLibrary->createQueryBuilder('l');
$query = $qb
->select('l, v.id, p.id')
->innerJoin('l.productVariant', 'v')
->innerJoin('v.product', 'p')
->innerJoin('p.taxons', 't', 'WITH', 't.id IN (:array)')
->where('l.user = :user')
->groupBy('l.id HAVING count(DISTINCT t.id) >= :count')
->setParameter('user', $user)
->setParameter('array', $s)
->setParameter('count', count($taxons))
->getQuery()
->getResult();

Symfony2 / Doctrine2 order joined relations with QueryBuilder

I'm using Symfony2 and have a headache on ordering results by joined relations with QueryBuilder. I want to select a group by ID with ordered related items. Here's the code:
$rs = $this
->createQueryBuilder('a')
->select('a', 'c', 'm')
->leftJoin('a.item', 'c', Expr\Join::WITH, 'c.active = 1')
->leftJoin('c.media', 'm')
->where('a.id = :group_id')
// ->orderBy('c.sortOrder', 'asc')
->setParameter('group_id', $group_id)
->getQuery()
->getOneOrNullResult();
This way I get what I want, but items aren't ordered. If I uncomment line ->orderBy('c.sortOrder', 'asc'), I get empty result set. Tried to execute runnable query in PHPMyAdmin and both (with ordering and without) queries return same results, but one is ordered as it should be.
Is there any way to get relations ordered?

Resources