Entity name with namespace change mapping #5383

Closed
opened 2026-01-22 15:06:03 +01:00 by admin · 6 comments
Owner

Originally created by @mirekratman on GitHub (Jan 13, 2017).

Originally assigned to: @Ocramius on GitHub.

When I create a query builder by using entity classname like:

$this->entityManager
            ->getRepository($entity->getClassName())
            ->createQueryBuilder($entity->getClassName())`
$entity->getClassName();//   ->   Cds\\Library\\Mysql\\Entity\\CdsDomain

and add a ordering

$dql->orderBy(
                sprintf(
                    '%s.%s',
                    $entity->getClassName,
                    $somestring
                ),
                $order->getParam('dir', 'string')
            );

doctrine create the query (example)

SELECT c0_.name AS name_0, c0_.id AS id_1, c0_.safeDelete AS safeDelete_2, c0_._created AS _created_3, c0_._modified AS _modified_4, c0_._deleted AS _deleted_5, c0_.domain AS domain_6 FROM CdsDomain c0_ WHERE c0_.safeDelete <> 1 ORDER BY c0_.id ASC LIMIT 10

when will use entity classname without namespace

$entity->getClassName();//   ->   CdsDomain

query looks:

'SELECT c0_.name AS name_0, c0_.id AS id_1, c0_.safeDelete AS safeDelete_2, c0_._created AS _created_3, c0_._modified AS _modified_4, c0_._deleted AS _deleted_5, c0_.server AS server_6 FROM CdsDomain c0_ WHERE c0_.safeDelete <> 1 LIMIT 10

difference is

c0_.domain AS domain_6` instead of right `c0_.server AS server_6

Annotation mapping look like this

    /**
     * @ORM\ManyToOne(targetEntity="Cds\Library\Mysql\Entity\CdsServer", inversedBy="domain", fetch="EAGER")
     * @ORM\JoinColumn(name="server", referencedColumnName="id", nullable=false)
     */
    protected $server;
Originally created by @mirekratman on GitHub (Jan 13, 2017). Originally assigned to: @Ocramius on GitHub. When I create a query builder by using entity classname like: ```php $this->entityManager ->getRepository($entity->getClassName()) ->createQueryBuilder($entity->getClassName())` ``` ```php $entity->getClassName();// -> Cds\\Library\\Mysql\\Entity\\CdsDomain ``` and add a ordering ```php $dql->orderBy( sprintf( '%s.%s', $entity->getClassName, $somestring ), $order->getParam('dir', 'string') ); ``` doctrine create the query (example) ```sql SELECT c0_.name AS name_0, c0_.id AS id_1, c0_.safeDelete AS safeDelete_2, c0_._created AS _created_3, c0_._modified AS _modified_4, c0_._deleted AS _deleted_5, c0_.domain AS domain_6 FROM CdsDomain c0_ WHERE c0_.safeDelete <> 1 ORDER BY c0_.id ASC LIMIT 10 ``` when will use entity classname without namespace ```php $entity->getClassName();// -> CdsDomain ``` query looks: ```sql 'SELECT c0_.name AS name_0, c0_.id AS id_1, c0_.safeDelete AS safeDelete_2, c0_._created AS _created_3, c0_._modified AS _modified_4, c0_._deleted AS _deleted_5, c0_.server AS server_6 FROM CdsDomain c0_ WHERE c0_.safeDelete <> 1 LIMIT 10 ``` difference is ```sql c0_.domain AS domain_6` instead of right `c0_.server AS server_6 ``` Annotation mapping look like this ```php /** * @ORM\ManyToOne(targetEntity="Cds\Library\Mysql\Entity\CdsServer", inversedBy="domain", fetch="EAGER") * @ORM\JoinColumn(name="server", referencedColumnName="id", nullable=false) */ protected $server; ```
admin added the BugIncompleteMissing Tests labels 2026-01-22 15:06:03 +01:00
admin closed this issue 2026-01-22 15:06:03 +01:00
Author
Owner

@Ocramius commented on GitHub (Jan 13, 2017):

@mirekratman I don't understand "when will use entity classname without namespace". What do you mean by "use" here? What is the $entity->getClassName() call?

@Ocramius commented on GitHub (Jan 13, 2017): @mirekratman I don't understand `"when will use entity classname without namespace"`. What do you mean by "use" here? What is the `$entity->getClassName()` call?
Author
Owner

@mirekratman commented on GitHub (Jan 13, 2017):

@Ocramius Hi, $entity->getClassName() just return classname of entity simply using

return (string)get_class($this); which returns Cds\\Library\\Mysql\\Entity\\CdsDomain

Regarding - "when will use" - wanted to say that problem happens when method ->orderBy() gets as a $sort parameter strng which contains classname with namespace Cds\\Library\\Mysql\\Entity\\CdsDomain instead of a simple string like CdsDomain.

I will prepare a simple entities and service to show the problem and will share a link here.

@mirekratman commented on GitHub (Jan 13, 2017): @Ocramius Hi, $entity->getClassName() just return classname of entity simply using `return (string)get_class($this);` which returns `Cds\\Library\\Mysql\\Entity\\CdsDomain` Regarding - "when will use" - wanted to say that problem happens when method ->orderBy() gets as a $sort parameter strng which contains classname with namespace `Cds\\Library\\Mysql\\Entity\\CdsDomain` instead of a simple string like `CdsDomain`. I will prepare a simple entities and service to show the problem and will share a link here.
Author
Owner

@lcobucci commented on GitHub (Jan 13, 2017):

@mirekratman but why are you using the class name as alias name on the query builder?

@lcobucci commented on GitHub (Jan 13, 2017): @mirekratman but why are you using the class name as alias name on the query builder?
Author
Owner

@mirekratman commented on GitHub (Jan 13, 2017):

@lcobucci good question - I can use any string like abc of course, I've just used classname.
Strange thing is Ive already did a separate unit tests but there I cannot simulate the issue yet.
In my project problem still exists - thats Ive also double checking my project.
Only one thing I have in mind now - maybe it has something very specific to do with parsing a query.

@mirekratman commented on GitHub (Jan 13, 2017): @lcobucci good question - I can use any string like `abc` of course, I've just used classname. Strange thing is Ive already did a separate unit tests but there I cannot simulate the issue yet. In my project problem still exists - thats Ive also double checking my project. Only one thing I have in mind now - maybe it has something very specific to do with parsing a query.
Author
Owner

@mirekratman commented on GitHub (Jan 13, 2017):

The unit test (copy of the entities/repositories and method from service) can be fount here - https://github.com/mirekratman/tests - it passes corectly the test. The method with existing issue in my project is located in $this->domainService->getWrong(....). Still looking how to replicate this.

@mirekratman commented on GitHub (Jan 13, 2017): The unit test (copy of the entities/repositories and method from service) can be fount here - https://github.com/mirekratman/tests - it passes corectly the test. The method with existing issue in my project is located in $this->domainService->getWrong(....). Still looking how to replicate this.
Author
Owner

@mirekratman commented on GitHub (Jan 24, 2017):

Because Ive tried many times to replicate this issue and I couldnt do it, please close the ticket.
When I will find what is the problem then share more info in new ticket.
Thank you

@mirekratman commented on GitHub (Jan 24, 2017): Because Ive tried many times to replicate this issue and I couldnt do it, please close the ticket. When I will find what is the problem then share more info in new ticket. Thank you
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5383