Doctrine querybuilder difference in initialisation? - doctrine-orm

I noticed that in tutorials there are different ways of instantiating the querybuilder.
1. $em->getRepository("Users")->createQueryBuilder('u')
2. $em->createQueryBuilder();
Is there a significant difference in using it with or without repository?

QueryBuilder in the repository will preset the from and select parts for you whereas the second one creates an empty QueryBuilder.

Related

How to use ROLLUP operator in ActiveRecord

I am making a mutli-series line-graph and I want to have a series for the data overall. At first, I thought I would need two queries to retrieve the data: one query for overall, and one query for the data sorted by groups. But then I learned MySQL has this functionality built in through an operator called ROLLUP that you can add to your GROUP BY clause. Unfortunately, after lots of googling, I couldn't find any working examples. Does this functionality exist in ActiveRecord 4.2? If so, how can I use it?
Note that my query averages the data before the DB returns it so I cannot simply run one query and have my app run more computations on the result to get the overall value.
After further investigation, it seems ActiveRecord simply does not support the ROLLUP operator. Does RAILS have GROUP BY...WITH ROLLUP query? offered a solution but it didn't work for me nor my coworkers.

Doctrine 2. query builder. association INSTANCE OF type

I have the following request on DQL, using Doctrine 2 Query Builder, which works well:
$qb->select('post')
->from('Posts\Entity\Post','post')
->join('post.author','author')
->where('author INSTANCE OF :utype')
->setParameter('utype',$userType);
But, considering that this example reflects a part of large query, I'd like to get rid of this join. I tried this:
$qb->select('post')
->from('Posts\Entity\Post','post')
->where('post.author INSTANCE OF :utype')
->setParameter('utype',$userType);
but it doesn't work.
How can I avoid using join? Or maybe there is other ways to optimize such query?
Thank's
I know this is an old question, but the answer is still missing.
The instance name can not be set as string via parameters.
It could be written like this:
$qb->select('post')
->from('Posts\Entity\Post','post')
->where('post.author INSTANCE OF '.UserType::class);
or like this
$qb->select('post')
->from('Posts\Entity\Post','post')
->where('post.author INSTANCE OF :utype')
->setParameter('utype', $entityManager->getClassMetadata(UserType::class));
#see: https://github.com/doctrine/orm/issues/6483

Doctrine 2.2.2 Annotated Database table relationship

I am so new to Doctrine 2 and not that good with regards to database table relationships. I understand the way of how Many to One, One to Many, One to One and Many to Many relationship works. I am just confused with this error I got below from generating database tables using the doctrine CLI.
[Doctrine\ORM\ORMException]
Column name `id` referenced for relation from Kent\Entity\DoctorSpecialties towards Kent\Entity\Doctors does not exist.
Please see this github link to view my entities. (Doctors.php & DoctorSpecialties.php)
https://github.com/aldee07/Doctor-Finder/tree/development-september/library/Kent/Entity
Note: I am using Zend Framework 1.11.12
That are the annotations you must put on DoctorSpecialties class
#ManyToOne(targetEntity="Doctors")
#JoinColumn(name="doctor_id", referencedColumnName="doctor_id")
in Many to many relationship you have to give the reference to the third forignkey table
so change this
/**
*
* #var Doctors
* #ManyToMany(targetEntity="Doctors", mappedBy="specialties")
* #JoinTable(name="doctor_specialties")
*/
protected $doctors
hope this helps
I think is better you look here:
http://www.doctrine-project.org/jira/browse/DDC-2646?page=com.atlassian.jira.plugin.system.issuetabpanels:changehistory-tabpanel

doctrine: QueryBuilder vs createQuery?

In Doctrine you can create DQL in 2 ways:
EntityManager::createQuery:
$query = $em->createQuery('SELECT u FROM MyProject\Model\User u WHERE u.id = ?1');
QueryBuilder:
$qb->add('select', 'u')
->add('from', 'User u')
->add('where', 'u.id = ?1')
->add('orderBy', 'u.name ASC');
I wonder what the difference is and which should I use?
DQL is easier to read as it is very similar to SQL. If you don't need to change the query depending on a set of parameters this is probably the best choice.
Query Builder is an api to construct queries, so it's easier if you need to build a query dynamically like iterating over a set of parameters or filters. You don't need to do any string operations to build your query like join, split or whatever.
Query builder is just, lets say, interface to create query... It should be more comfortable to use, it does not have just add() method, but also methods like where(), andWhere(), from(), etc. But in the end, it just composes query like the one you use in the createQuery() method.
Example of more advanced use of query builder:
$em->createQueryBuilder()
->from('Project\Entities\Item', 'i')
->select("i, e")
->join("i.entity", 'e')
->where("i.lang = :lang AND e.album = :album")
->setParameter('lang', $lang)
->setParameter('album', $album);
They have different purposes:
DQL is easier to use when you know your full query.
Query builder is smarter when you have to build your query based on some conditions, loops etc.
The main difference is the overhead of calling the methods. Your first code sample (createQuery) just for simplicity makes one method call, while the the queryBuilder makes 4. At the end of everything, they come down to a string that has to be executed, first example you are giving it the string, and the other you are building it with multiple chained method calls.
If you are looking for a reason to use one over the other, that is a question of style, and what looks more readable. For me, I like the queryBuider most of the time, it provides well defined sections for the query. Also, in the past it makes it easier to add in conditional logic when you need it.
It might be easier to unit test when using the query builder. Let's say you have a repository that queries for some data basing on the complicated list of conditions. And you want to assure that if a particular condition is passed into the repository, some other conditions are added into the query. In case of DQL you have two options:
1) To use fixtures and test the real interaction with DB. Which I find somewhat troublesome and ununitestish.
2) To check the generated DQL code. Which can make your test too fragile.
With QueryBuilder, you can substitute it with mock and verify that "andWhere" method with needed parameter is called. Of course such considerations are not applicable if your query is simple and not depended on any parameters.

subsonic 3.0 creates a byte array for a tinyint sql column

I was trying to use Subsonic to generate my DAL, but I noticed a bug while generating Classes using LinqToEntities T4 templates.
I noticed that one of the columns in a table was having the type tinyint, while generating a Property for this column it created one which returns a byte[] instead of a byte.
Also another Table had a Column named ModifiedBy with the type set to smallint. Subsonic created the Property in the class correctly as short however in one of the PropertyChanged methods it was expecting a string for this field and was setting its value to Environment.UserName.
Has anyone faced this issue before?
TIA..
you may have solved this by now but..
Search in the file SQLServer.ttinclude for "tinyint"
You'll see a C# switch statement
Modify the one for tinyint to something more useful, such as Integer
Worked for me