-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DoctrineBridge] UniqueEntity: Support a ManyToOne related entity as "field" #58282
Comments
Did you try specifying the name of the column directly? It should work. #[UniqueEntity(fields: ['parent_id', 'name'])] |
When looking at the initial error message again:
What I don't understand is that the constraint is obviously looking for a #[UniqueEntity(fields: ['position'])] ... I'm getting:
So it looks like in this case |
Aren't you just missing the |
No, mapping is fine. Both entities are getting persisted to the database for years; just wanted to add |
Ah, sorry. I thought we we're talking about This should work for you, but uses a different constraint: #[UniqueConstraint(columns: ['parent_id', 'name'])] |
Yeah, sure, I already have |
PR welcome I guess :) |
I am have been using this constraint for years with ManyToOne relations and it is working great, so there must be some sort of issue with the Doctrine mapping here. Hard to say without a reproducer... |
@ThomasLandauer Can you create a small example application that allows to reproduce the behaviour that you observe? |
@bobvandevijver Could you show me your syntax please? |
While copying the stuff below, looks like you might be missing the Sure, I have for example: namespace App\Entity\Equipment;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity]
#[ORM\UniqueConstraint(fields: ['equipment', 'name'])]
#[UniqueEntity(fields: ['equipment', 'name'])]
class EquipmentConsumable
{
#[ORM\Column(name: 'id')]
#[ORM\Id]
#[ORM\GeneratedValue]
protected ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'consumables')]
#[ORM\JoinColumn(nullable: false)]
#[Assert\NotNull]
private ?Equipment $equipment = null;
#[ORM\Column(length: 50)]
protected string $name = '';
} namespace App\Entity\Equipment;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: EquipmentRepository::class)]
class Equipment
{
#[ORM\Column(name: 'id')]
#[ORM\Id]
#[ORM\GeneratedValue]
protected ?int $id = null;
/** @var Collection<int, EquipmentConsumable> */
#[ORM\OneToMany(mappedBy: 'equipment', targetEntity: EquipmentConsumable::class, fetch: 'EXTRA_LAZY')]
private Collection $consumables; // Default in constructor
public function __construct()
{
$this->consumables = new ArrayCollection();
}
} |
In my real entities I do have
(The second error is strange, cause So one more question before I sit down and create a reproducer :-) BTW: I think your |
The database out of sync shouldn't really matter, but it is still worth looking into that. Using
Yes, the default "This value is already used." is shown as it expected.
That is because I simplified the definition here by moving my trait inline and forgot to remove that remnant. |
Sorry guys, my fault! #[ORM\Entity(repositoryClass: ParentRepository::class)] Actually, in hindsight, the problem would have been quite obvious from the error message:
Anyway, now I'm wondering why the entire app has been working at all... ;-) So I'm closing here - thanks everybody! BTW: The apparently out-of-sync database is a known issue: doctrine/migrations#1406 |
Description
I would like to be able to set up
UniqueEntity
so that $name must be unique for each $parent.Example
Right now, this throws an
Doctrine\ORM\Persisters\Exception\UnrecognizedField
:In the parent class, I'm having:
And I'm submitting the data with a
CollectionType
.The text was updated successfully, but these errors were encountered: