Use fieldNames instead of fieldMappings, if columnPrefix=false #6078

Closed
opened 2026-01-22 15:26:23 +01:00 by admin · 1 comment
Owner

Originally created by @waahhhh on GitHub (Sep 27, 2018).

Originally assigned to: @Ocramius on GitHub.

Q A
Version 2.6.2

Intro

Example:
Embeddable :

<?php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Embeddable
 */
class Price
{
    /**
     * @var string
     * @Serializer\Type("string")
     * @ORM\Column (name="price", type="string", length=20, nullable=false)
     */
    private $price = '';
}

Entity:

<?php

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table
 * @ORM\Entity
 */
class Article
{
    /**
     * @var Price
     * @ORM\Embedded(class="Price", columnPrefix=false)
     */
    private $price;
}

An Query:

<?php
$repo = $em->getRepository(Article::class);
$articles = $repo->findBy(['price.price' => '10']);

My recommendation:

<?php
$repo = $em->getRepository(Article::class);
$articles = $repo->findBy(['price' => '10']);

Summary

When using columnPrefix=false the column is named price instead of price_price.
So it is not possible to have multiple price columns.

In \Doctrine\ORM\Persisters\Entity\BasicEntityPersister::getSelectConditionStatementColumnSQL() the Mapping $this->class->fieldMappings is used, which knows the column price as price.price.
In $this->class->fieldNames the column price is mapped by the index price and the value price.price. I would suggest to use fieldNames in case of columnPrefix=false instead.

Originally created by @waahhhh on GitHub (Sep 27, 2018). Originally assigned to: @Ocramius on GitHub. | Q | A |------------ | ----- | Version | 2.6.2 #### Intro **Example:** Embeddable : ```php <?php use Doctrine\ORM\Mapping as ORM; /** * @ORM\Embeddable */ class Price { /** * @var string * @Serializer\Type("string") * @ORM\Column (name="price", type="string", length=20, nullable=false) */ private $price = ''; } ``` Entity: ```php <?php use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table * @ORM\Entity */ class Article { /** * @var Price * @ORM\Embedded(class="Price", columnPrefix=false) */ private $price; } ``` An Query: ```php <?php $repo = $em->getRepository(Article::class); $articles = $repo->findBy(['price.price' => '10']); ``` My recommendation: ```php <?php $repo = $em->getRepository(Article::class); $articles = $repo->findBy(['price' => '10']); ``` #### Summary When using `columnPrefix=false` the column is named `price` instead of `price_price`. So it is not possible to have multiple `price` columns. In `\Doctrine\ORM\Persisters\Entity\BasicEntityPersister::getSelectConditionStatementColumnSQL()` the Mapping `$this->class->fieldMappings` is used, which knows the column `price` as `price.price`. In `$this->class->fieldNames` the column `price` is mapped by the index `price` and the value `price.price`. I would suggest to use `fieldNames` in case of `columnPrefix=false` instead.
admin added the Invalid label 2026-01-22 15:26:23 +01:00
admin closed this issue 2026-01-22 15:26:23 +01:00
Author
Owner

@Ocramius commented on GitHub (Sep 27, 2018):

When using columnPrefix=false the column is named price instead of price_price.
So it is not possible to have multiple price columns.

This is correct and expected.

<?php
$repo = $em->getRepository(Article::class);
$articles = $repo->findBy(['price.price' => '10']);

This is also correct and expected: $repo->findBy(['price' => new Price()]) would be the correct approach if the sub-field is not used, but this is not supported.

@Ocramius commented on GitHub (Sep 27, 2018): > When using `columnPrefix=false` the column is named `price` instead of `price_price`. So it is not possible to have multiple `price` columns. This is correct and expected. ```php <?php $repo = $em->getRepository(Article::class); $articles = $repo->findBy(['price.price' => '10']); ``` This is also correct and expected: `$repo->findBy(['price' => new Price()])` would be the correct approach if the sub-field is not used, but this is not supported.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6078