Skip to content
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

Unable to save entity with CollectionType if unique constraint and reorder #17960

Closed
Seb33300 opened this issue Feb 29, 2016 · 2 comments
Closed

Comments

@Seb33300
Copy link
Contributor

For instance, we have 2 entities: Website and Domain.
Website have a OneToMany relation to Domain:

@ORM\OneToMany(targetEntity="Domain", mappedBy="domain", cascade={"all"}, orphanRemoval=true)
@Assert\Valid

And Domain have a ManyToOne relation and a unique constraint on the domain name field:

@UniqueEntity("name", message="Domain already exists.")

@ORM\Column(name="name", type="string", unique=true)
@Assert\NotBlank(message = "Domain required.")
$name

@ORM\ManyToOne(targetEntity="Website", inversedBy="domains")
@ORM\JoinColumn(name="website_id", referencedColumnName="id", nullable=false)
@Assert\NotBlank(message = "Website required.")
$website

I have 2 forms: WebsiteType and DomainType.
WebsiteType have a CollectionType of DomainType:

$builder
    ->add('domains', CollectionType::class, array(
        'entry_type'   => DomainType::class,
        'allow_add'    => true,
        'allow_delete' => true,
        'by_reference' => false,
    ))
;

I can save a new website with domains automatically created.
I can update website and domains in the same form too.

The problem is on an update, if we do not send domains in the same order then they have been saved, it will raise an error on the Domain unique constraint.

Example:
Create a new website with 2 domains:

  • domain[0][name] = domain1.com
  • domain[1][name] = domain2.com

Save it: OK

Update the website on the same form (changing domain order):

  • domain[0][name] = newdomain.com
  • domain[1][name] = domain1.com

Save it: ERROR: Unique constraint on domain name, domain1.com already exist

Update the website on the same form (without changing order):

  • domain[0][name] = domain1.com
  • domain[1][name] = newdomain.com

Save it: OK

@HeahDude
Copy link
Contributor

Hi @Seb33300, I think it's a duplicate of #7468

@javiereguiluz
Copy link
Member

Closing it as duplicated, as explained by @HeahDude.

@Seb33300 for now, the solution would be to define a setDomains() method and use it instead of addDomain() as explained in the comments of #7468.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants