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

Open
opened 2026-01-22 15:25:15 +01:00 by admin · 0 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:15 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6041