I have this query below:
SELECT *
FROM mydb.users
left join mydb.jobs
on users.user_id = jobs.job_id;
And I used to convert them in orm query just like below:
return
$qb = $this->entityManager->createQueryBuilder();
$qb->select('rb', 'l')
->from('Admin\Entity\Users', 'rb')
->leftJoin(
'Admin\Entity\Jobs',
'l',
\Doctrine\ORM\Query\Expr\Join::WITH,
'rb.user_id = l.job_id'
)
->getQuery()
->getResult(AbstractQuery::HYDRATE_ARRAY);
But it doesn's still work. I get the following error:
PHP Fatal error: Call to a member function createQueryBuilder() on null
please help i don't know what to do.
Try this
$entityManager = $this->serviceLocator->get('Doctrine\ORM\EntityManager');
$qb = $entityManager->createQueryBuilder();
Or make sure $this->entityManager is assigned.
Well it simply means you don't have an EntityManager instance in $this->entityManager.
My guess would be that $this->entityManager is probably null because you did not inject an EntityManager inside your class instance.
Related
I'm using Doctrine's querybuilder in Symfony2 to create a query to fetch entities.
I'm trying to get this result in MySQL :
SELECT u0_.id AS id0
FROM user u0_
LEFT JOIN rva_victims r3_ ON u0_.id = r3_.user_id
INNER JOIN rva r1_ ON r1_.id = r3_.rva_id or u0_.id = r1_.declarant_id
I've tried both ON and WITH conditionType in $qb->join(), but nothing work.
Any ideas to solve my problem?
The solution should be pretty straightforward. In your case it should look like this, assuming you're in the repository class:
$this
->createQueryBuilder('u')
->leftJoin(Victims::class, 'v', Query\Expr\Join::WITH, 'v.user = u.id')
->join(Rva::class, 'r', Query\Expr\Join::WITH, 'r.id = v.rva OR u.id = r.declarant');
This should work fine, I am just assuming the class names right now. Please also take in account that all the conditions are done on class attribute names (DQL), not column names.
You can do this by creating a query builder in entity repository
$qb = $this->createQueryBuilder('u')
->leftJoin('yourbundle:Entityname', 'ye', 'WITH', 'u0_.id = r3_.user_id')
->innerJoin('yourbuncle:entityname', 'yen', 'WITH', 'r1_.id = r3_.rva_id', 'OR', 'u0_.id = r1_.declarant_id');
I'm using Symfony2 and i'm trying to get an array of languages not associated to a specified client.
I have a Client entity indicating the Client, ClientLanguage that has the following structure:
id_menu_language PRIMARY KEY
language the association with the Language entity
client the association with the Client entity
sequence tells the order the language should be shown (not used here)
and Language Entity.
To get an array of languages not associated to the client i want to proceed in the following way:
Get the languages that the client has already associated (and I'm getting the correct DQL in the $clientLanguagesDQL variable)
Retrieve a list of all the available languages
Exclude from that list all the languages already associated to the client (by using NOT IN (...) ).
This is the function I wrote to accomlish that:
<?php
namespace AppBundle\Repository;
use AppBundle\Entity\Client;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join;
class ClientRepository extends EntityRepository
{
/**
* #param $client Client to check
* #return array
*/
public function getLanguagesNotAssociatedToClient($client)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$clientLanguagesDQL = $qb
->select('lang')
->from('AppBundle:Language', 'lang')
->join('AppBundle:ClientLanguage', 'languages_assoc', Join::WITH, 'languages_assoc.language = lang')
->join('AppBundle:Client', 'client', Join::WITH, 'languages_assoc.client = client')
->where('client.idClient = :client_id')
->getQuery()
->getDQL();
$languages = $qb->select('language')
->from('AppBundle:Language', 'language')
->where($qb->expr()->notIn('language', $clientLanguagesDQL))
->setParameter('client_id', $client->getIdClient())
->getQuery()
->getResult();
return $languages;
}
}
However, when I run this, Symfony complains about a : [Semantical Error] line 0, col 293 near 'lang INNER JOIN': Error: 'lang' is already defined. It also tells me that there is a QueryException, and it shows me the following query:
SELECT language
FROM AppBundle:Language lang
INNER JOIN AppBundle:ClientLanguage languages_assoc
WITH languages_assoc.language = lang
INNER JOIN AppBundle:Client client
WITH languages_assoc.client = client, AppBundle:Language language WHERE language NOT IN(
SELECT lang
FROM AppBundle:Language lang
INNER JOIN AppBundle:ClientLanguage languages_assoc
WITH languages_assoc.language = lang
INNER JOIN AppBundle:Client client
WITH languages_assoc.client = client
WHERE client.idClient = :client_id
)
And this is definitely not what I want to do. Why there appeared to be two joins with AppBundle:ClientLanguage and AppBundle:Client? I use this association only in my first subquery.
If it can help, when I run this:
$clientLanguagesDQL = $qb
->select('lang')
->from('AppBundle:Language', 'lang')
->join('AppBundle:ClientLanguage', 'languages_assoc', Join::WITH, 'languages_assoc.language = lang')
->join('AppBundle:Client', 'client', Join::WITH, 'languages_assoc.client = client')
->where('client.idClient = :client_id')
->getQuery()
->getDQL();
This is the returned DQL stored in $clientLanguageDQL:
SELECT lang
FROM AppBundle:Language lang
INNER JOIN AppBundle:ClientLanguage languages_assoc
WITH languages_assoc.language = lang
INNER JOIN AppBundle:Client client
WITH languages_assoc.client = client
WHERE client.idClient = :client_id
What's wrong with this query?
My mistake was very stupid, all I had to do is creating a new query builder for each query instead of reusing the first one:
$qb1 = $this->getEntityManager()->createQueryBuilder();
$clientLanguagesDQL = $qb1
->select('lang')
->from('AppBundle:Language', 'lang')
->join('AppBundle:ClientLanguage', 'languages_assoc', Join::WITH, 'languages_assoc.language = lang')
->join('AppBundle:Client', 'client', Join::WITH, 'languages_assoc.client = client')
->where('client.idClient = :client_id')
->getQuery()
->getDQL();
$qb2 = $this->getEntityManager()->createQueryBuilder();
$languages = $qb2->select('language')
->from('AppBundle:Language', 'language')
->where($qb2->expr()->notIn('language', $clientLanguagesDQL))
->setParameter('client_id', $client->getIdClient())
->getQuery()
->getResult();
return $languages;
I have the following query I'm executing in Symfony2 Repository. The query looks like
$q = $this
->createQueryBuilder('m')
->select(array('m.reciever','m.created','m.id','m.subject'))
->where('m.reciever = ?1')
->orderBy('m.id','DESC')
->setMaxResults( '?2' )
->setFirstResult( '?3' )
->setParameter(1,$id)
->setParameter(2,$itemsPerPage)
->setParameter(3,$offset)
->getQuery();
Where reciever, created, id, and subject are fields part of my message entity. I do not need to specify which entity I am selecting from. The error I keep getting is such...
[Semantical Error] line 0, col 12 near 'reciever, m.created,': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
I'm not sure what a state field path expression is or what the syntax might be. It seems like everything should be right.
When you use the select() method, you override the default one which is in $this->createQueryBuilder('m') . That's why you lost the m alias. To avoide this, use an addSelect() or specify the alias in the from() method:
->from('Bundle:Entity', 'ALIAS')
Do you have something like this:=?
$q = $this
->createQueryBuilder()
->select('m.reciever, m.created ,m.id , m.subject')
->from('/Acme/Entity/DemoEntity', 'm')
->where('m.reciever = ?1')
->orderBy('m.id','DESC')
->setMaxResults( '?2' )
->setFirstResult( '?3' )
->setParameter(1,$id)
->setParameter(2,$itemsPerPage)
->setParameter(3,$offset)
->getQuery();
Im not sure but I think you use the array syntax only if you have multiple tables to join together: array('m.reciever, m.created', 'p.user, p.id').
i hope this will help u..
$em = $this->getDoctrine()->getManager();
$q = $em->createQueryBuilder()
->select('m.reciever,m.created,m.id,m.subject')
->from('bundle:entity','m')
->where('m.reciever = ?1')
->orderBy('m.id','DESC')
->setMaxResults( '?2' )
->setFirstResult( '?3' )
->setParameter(1,$id)
->setParameter(2,$itemsPerPage)
->setParameter(3,$offset)
->getQuery();
I have two entities which are connected through a 1:1 relationship, e.g: MyEntity.idRelatedEntity
I want to create a Doctrine query where I can retrieve data from MyEntity depending on a value from a certain column in RelatedEntity. Something like this (it doesn't work of course):
$entity = $em
->getRepository('MyBundle:RelatedEntity')
->createQueryBuilder('e')
->leftJoin('MyBundle:RelatedEntity', 'r')
->where('r.foo = 1')
->getQuery()
->getResult();
Any help would be much appreciated :)
$entity = $em
->getRepository('MyBundle:MyEntity')
->createQueryBuilder('e')
->join('e.idRelatedEntity', 'r')
->where('r.foo = 1')
->getQuery()
->getResult();
Also left join makes no sense here (because of where clause that will make it work like inner join)
Note that you should write this query in your MyEntityRepository
public function getMyEntityWithRelatedEntity($parameter)
{
$query = $this->createQueryBuilder('e')
->addSelect('r') // to make Doctrine actually use the join
->leftJoin('e.relatedEntity', 'r')
->where('r.foo = :parameter')
->setParameter('parameter', $parameter)
->getQuery();
return $query->getResult();
}
And then use it in your controller/service :
$manager = $this->getDoctrine()->getManager();
$results = $manager->getRepository(MyEntity::class)->getMyEntityWithRelatedEntity(1);
everyone.
I have 2 entities City and POI. Mapping looks like this:
class City {
/**
* #ORM\ManyToMany(targetEntity="POI", mappedBy="cities")
* #ORM\OrderBy({"position" = "ASC"})
*/
protected $pois;
and
class POI {
/**
* #ORM\ManyToMany(targetEntity="City", inversedBy="pois")
* #ORM\JoinTable(name="poi_cities")
*/
protected $cities;
I would like to fetch all POIs that have at least 1 association with some City using QueryBuilder. I should probably use exists() function but I don't quiet know how.
You'd have to Left join them and check if cities is null.
$qb->select('p', 'c')
->from('AcmeDemoBundle:POI', 'p')
->leftJoin('p.cities', 'c')
->where('c IS NOT NULL');
I haven't tested it, but I hope it gives you the general direction. You can read more about the QueryBuilder from here.
Docrine2 was changed in 2013, so the other solution displays error Error: Cannot add having condition on a non result variable. Now we cannot use joined alias just as a condition variable. We should use any of its properties like c.id
So you should fix the code to
$qb->select('p', 'c')
->from('AcmeDemoBundle:POI', 'p')
->leftJoin('p.cities', 'c')
->where('c.id IS NOT NULL');
$results = $qb->getQuery()->execute();
If you want to select entities that does not have any cities, use IS NULL.
$qb->leftJoin('p.cities', 'city')
->where('city.id IS NULL')
->getQuery()
->execute();
Description of a problem and link to the commit that responsible for that - http://www.doctrine-project.org/jira/browse/DDC-2780