| Q | A
|------------ | -----
| Version | 1.8.1
Just wondering if I am doing something wrong or missing. I am not able to write a query to pull data from child class when I use entity repository of the parent class. Is such thing possible? If not what the best way of doing this rather than injecting the entity repository of the child class?
I get error below:
[Semantical Error] line 0, col 54 near 'location = :': Error: Class AppBundle\\Entity\\Country has no field or association named username
Thanks
ENTITY
/**
* @ORM\Entity
* @ORM\Table(name="country")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="name", type="string")
* @ORM\DiscriminatorMap({"uk"="Uk"})
*/
abstract class Country
{
private $id;
public function getId()
{
return $this->id;
}
}
/**
* @ORM\Entity
*/
class Uk extends Country
{
private $location;
public function getLocation()
{
return $this->id;
}
}
REPOSITORY
app.entity_repository.country:
class: Doctrine\ORM\EntityRepository
factory: [ '@doctrine.orm.entity_manager', getRepository ]
arguments:
- AppBundle\Entity\Country
AppBundle\Repository\CountryRepository:
arguments:
$countryEntityRepository: '@app.entity_repository.country'
class CountryRepository
{
private $countryEntityRepository;
public function __construct(
EntityRepository $countryEntityRepository
) {
$this->countryEntityRepository = $countryEntityRepository;
}
public function findOneByLocation($location)
{
return $this->countryEntityRepository
->createQueryBuilder('c')
->where('c.location = :location')
->setParameters(['location' => $location])
->getQuery()
->getOneOrNullResult(Query::HYDRATE_SIMPLEOBJECT);
}
}
I can solve the problem by injecting other entity repos like below and use the relevant one while using query builder but this wouldn't be scalable as you will guess!
public function __construct(
EntityRepository $countryEntityRepository,
EntityRepository $ukEntityRepository,
EntityRepository $deEntityRepository,
EntityRepository $frEntityRepository,
EntityRepository $itEntityRepository,
....
) {
}
@BentCoder what's version 1.8.1? Can you check the ORM version in use?
Class AppBundle\\Entity\\Country has no field or association named username
Can you check your example? Did you mean location here?
It is indeed correct that the CountryReposiory can't filter by location, since there is no location field in a country. You can only filter by Uk.location.
@Ocramius
doctrine/orm 2.6.1
doctrine/doctrine-bundle 1.8.1
Likely a duplicate of https://github.com/doctrine/doctrine2/issues/2237
I've read that one and this one but I didn't really get what I meant to change in my example. If you don't mind, could you please tell me what I should change/use?
@BentCoder you'd need to start selecting from the $ukEntityRepository, or you can design the query differently (but in a more inefficient way):
SELECT c
FROM Country c
WHERE c.id IN (
SELECT uk.id
FROM Uk uk
WHERE uk.location = :location
)
you'd need to start selecting from the
$ukEntityRepository
This is currently what I am doing but as you will guess it will potentially lead me to inject many EntityRepository parameters as my countries grow. It is OK for now because I have only uk and fr but more on the way so this is not an option for me.
you can design the query differently (but in a more inefficient way)
Looks like I have no option but go for this one.
Note: I think it would be perfect for everyone in future if we could just inject/use EntityRepository $countryEntityRepository (parent) and query it (just to prevent injecting/using children).
Note: I think it would be perfect for everyone in future if we could just inject/use
EntityRepository $countryEntityRepository(parent) and query it (just to prevent injecting/using children).
I've already stated a clear NO in #2237 (DDC-16 - to give you an idea of how old this issue is) about that.
Most helpful comment
I've already stated a clear NO in #2237 (DDC-16 - to give you an idea of how old this issue is) about that.