dql

Doctrine QueryBuilder - Exclude articles linked to specific themes

南楼画角 提交于 2019-12-13 17:54:49
问题 I'm working on a Symfony2 project using Doctrine2. I have a 'article' and a 'theme' table in a many-to-many relationship. I am trying to get every articles except those linked to theme 35. $query = $this->createQueryBuilder('art') ->join('art.themes', 'the') ->where('the != '.35) ->getQuery() ->getResult(); This request only works when my article has only one theme. If the article has more than one (for example theme 35 + theme 36), it's not excluded from the results. How can I fix this? Here

Doctrine DQL greatest-n-per-group

与世无争的帅哥 提交于 2019-12-13 14:19:36
问题 Here are 2 tables of my Symfony2 project : +-----------+ +----------------------------+ | EVENT | | PHOTO | +-----------+ +------+-----------+---------+ | id | | id | event_id | likes | +-----------+ +------+-----------+---------+ | 1 | | 1 | 1 | 90 | | 2 | | 2 | 1 | 50 | +-----------+ | 3 | 2 | 20 | | 4 | 2 | 10 | +------+-----------+---------+ I would like to select the 2 events with its most liked photo, which would look like : +------------+------------+---------+ | event_id | photo_id |

Doctrine query syntax error in multiple join

半腔热情 提交于 2019-12-13 06:01:46
问题 I'm trying to query the model in my Symfony2 project, and I have a little problem that I can't figure out. Check this out: $q2 = 'SELECT p.code, p.desc, SUM(d.quantity) as quantity, SUM(d.quantity*d.prize) as euros FROM Product p JOIN TransactionDetail d JOIN d.transaction t WHERE d.product IN :array AND t.shop = :shop GROUP BY p.code'; $query2 = $this->em->createQuery($q2) ->setParameter('shop', $shop)->setParameter('array', $array); $result = $query2->getResult(); And I get this error:

Symfony2, Doctrine2, left join (dql) and its result

☆樱花仙子☆ 提交于 2019-12-13 05:32:15
问题 So we have such dql $oQuery = $this->createQueryBuilder('assets') ->addSelect('flags') ->addSelect('types') ->addSelect('groups') ->leftJoin('Site\MainBundle\Entity\123\invFlags', 'flags', Join::WITH, 'assets.flag = flags.flagID') ->leftJoin('Site\MainBundle\Entity\123\invTypes', 'types', Join::WITH, 'assets.typeID = types.typeID') ->leftJoin('Site\MainBundle\Entity\123\invGroups', 'groups', Join::WITH, 'types.groupID = groups.groupID'); if (!empty($aFilter)) { $bFirst = true; foreach (

How do I create an efficient DQL statement, to match my efficient SQL when doing a simple LEFT JOIN?

久未见 提交于 2019-12-12 17:15:09
问题 I can craft a simple SQL that returns 1 when there is an item with requested id and corresponding model that matches FS-% , and 0 otherwise. But when I try to write it as DQL, I fail in all spectacular ways. See EXPLAIN results below. Question: How do I write an efficient DQL? SQL (efficient) select count(*) from item left join product on item.product_id = product.id where item.id=2222 and product.model like "FS-%"; Using Explain: +----+-------------+---------+-------+--------------------+---

Doctrine Query Class Not in Entity Directory

▼魔方 西西 提交于 2019-12-12 10:12:28
问题 My Entity directory for my bundle is getting quite large. I'd like to be able to group my classes into sub-directories. For example, all forum related classes in Entity/Forum/. I've been able to do this relatively successfully, but I don't know how to do a doctrine query now. The following doesn't work. SELECT fp FROM AcmeMainBundle:ForumPost fp WHERE 1 How do I query an entity not directly in the Entity Folder? 回答1: SELECT fp FROM AcmeMainBundle:Forum\ForumPost fp WHERE 1 This will work as

Filter query using SQL IN statement in doctrine

守給你的承諾、 提交于 2019-12-12 09:05:45
问题 One branch may have many customers, a customer may be related to many branches. So this is a many to many relation. Branch: <many-to-many target-entity="Customer" inversed-by="branches" field="customers"/> Customer: <many-to-many field="branches" target-entity="Branch" mapped-by="customers"/> Now I want to perform following query: Select all customers where customer's branch matches a given branch object. This is what I tried: $branch = $em->getRepository('MyBundle:Branch') ->findOneById($bid

With Doctrine what are the benefits of using DQL over SQL?

﹥>﹥吖頭↗ 提交于 2019-12-12 08:49:13
问题 Can someone provide me a couple clear (fact supported) reasons to use/learn DQL vs. SQL when needing a custom query while working with Doctrine Classes? I find that if I cannot use an ORM's built-in relational functionality to achieve something I usually write a custom method in the extended Doctrine or DoctrineTable class. In this method write the needed it in straight SQL (using PDO with proper prepared statements/injection protection, etc...). DQL seems like additional language to learn

Optimize DQL with double join

你。 提交于 2019-12-12 04:25:02
问题 I have such a structure: single profile can attend do several studies and for each study there is statute field which is foreign key to study_statute . In HTML in each row I need to show information for profile: profile.name, profile.study.field, profile.study.statute. I've done this using $profileRepository->findAll() but it generates to many queries and I want to optimize. At the moment there is query fetching profiles , for each profile ask for studies and for each study ask for statute

DQL Doctrine query translation

空扰寡人 提交于 2019-12-12 03:56:45
问题 I have my scores table where I have multiple scores for 1 user. What I am trying to do is to select all highest scores for each user. I am trying to do the fallowing in Doctrine DQL: SELECT * FROM scores s1 LEFT OUTER JOIN scores s2 ON s1.user_id = s2.user_id AND ((s1.score < s2.score) OR (s1.score = s2.score AND s1.date_added < s2.date_added)) WHERE s2.score IS NULL ORDER BY s1.score DESC LIMIT 10 My current state is: $rowQuery = $this->getEntityManager()->createQuery(' SELECT s1 FROM