dql

pass array of conditions to doctrine expr()->orx() method

两盒软妹~` 提交于 2019-11-30 01:24:17
I need to construct DQL with a QueryBuilder like this [QUERY]... AND WHERE e.type = x OR e.type = Y OR e.type = N [...] I have types in array How can I pass this array to my query builder? $qb->andWhere($qb->expr()->orx(CONDITIONS)); List of types will be dynamic, calling $qb->andWhere on each foreach types loop will make only more AND WHERE's no more ORs. Can I store multiply orx expressions and then add it to andWhere ? Any idea how to solve this, probably, common problem? DEY I hope so, then I found this : $conditions = array('e.type = x', 'e.type = Y', 'e.type = N'); $orX = $qb->expr()-

Doctrine 2 DQL - how to select inverse side of unidirectional many-to-many query?

允我心安 提交于 2019-11-30 00:16:38
I have two classes - Page and SiteVersion, which have a many to many relationship. Only SiteVersion is aware of the relationship (because the site is modular and I want to be able to take away and drop in the module that SiteVersion belongs to). How would I therefore select pages based on criteria of SiteVersion? For example, this doesn't work: SELECT p FROM SiteVersion v JOIN v.pages p WHERE v.id = 5 AND p.slug='index' I get the error: [Doctrine\ORM\Query\QueryException] [Semantical Error] line 0, col -1 near 'SELECT p FROM': Error: Cannot select entity through identification variables

doctrine2 dql, use setParameter with % wildcard when doing a like comparison

*爱你&永不变心* 提交于 2019-11-29 23:25:47
I want to use the parameter place holder - e.g. ?1 - with the % wild cards. that is, something like: "u.name LIKE %?1%" (though this throws an error). The docs have the following two examples: 1. // Example - $qb->expr()->like('u.firstname', $qb->expr()->literal('Gui%')) public function like($x, $y); // Returns Expr\Comparison instance I do not like this as there is no protection against code injection. 2. // $qb instanceof QueryBuilder // example8: QueryBuilder port of: "SELECT u FROM User u WHERE u.id = ?1 OR u.nickname LIKE ?2 ORDER BY u.surname DESC" using QueryBuilder helper methods $qb-

Symfony form query_buider and entity repository

半腔热情 提交于 2019-11-29 21:54:41
I'm trying to create a form with data in collection type depending on the user being logged. I'm following this chapter of the Symfony cookbook . Everything works fine when the query_builder option is a closure where I get my data from DQL. As the data need to be fetched from different location in code, I would prefer to define the query in the Repository class. Here is the function in my repository : public function findOwnedBy($user) { $query = $this->getEntityManager()->createQuery("SELECT l FROM MyBundle:Article a JOIN a.owndBy u WHERE u.id = :userId"); $query->setParameters(array("userId"

Limit amount of records retrieved when using Doctrine DQL in Symfony2

て烟熏妆下的殇ゞ 提交于 2019-11-29 16:03:52
问题 I have the following query: $latestcontent = $em->createQuery(' SELECT c.title, c.content, c.lastedit, a.firstname, a.surname FROM ShoutMainBundle:Content c, ShoutMainBundle:Admin a WHERE c.author = a.id ORDER BY c.lastedit ASC' ); What I need to do, is limit the amount of records returned from this query. However, when I add LIMIT 10 to the SQL query, it returns this error: Error: Expected end of string, got 'LIMIT'. So, I had a look and found that you could do add ->limit(10) to the code

Doctrine Query Builder not working with UPDATE and INNER JOIN

为君一笑 提交于 2019-11-29 10:24:34
In my repository I have this query: $qb = $this->getEntityManager()->createQueryBuilder(); $qb ->update('MyBundle:Entity1', 'e1') ->join('e1.Entity2', 'e2') ->set('e1.visibile', '1') ->andWhere('e2.id = :id')->setParameter("id", 123) ; throw this error [Semantical Error] line 0, col 66 near 'e2.id = :id': Error: 'e2' is not defined I have checked the relation and it is right. Is there any issue using join in query update? You can not use join on update and delete queries. You have to use subqueries. Joins are not supported on update and delete queries because it is not supported on all dbms.

Using JOIN in Symfony2/Doctrine SQL

ε祈祈猫儿з 提交于 2019-11-29 06:52:31
问题 I have a problem while trying to USE QueryBuilder OR DQL. I have the following relation: User <-1:n-> Profile <-n:m-> RouteGroup <-1:n-> Route I would like to make a DQL that lists all the routes that a specific user has access. I can get this information with the following code: $usr = $this->container->get('security.context')->getToken()->getUser(); foreach ($usr->getProfiles() as $profile){ foreach ($profile->getRoutegroups() as $routegroup){ var_dump($routegroup->getRoutes()->toArray());

Doctrine 2 DQL - Select rows where a many-to-many field is empty?

不想你离开。 提交于 2019-11-29 05:32:32
I have two classes in this example - DeliveryMethod and Country. They have a many-to-many relationship with each other. What I want to do is select all DeliveryMethods that do not have any Countries mapped to them. I can do the opposite, that is select all delivery methods that have at least one country - SELECT m FROM DeliveryMethod m JOIN m.countries But I can't figure out how to do select where the countries field is empty. In plain SQL I would do the following (deliverymethod_country is the linking table): SELECT m.* FROM deliverymethods m LEFT JOIN deliverymethod_country dc ON dc

Doctrine won't let me select specific fields

拥有回忆 提交于 2019-11-29 03:57:46
The symfony framework features an app/console file that can be executed via php to perform some maintenance tasks. It allows users to run DQL queries as well: # php app/console doctrine:query:dql --hydrate=array \ 'SELECT u.id, u.nameFirst, u.nameLast FROM DatabaseBundle:User u' array 0 => array 'id' => string '1' (length=1) 'nameFirst' => string 'jaroslav' (length=8) 'nameLast' => string 'rakhmatoullin' (length=13) 1 => array 'id' => string '2' (length=1) 'nameFirst' => string 'Båb Kåre' (length=10) 'nameLast' => string 'Ytrefoss' (length=8) Observe that I selected three specific columns. The

Deleting record in many-to-many table

南楼画角 提交于 2019-11-28 23:15:37
I'm following the security chapter of the Symfony 2 book. There's is an example with a table USERS and GROUPS . There is a many-to-many relationship between USERS and GROUPS , which creates in the database a table called USERGROUPS . What I want is to delete a record from USERGROUPS , for example: DELETE from USERGROUPS WHERE user_id = 1 and group_id = 1 I don't know how to do this since I don't have an USERGROUPS.php table file. Using DQL, for example, I want to be able to do this: $em = $this->getDoctrine()->getEntityManager(); $query = $em->createQuery( 'DELETE FROM AcmeStoreBundle