dql

CASTING attributes for Ordering on a Doctrine2 DQL Query

早过忘川 提交于 2019-11-27 13:13:54
I am trying to get Doctrine2 Entities , ordered by their ID which apparently is a String even though it contains only Numbers. So what I would like to do is something like this: SELECT entity1, cast (entity1.id AS integer) AS orderId FROM Namespace\Bla\MyEntity ORDER BY orderId Is there a way to do something like this in Doctrine2 ? Or, what would be the best practise to get my Result if i can't change the type of the id ( due to customer requirements of course )? Attention : I am not asking SQL Code, i am asking for a Doctrine2 Solution, preferably in DQL You should be able to add your own

Use a DATE() function in a WHERE clause with DQL

99封情书 提交于 2019-11-27 08:55:06
I get a strange error when I execute this DQL query: SELECT u FROM User u LEFT JOIN u.schedule s WHERE DATE(s.timestamp) = DATE(NOW()) The exception is thrown by Doctrine with the message: Expected known function, got 'DATE' The problem looks similar to this bug , but that addresses the DATE() function in a GROUP BY clause and the bug is closed for Doctrine 2.2. At this moment, I get the exception with doctrine 2.4-DEV. The query is meant to select all users scheduled for today. Is there any way I can create this DQL? I tested the SQL version in phpMyAdmin and there the query does not raise an

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

天大地大妈咪最大 提交于 2019-11-27 07:15:30
问题 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

custom query in entity field type

穿精又带淫゛_ 提交于 2019-11-27 03:25:21
问题 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

Symfony2 Doctrine querybuilder where IN

不打扰是莪最后的温柔 提交于 2019-11-27 01:49:51
问题 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:

How to select randomly with doctrine

青春壹個敷衍的年華 提交于 2019-11-26 20:40:59
Here is how I query my database for some words $query = $qb->select('w') ->from('DbEntities\Entity\Word', 'w') ->where('w.indictionary = 0 AND w.frequency > 3') ->orderBy('w.frequency', 'DESC') ->getQuery() ->setMaxResults(100); I'm using mysql and I'd like to get random rows that match the criteria, I would use order by rand() in my query. I found this similar question which basically suggests since ORDER BY RAND is not supported in doctrine, you can randomize the primary key instead. However, this can't be done in my case because I have a search criteria and a where clause so that not every

Symfony getting logged in user's id

ぐ巨炮叔叔 提交于 2019-11-26 20:18:45
问题 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. 回答1: 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

Limiting a doctrine query with a fetch-joined collection?

ε祈祈猫儿з 提交于 2019-11-26 16:22:29
问题 I have a doctrine query that returns blog posts and their comments: SELECT b, c FROM BlogPost b LEFT JOIN b.comments c I would like to limit the results to 10 blog posts. According to the DQL documentation, setMaxResults() doesn't work correctly on queries that fetch-join a collection (comments in this case): If your query contains a fetch-joined collection specifying the result limit methods are not working as you would expect. Set Max Results restricts the number of database result rows,

Use a DATE() function in a WHERE clause with DQL

删除回忆录丶 提交于 2019-11-26 14:22:33
问题 I get a strange error when I execute this DQL query: SELECT u FROM User u LEFT JOIN u.schedule s WHERE DATE(s.timestamp) = DATE(NOW()) The exception is thrown by Doctrine with the message: Expected known function, got 'DATE' The problem looks similar to this bug, but that addresses the DATE() function in a GROUP BY clause and the bug is closed for Doctrine 2.2. At this moment, I get the exception with doctrine 2.4-DEV. The query is meant to select all users scheduled for today. Is there any

Symfony2 and Doctrine - Error: Invalid PathExpression. Must be a StateFieldPathExpression

橙三吉。 提交于 2019-11-26 08:14:01
问题 I have an entity that looks like this: /** * @Gedmo\\Tree(type=\"nested\") * @ORM\\Table(name=\"categories\") * @ORM\\Entity() */ class Category extends BaseCategory { /** * @ORM\\OneToMany(targetEntity=\"Category\", mappedBy=\"parent\") */ protected $children; /** * @Gedmo\\TreeParent * @ORM\\ManyToOne(targetEntity=\"Category\", inversedBy=\"children\") * @ORM\\JoinColumn(name=\"parent_id\", referencedColumnName=\"id\", onDelete=\"SET NULL\") */ protected $parent; } and I am trying to run a