dql

How to export millions of rows from MySQL to CSV via PHP without exhausting memory?

老子叫甜甜 提交于 2019-12-05 14:32:54
So I have this table: mysql> DESCRIBE table; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(15) unsigned | NO | PRI | NULL | auto_increment | | unid | char(9) | NO | UNI | NULL | | | rs | varchar(255) | NO | | NULL | | +-------+------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) Which contains millions of rows: mysql> SELECT COUNT(1) FROM table; +----------+ | COUNT(1) | +----------+ | 9435361 | +----------+ 1 row

doctrine2 - how to use DATE_ADD function

好久不见. 提交于 2019-12-05 12:34:54
问题 I am trying to use the DATE_ADD function from doctrine2 but I am having trouble get it right. I am using like this in DQL: ->andWhere('p.created_at <= DATE_ADD(CURRENT_DATE(),4, day)') but I am getting syntax error: [Syntax Error] line 0, col 215: Error: Expected'.' or '(', got 'day' I tried different implementations but I allways get some kind of syntax errror. I have checked DoctrineExtensions which contain this function, but I shouldnt need it because the function is already included in

Join and count in DQL

ⅰ亾dé卋堺 提交于 2019-12-05 09:35:48
I have a MySQL command and I cannot find the equivalent in DQL. I am trying to fetch the list most commented posts. Here is the MySQL command : SELECT posts.id, COUNT(comments.id) AS num FROM posts LEFT JOIN comments ON ( posts.id = comments.post_id ) GROUP BY posts.id Here is the result : id num 1 8 2 9 3 17 4 7 5 6 6 20 7 7 8 10 9 14 10 7 In DQL, it should be : SELECT post, COUNT(comment.id) AS num FROM Entity\Post post LEFT JOIN post.comments comment GROUP BY post.id But this gives : id num 1 50 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 I don't understand where the 50 comes from and why there is

Where-ing in discriminated tables

二次信任 提交于 2019-12-05 05:36:22
问题 How can I select all items from one specific author ? Its possible this way ? Or how can I edit entities if I want many item types and item packages (item has many items) too ? Item /** * @ORM\Table() * @ORM\Entity * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="discr", type="string") * @ORM\DiscriminatorMap({ * "cd" = "ItemCD", * "dvd" = "ItemDVD", * "pack" = "ItemPack", * }) */ class Item { /** * @ORM\Column(name="id", type="integer", nullable=false) * @ORM\Id * @ORM

How to sort a Doctrine DQL Query by the number or members of a relation?

大兔子大兔子 提交于 2019-12-05 01:38:18
问题 I'm trying to create a query for retrieving objects from a Doctrine database, sorted by the number of members of a specific one-to-many relationship. More specifically: I have two Entities: Person and Federation. A person can be a member of one federation (Person has 'federation' relationship), and a federation may have n people (Federation as 'people' relationship). I would like to create a DQL Query that would return the list of Federations, ordered by how many people are members of that

Get random records with Doctrine

柔情痞子 提交于 2019-12-05 01:21:35
I wonder how to get a random number of Members from a Group, but I do not know what is the best way to do this, and I think ORDER BY RAND() is not the best alternative, as a Group can have more than 100,000 Members, performing this type of query could be very slow. I found this way to make using SQL, but I do not know how to do the same thing in DQL: How can i optimize MySQL's ORDER BY RAND() function? I'm not aware of any way to ORDER BY RAND() "efficiently" from Doctrine. In your situation, the best thing is probably to get primary keys first, shuffle these keys and then make use of them

Doctrine2 Order By before Group By

橙三吉。 提交于 2019-12-04 15:05:47
I am having issues implementing a sub-select solution for ORDERING a resulting dataset before the GROUP BY reduces it. Normally, in SQL you would do a sub-select: SELECT * FROM ( SELECT * FROM a_table order by a_table.timestamp desc ) as table_tmp group by userId However, I am having difficulty implementing this in DQL. Can anyone point me in the right direction please? My query is more complex than this and I assume I JOIN other tables through 'table_tmp' and in the outer SELECT. Thanks. I'm afraid DQL isn't able to handle such a complex query. However Doctrine allows you to write a custom

How to return object from a DQL query?

时光怂恿深爱的人放手 提交于 2019-12-04 09:51:41
I have written a DQL query in Doctrine 2: $qb->select('r.position') ->from('\Entities\Races', 'r') ->where($qb->expr()->eq('r.entrantId', ':entrant_id')) ->setParameter('entrant_id', $this->entrantId); $query = $qb->getQuery(); $aRaces = $query->getResult(); Currently it returns the results of the query in an array like so: Array ( [0] => Array ( [position] => 10 ) [1] => Array ( [position] => 4 ) ) I want the result to return an array of Races objects so that I can access the methods associated with the object (I'm pretty sure the previous version of Doctrine returned objects by default). I

Stuck in building MySQL query

痴心易碎 提交于 2019-12-04 07:06:30
问题 Given an example of table: id | item_id | user_id | bid_price ---------------------------------- The task is to select rows with minimum bid_price for each item_id in the provided set. For example: item_id = [1, 2, 3] - so I need to select up to three (3) rows, having a minimum bid_price . Example of data: id | item_id | user_id | bid_price ---------------------------------- 1 | 1 | 11 | 1 2 | 1 | 12 | 2 3 | 1 | 13 | 3 4 | 1 | 14 | 1 5 | 1 | 15 | 4 6 | 2 | 16 | 2 7 | 2 | 17 | 1 8 | 3 | 18 | 2

Query Builder / DQL not working with INNER JOIN - Syntax Issue

对着背影说爱祢 提交于 2019-12-04 07:01:14
I know I have a syntax isse here however I cant figure it out. I'm trying to do a SELECT and INNER JOIN of 5 tables but Symfony is complaining about the Entities in the JOIN are used before being defined. Actual error is as follows: [Semantical Error] line 0, col 121 near 'I ON C.id = ': Error: Identification Variable MySiteBundle:Items used in join path expression but was not defined before. Here is the PHP code. Note: I have shortened this query to two columns, two tables, and one join to keep the question simple and show my point. The actual query is much longer and is producing the same