dql

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

佐手、 提交于 2019-11-28 21:14: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]

Complex WHERE clauses using the PHP Doctrine ORM

谁说我不能喝 提交于 2019-11-28 17:48:06
I'm using the PHP Doctrine ORM to build my queries. However, I can't quite seem to figure how to write the following WHERE clause using DQL (Doctrine Query Language): WHERE name='ABC' AND (category1 = 'X' OR category2 = 'X' OR category3 = 'X') AND price > 10 How can I specify where the parentheses go? What I currently have in my PHP code is this: ->where('name = ?', 'ABC') ->andWhere('category1 = ?', 'X') ->orWhere('category2 = ?', 'X') ->orWhere('category3 = ?', 'X') ->andWhere('price > ?', 10) But this produces something like WHERE name='ABC' AND category1 = 'X' OR category2 = 'X' OR

Symfony form query_buider and entity repository

笑着哭i 提交于 2019-11-28 17:47:48
问题 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

Query a ManyToMany relation and display the good result in Symfony with Doctrine

[亡魂溺海] 提交于 2019-11-28 12:42:10
in order to know how it really works, there is an unanswered question from Stack website, and notice that I have the similar problem. In my SQl database , I have two tables: Adverts and Categories Indeed, the Adverts table can contain MANY Categories , and of course a Category can be in many Adverts . So I have a ManyToMany relation between the two tables. in SQL, Doctrine creates me a pivot table named adverts_categories. So far there are no problems , everything is theoretically correct. So, in my SQl database, I have three tables: adverts , adverts_categories and categories like this:

Selecting from subquery in DQL

自古美人都是妖i 提交于 2019-11-28 10:16:48
I would like to perform a SELECT from the results of a subquery in DQL. The equivalent of doing the following in SQL: SELECT * FROM ( SELECT foo1,foo2 FROM bar ) where foo1='something'; The problem I am running into is that it complains that Error: Class '(' is not defined The actual DQL that produces that error is: SELECT u FROM ( SELECT u, COUNT(u) as total FROM Utterance u LEFT JOIN u.recordings r WHERE r.speaker IS NULL OR r.speaker <> 5 GROUP BY u.id ) matched WHERE total < 5 So to reiterate, how can I perform a selection from a sub query? Jovan Perovic Using DQL I'm pretty sure that's

custom query in entity field type

二次信任 提交于 2019-11-28 10:07:37
I build a form with entity type like this: $form = $this->createFormBuilder() ->add('users', 'entity', array( 'class' => 'UserBundle:Users', 'query_builder' => function(EntityRepository $er) { return $er->createQueryBuilder('u') ->orderBy('u.name', 'ASC'); },) ) ->getForm(); Now I want to modify this form, to show only distinct users. I try this: ->add('users', 'entity', array( 'class' => 'UserBundle:Users', 'query_builder' => function(EntityRepository $er) { return $er->createQuery('SELECT DISTINCT u.name FROM UserBundle:Users ORDER BY u.name ASC')->getResult(); },) ) but Symfony throws me an

Is this possible to join tables in doctrine ORM without using relations?

前提是你 提交于 2019-11-28 08:46:40
问题 Suppose there are two tables. Table X-- Columns: id x_value Table Y-- Columns: id x_id y_value Now I dont want to define relationship in doctrine classes and i want to retrieve some records using these two tables using a query like this: Select x_value from x, y where y.id="variable_z" and x.id=y.x_id; I m not able to figure out how to write query like this in doctrine orm EDIT: Table structures: Table 1: CREATE TABLE IF NOT EXISTS `image` ( `id` int(11) NOT NULL AUTO_INCREMENT, `random_name`

Symfony2 Doctrine querybuilder where IN

橙三吉。 提交于 2019-11-28 07:33:32
I losted trilion hours google this but none of the solutions were good. I have this querybuilder: $qb2=$this->createQueryBuilder('s') ->addSelect('u') ->innerJoin('s.user','u') ->where("u.id IN(:followeeIds)") ->andWhere('s.admin_status = false') ->setParameter('user', $user) ->setParameter('followeeIds', $arrayFolloweeIds) ->orderBy('s.id','DESC') ->setMaxResults(15) ; I could do a second query and then do like $qb->getDQL() but have would i cache the query ? Error: Invalid parameter number: number of bound variables does not match number of tokens Ghassan Idriss You are setting the user

Symfony getting logged in user's id

為{幸葍}努か 提交于 2019-11-27 20:21:41
I am developing an application using Symfony2 and doctrine 2. I would like to know how can I get the currently logged in user's Id. Current Symfony versions (Symfony 4, Symfony >=3.2) Since Symfony >=3.2 you can simply expect a UserInterface implementation to be injected to your controller action directly. You can then call getId() to retrieve user's identifier: class DefaultController extends Controller { // when the user is mandatory (e.g. behind a firewall) public function fooAction(UserInterface $user) { $userId = $user->getId(); } // when the user is optional (e.g. can be anonymous)

Deleting record in many-to-many table

痴心易碎 提交于 2019-11-27 14:39:15
问题 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 =