I have a query in Doctrine's DQL that needs to be able to use MySQL's "FORCE INDEX" functionality in order to massively reduce the query time. Below is what the query basically looks like in plain SQL:
SELECT id FROM items FORCE INDEX (best_selling_idx)
WHERE price = ... (etc)
LIMIT 200;
I assume I have to extend some Doctrine component to be able to do this with DQL (or is there a way to inject arbitrary SQL into one of Doctrin's queries?). Anyone have any ideas?
I've found very few helpful Doctrine_RawSql examples online, so here's what I ended up doing to create my query.
$q = new Doctrine_RawSql();
$q->select('{b.id}, {b.description}, {c.description}')
->from('table1 b FORCE INDEX(best_selling_idx) INNER JOIN table2 c ON b.c_id = c.id')
->addComponent('b', 'Table1 b')
->addComponent('c', 'b.Table2 c');
If you don't like native SQL, you can use the following patch. https://gist.github.com/arnaud-lb/2704404
This patch suggests to create only one custom Walker and set it up as follows
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'UseIndexWalker');
$query->setHint(UseIndexWalker::HINT_USE_INDEX, 'some_index_name');
I created a Doctrine SqlWalker extension to apply USE INDEX and FORCE INDEX hints using DQL on top of MySql. Works with both createQuery and createQueryBuilder. You can set different index hints per DQL table aliases.
use Ggergo\SqlIndexHintBundle\SqlIndexWalker;
...
$query = ...;
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, SqlIndexWalker::class);
$query->setHint(SqlIndexWalker::HINT_INDEX, [
'your_dql_table_alias' => 'FORCE INDEX FOR JOIN (your_composite_index) FORCE INDEX FOR ORDER BY (PRIMARY)',
'your_another_dql_table_alias' => 'FORCE INDEX (PRIMARY)',
...
]);
https://github.com/ggergo/SqlIndexHintBundle
Related
I had the need to include in one of my system's DQL queries, a subquery with LIMIT clause. As Doctrine doesn't support it, I changed it to a native query. Yet the native query is returning lower case fields instead of the correct case.
The case is that as this is working code, I had some views depending on this names and it's much harder to change all names.
But I found here http://bit.ly/1Ht1ojH, that this aspect can be configured in Doctrine. So I tried this code:
$conn = $this->getConnection();
$conn->setAttribute(Doctrine_Core::ATTR_FIELD_CASE, CASE_NATURAL);
$res = $conn->query("select MyCasedField from whatever")->fetchAll();
Yet I'm getting the error "Attempted to call method "setAttribute" on class "Doctrine\ORM\EntityManager".
I tried with manager also with same result.
Now I now I can make some code to translate the fields, but I find that the configure solution to be much more clean.
Someone knows why symfony doesn't let me configure the connection ?
Also if there is any way of using LIMIT in a DQL's subquery I would find it better.
There is no LIMIT keyword in DQL. To use this functionality you can call method setMaxResults($limit) on your query object. It is also can be applied to Query Builder.
$query = $this->getEntityManager()
->createQuery('SELECT p FROM Product p')
->setMaxResults($limit);
I want to set a variable into a DQL query on the basis of condition
case when both varibale value are matches the set variable rank.
dql query
$q=$this->em->createQuery("select distinct u.id,u.city,u.country,u.designation FROM Entities\User u JOIN Entities\EventVisitor evt_vstr WITH evt_vstr.user = u.id WHERE (
CASE
WHEN $a =u.designation THEN SET #rank=1
WHEN $b =u.country THEN SET #rank=2
WHEN $c =u.city THEN SET #rank=3
WHEN $d =u.company THEN #rank=4
END) order by #rank")
->setFirstResult($i)
->setMaxResults($max_result);
$results = $q->getResult();
Here rank is the variable i am trying to set
problem im facing
im unable to set variable in dql .
errorlog
PHP Fatal error: Uncaught exception 'Doctrine\\ORM\\Query\\QueryException' with message 'select distinct u.id, u.city,u.country,u.designation FROM Entities\\User u JOIN Entities\\EventVisitor evt_vstr WITH evt_vstr.user = u.id \n\t \t\t\t\t\tWHERE (\n\t\t\t\t\t\t\t\t\tCASE\n\t\t\t\t\t\t\t\t\t\t\t\tWHEN Co-founder =u.designation THEN SET #rank=1\n\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\t\t\tEND ) ' in /home/india/public_html/serve-trade-com/application/libraries/Doctrine/ORM/Query/QueryException.php:39\nStack
But its not working.
How can i do that ?
OR is there any way to do that
Please help
Unfortunately I don't think that's possible in DQL, but you can use just plain SQL with Doctrine. take a look at the createNativeQuery function.
If you want to use it with an Symfony2 Entity, you should also take a look at the Doctrine\ORM\Query\ResultSetMapping class, which is used to create proper Entities out of the result.
Hope this helps.
Btw. In my opinion you shouldn't use PHP variables in your DQL string, but instead work with parameters, you can read about them here.
I have two Entities with relation OneToMany, Project and Services. Now i want to remove all the services by project_id.
First attempt:
$qb = $em->createQueryBuilder();
$qb->delete('Services','s');
$qb->andWhere($qb->expr()->eq('s.project_id', ':id'));
$qb->setParameter(':id',$project->getId());
This attempt fails with the Exception Entity Service does not have property project_id. And it's true, that property does not exists, it's only in database table as foreign key.
Second attempt:
$qb = $em->createQueryBuilder();
$qb->delete('Services','s')->innerJoin('s.project','p');
$qb->andWhere($qb->expr()->eq('p.id', ':id'));
$qb->setParameter(':id',$project->getId());
This one generetate a non valid DQL query too.
Any ideas and examples will be welcome.
You're working with DQL, not SQL, so don't reference the IDs in your condition, reference the object instead.
So your first example would be altered to:
$qb = $em->createQueryBuilder();
$qb->delete('Services', 's');
$qb->where('s.project = :project');
$qb->setParameter('project', $project);
If you really can't get project object and you have to handle only with id you can use this.
Citation from doctrine documentation:
There are two possibilities for bulk deletes with Doctrine. You can either issue a single DQL DELETE query or you can iterate over results removing them one at a time. (Below I paste only first solution)
DQL Query
The by far most efficient way for bulk deletes is to use a DQL DELETE query.
Example that worked in my project
$q = $em->createQuery('delete from AcmeMyTestBundle:TemplateBlock tb where tb.template = '.intval($templateId));
$numDeleted = $q->execute();
In entity TemplateBlock I have property called template which is mapped to template_id in db.
But I agree that highly preferred way of doing it is using objects.
first of all, that's what I'm trying to do:
In one of my classes in the library I want to count the total amount of rows of a search result. The class uses a select object set by the appendant model of the search result. My problem is now, this select() has already set the requested columns by from(), but to simply count the rows I just want to select the id, because the website has to to be performant. I can't simply change the values of the object, because I'm using it in the library and the variables are protected. Unfortunately, Zend has no function for the mySql count command and I don't want to use static mySql code, because it could be, that we switch our database system in the future.
Now here's my question:
Is there any possibility by Zend_Select how I could change the selected columns?
Try this:
$select->reset(Zend_Db_Select::COLUMNS)
->from('thetable', 'COUNT(*)');
replacing the 'thetable' with the correct table name.
This is from a project and isn't tested, but one of these should work.
$select->from(array("table_name" => "table_name"), array("my_col" => "COUNT(id)"));
OR
$select->from(array("table_name"), array("my_col" => "COUNT(id)"));
This is the same as
SELECT COUNT(id) as my_col FROM table_name
Hope that helps
Jake
This one didn't work for me (I needed to select only from one joined table):
$select->reset(Zend_Db_Select::COLUMNS)
->from('thetable', 'COUNT(*)');
Maybe because I had some joins. But nevertheless, here's the solution: to use reset() and then columns():
$select->setIntegrityCheck(false)
->from(['t1' => 'table1'])
->join(['t2' => 't2'], 't1.id = t2.t1_id')
->reset(Zend_Db_Select::COLUMNS)
->columns('t1.*');
Just FYI, the version of Zend Framework is 1.12
To use a mysql command in a select, you need to use Zend_Db_Expr:
$select = $this->select()
->from('myTable', new Zend_Db_Expr('COUNT(id) as count'));
echo $select; //SELECT COUNT(id) as count FROM myTable;
I am trying to use Query Builder in Symfony2 to get some records from a database. I run the normal query in SQL and it returns the correct results. The query is
SELECT pg.name, pg.description
FROM pm_patentgroups pg
LEFT JOIN pm_portfolios pp ON pp.id = pg.portfolio_id
I want to use the exact query using Doctorine query builder in Symfony2. What I have tried so far is
$repository = $this->getDoctrine()
->getRepository('MunichInnovationGroupBundle:PmPatentgroups');
$query = $repository->createQueryBuilder('pg')
->from('pm_patentgroups', 'pg')
->leftJoin('pg','pm_portfolios','pp','pp.id = pg.portfolio_id')
->getQuery();
$portfolio_groups = $query->getResult();
but its giving me the following error:
Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::createQueryBuilder()
I am new to Symfony2 and Doctorine. Can you please tell me what is going wrong here?
Thanks
You are missing the alias when using createQueryBuilder. Since you have the repository you can drop the from portion and just use
$query = $repository->createQueryBuilder('pg')
Something like:
$qb = $this->getDoctrine()->createQueryBuilder();
$qb->addSelect('pm_patentgroups');
$qb->addSelect('pm_portfolios');
$qb->from('MunichInnovationGroupBundle:PmPatentgroups','pm_patentgroups');
$qb->leftJoin('pm_patentgroups.pm_portfolios','pm_portfolios');
This assumes you have your two entities properly related.
Lots of examples in the D2 manual. Just keep in mind that query builder works with objects, not sql.
And by the way, your error message comes from the fact that the entity repository (as opposed to the entity manager) requires an alias.