I have the following DQL:
$qb = $this->createQueryBuilder('p')
->select('PARTIAL p.{id, name}', 'p.sku', 'p.name', 'bu.abbreviation AS baseUnit', 'p.price',
'p.WP', 'p.SP', 'p.MP', 'p.PP', 'p.P3', 's.name as supplier')
->join('p.baseUnit', 'bu')
->join('p.suppliers', 's')
->where('p.active = 1');
Product has many suppliers and supplier can be in many products.
The problem in the query is that it returns only 1 supplier.
I tried searching the internet but it always has setParameter on it which i dont need since i just want to get all the products with their related suppliers only without any parameters or whatsoever.
How can I do that in DQL?
Thanks.
Related
I have the following query:
$qb = $this->_em->createQueryBuilder();
$qb->select('a')
->from('CatalogueBundle\Entity\Avantage','a')
->leftJoin('a.avantagegrilles', 'g')
...
->orderBy('a.avantageOrdre', 'ASC');
The result of the query is as follows:
An avantage can contain several avantagegrilles. Now there is a variable in entity avantagegrille called avantagegrilleMini and I want to do an order by asc on it; that is, when an avantage is returned, its avantagegrilles are ordered according to their avantagegrilleMini.
I added ->orderBy('g.avantagegrilleMini', 'ASC') to the query but it didn't have any effect. Is what I want to implement possible in Doctrine?
Let's try this line: ->orderBy('g.avantageOrdre', 'ASC');
I have two entities Message and Skill, and many to many relationship between them. I am trying to get all messages with skill=5 or without any skills.
$queryBuilder = $repository->createQueryBuilder('entity');
$queryBuilder->leftJoin('entity.skills', 'skill');
$queryBuilder->andWhere(
$criteria->expr()->orX(
$criteria->expr()->eq('skill.id', 5),
$criteria->expr()->isNull('skill.id')
)
)
But in results I have question ONLY with skill=5.
How can I select questions without skill or with skill=5 is SINGLE query?
$queryBuilder = $repository->createQueryBuilder('entity');
$queryBuilder->leftJoin('entity.skills', 'skill');
$queryBuilder->where('skill.id = 5');
$queryBuilder->orWhere('skill.id IS NULL');
I am implementing category filters with many to many relations in doctrine using symfony3. I have an entity Business and Category with many to many association. The new table with many to many relations looks like below
business_id category_id
1 1
1 2
2 1
2 2
3 1
Now I want to get all the businesses which are having category_id=1 and category_id=2.
It should select the business id 1,2.
My Sql Query:-
SELECT * FROM business
LEFT JOIN business_category ON business_category.business_id=business.id
WHERE business_category.category_id = 1 AND business_category.category_id = 2
Any SQL or Doctrine query would work.
I would really appreciate for any help.
To get the businesses which exists in both categories your write your query builder as follows,I assume your entities are mapped with proper many to many relationship
$repo = $this->getDoctrine()->getRepository('YourBundle:Business');
$repo = $this->createQueryBuilder('b')
->addSelect('COUNT(DISTINCT c.id) AS total_categories')
->innerJoin('b.categories', 'c');
$categoryIds = array(1,2);
$repo->add('where', $qb->expr()->in('c', $categoryIds))
->groupBy('b.id')
->having('total_categories = '.count($categoryIds))
->getQuery()
->getResult();
For reference see another answer here
you can try
$qb->select( 'p' )
->from( 'AppBundle:Project', 'p' )
->innerJoin( 'p.users', 'u' )
->where( 'u.id=:userIdDaily' )
->andWhere('u.id=:UserID')
->setParameter( 'userIdDaily', $UserObj )
->setParameter( 'UserID', $UserObj->getId() )
;
$query = $qb->getQuery();
$results = $query->getResult();
i have project and user many to many relation. i use this to fetch data with multiple where clauses
I've been trying with no success to user doctrine2 query builder to fetch records in a related many-to-many table using a where clause.
I would like to reproduce the following statement:
SELECT [...] FROM Company
JOIN CompanyAddress ON CompanyAddress.CompanyId = Company.Id
JOIN Address ON Address.Id = CompanyAddress.AddressId
WHERE Address.State = ?
following some ideias found on google, stackoverfow and doctrine docs:
$qb = $this->_em->createQueryBuilder();
$qb->select('c')
->from('Company', 'c')
->where(':State MEMBER OF c.Address')
->setParameter('State', $arguments);
but the results are not the desired one. Any help? Thanks..
I am setting 1 row per page in symfony pager but its showing more data than 1. It is because my query is left joining the sfGuardUser table with other tables and it is causing duplication of 'sfGuardUser' row in some cases when there are multiple entries in 'license' table for that particular user ! Here is my query:
$pager = new sfDoctrinePager('sfGuardUser', '1');
$q = Doctrine_Query::create()
->select('u.id, u.username, g.name, l.id, l.status, l.client_id, l.vendor_id, l.applied_date, p.org_name')
->from('sfGuardUser u')
->leftJoin('u.Profile p')
->leftJoin('u.Groups g')
->leftJoin('u.LicensedClients l')
->where('g.name = \'vendor\'')
->orderBy('l.applied_date');
$q->setHydrationMode(Doctrine_Core::HYDRATE_SCALAR);
$pager->setQuery($q);
$pager->setPage($request->getParameter('page', 1));
$pager->init();
The pagination is there, but problem is in some cases there are multiple rows shown in one page when there are multiple rows for a particular user in the license table. How can I implement it in a right way ?
What do you want to achieve with joining user and license table (1:n relation) when paging through (sfGuard)Users ?
If you want to sort users by license type/date/count, you could use
DISTINCT u.id, u.username