Inheritance mapping: Entity has no field or association named username #6042

Closed
opened 2026-01-22 15:25:19 +01:00 by admin · 8 comments
Owner

Originally created by @bentcoder on GitHub (Aug 21, 2018).

Originally assigned to: @Ocramius on GitHub.

Q A
Version 1.8.1

Support Question

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,
    ....
) {
}
Originally created by @bentcoder on GitHub (Aug 21, 2018). Originally assigned to: @Ocramius on GitHub. <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ----- | Version | 1.8.1 ### Support Question 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, .... ) { }
admin added the DuplicateQuestion labels 2026-01-22 15:25:19 +01:00
admin closed this issue 2026-01-22 15:25:19 +01:00
Author
Owner

@Ocramius commented on GitHub (Aug 21, 2018):

@BentCoder what's version 1.8.1? Can you check the ORM version in use?

@Ocramius commented on GitHub (Aug 21, 2018): @BentCoder what's version 1.8.1? Can you check the ORM version in use?
Author
Owner

@Ocramius commented on GitHub (Aug 21, 2018):

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 commented on GitHub (Aug 21, 2018): ``` 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`.
Author
Owner

@bentcoder commented on GitHub (Aug 21, 2018):

@Ocramius
doctrine/orm 2.6.1
doctrine/doctrine-bundle 1.8.1

@bentcoder commented on GitHub (Aug 21, 2018): @Ocramius `doctrine/orm 2.6.1` `doctrine/doctrine-bundle 1.8.1`
Author
Owner

@Ocramius commented on GitHub (Aug 21, 2018):

Likely a duplicate of https://github.com/doctrine/doctrine2/issues/2237

@Ocramius commented on GitHub (Aug 21, 2018): Likely a duplicate of https://github.com/doctrine/doctrine2/issues/2237
Author
Owner

@bentcoder commented on GitHub (Aug 21, 2018):

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 commented on GitHub (Aug 21, 2018): I've read that one and [this](https://github.com/doctrine/doctrine2/issues/5998) 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?
Author
Owner

@Ocramius commented on GitHub (Aug 21, 2018):

@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
)
@Ocramius commented on GitHub (Aug 21, 2018): @BentCoder you'd need to start selecting from the `$ukEntityRepository`, or you can design the query differently (but in a more inefficient way): ```sql SELECT c FROM Country c WHERE c.id IN ( SELECT uk.id FROM Uk uk WHERE uk.location = :location ) ```
Author
Owner

@bentcoder commented on GitHub (Aug 21, 2018):

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).

@bentcoder commented on GitHub (Aug 21, 2018): > 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).
Author
Owner

@Ocramius commented on GitHub (Aug 21, 2018):

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.

@Ocramius commented on GitHub (Aug 21, 2018): > **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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6042