Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7785385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:14:50+00:00 2026-06-01T20:14:50+00:00

I’m having trouble validating the uniqueness of two fields, being one of them an

  • 0

I’m having trouble validating the “uniqueness” of two fields, being one of them an entity of an association. The logic is that there can’t be two taxes with the same description for a single country.

Here’s my (failed) attempt:

/**
 * @ORM\Entity
 * @ORM\Table(name="taxes", uniqueConstraints={@ORM\UniqueConstraint(columns={"country_id", "description"})})
 * @UniqueEntity(fields={"country", "description"})
 */

class Tax
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO") 
     */
    protected $id;

    /**
     * @ORM\Column()         
     */
    protected $description;

    /**
     * @ORM\Column(type="float")
     */
    protected $value;

    /**
     * @ORM\ManyToOne(targetEntity="Country", inversedBy="taxes")
     */
    protected $country;

    //getters and setters...
}

When I test my app with a duplicate tax entity, the form pass the validation (when it shouldn’t) and Symfony throws an error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-ITBMS' for key 'country_id'

UPDATE: I found that this is a known bug in Doctrine 2.1 that is fixed in Doctrine 2.2. Unfortunately, Symfony 2.0.11 (my current version) ships with Doctrine 2.1 and I don’t know how to update my deps file appropriately

UPDATE 2: After updating my deps and deps.lock files to get the latest Doctrine 2.2.1 files as @elnur suggested below, the problem is still there: The composite unique key is created in the database but the validation is not correctly performed. Upgrading the Doctrine files alone is not solving the problem.

UPDATE 3: I even updated Symfony core to version 2.0.12 but it doesn’t solve the problem either.

UPDATE 4 (SOLVED): I found the error inside my controller. Here is my original controller code:

public function createAction($country_id)
{
    //...        

    if($request->getMethod() == 'POST')
    {
        $form->bindRequest($request);
        $tax->setCountry($country); //HERE IS THE ERROR...

        if($form->isValid())
        {   
            //...
        }
    }        
    //...
}

Setting the country before binding the request was the solution.

public function createAction($country_id)
{
    //...        

    if($request->getMethod() == 'POST')
    {            
        $tax->setCountry($country); //NOW IT WORKS...
        $form->bindRequest($request);            

        if($form->isValid())
        {   
            //...
        }
    }        
    //...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:14:51+00:00Added an answer on June 1, 2026 at 8:14 pm

    To upgrade to Doctrine 2.2.1, replace related entries in your deps file with these:

    [doctrine-common]
        git=http://github.com/doctrine/common.git
        version=2.2.1
    
    [doctrine-dbal]
        git=http://github.com/doctrine/dbal.git
        version=2.2.1
    
    [doctrine]
        git=http://github.com/doctrine/doctrine2.git
        version=2.2.1
    

    And in your deps.lock with these:

    doctrine-common 2.2.1
    doctrine-dbal 2.2.1
    doctrine 2.2.1
    

    Then run:

    bin/vendors install
    

    UPDATE

    Since upgrading Doctrine didn’t work, try the UniqueEntityCaseInsensitive constraint from my ValidatorBundle.

    Install the bundle, import the constraint:

    use Elnur\ValidatorBundle\Validator\Constraints\UniqueEntityCaseInsensitive;
    

    and replace your

    @UniqueEntity(fields={"country", "description"})
    

    with

    @UniqueEntityCaseInsensitive(fields={"country", "description"})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.