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 6158103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:53:59+00:00 2026-05-23T20:53:59+00:00

In Doctrine2.0.6, I keep getting an error: Column VoucherId specified twice. The models in

  • 0

In Doctrine2.0.6, I keep getting an error: “Column VoucherId specified twice”.

The models in question are:

  • Basket
  • BasketVoucher
  • Voucher

Basket links to BasketVoucher.

Voucher links to BasketVoucher.

In Voucher and BasketVoucher, there is a field called VoucherId. This is defined in both models and exists with the same name in both DB tables.

The error occurs when saving a new BasketVoucher record:

$basketVoucher = new BasketVoucher;
$basketVoucher->setVoucherId($voucherId);
$basketVoucher->setBasketId($this->getBasket()->getBasketId());
$basketVoucher->setCreatedDate(new DateTime("now"));
$em->persist($basketVoucher);
$em->flush();

I’ve checked the models and VoucherId is not defined twice. However, it is used in a mapping. Is this why Doctrine thinks that the field is duplicated?

Here’s the relevant code – I haven’t pasted the models in their entirety as most of the code is get/set.

Basket

/**
 * @OneToMany(targetEntity="BasketVoucher", mappedBy="basket")
 * @JoinColumn(name="basketId", referencedColumnName="BasketId")
 */
private $basketVouchers;

public function getVouchers()
{
    return $this->basketVouchers;
}

BasketVoucher

/**
 * @ManyToOne(targetEntity="Basket", inversedBy="basketVouchers")
 * @JoinColumn(name="basketId", referencedColumnName="BasketId")
 */
private $basket;

public function getBasket()
{
    return $this->basket;
}

/**
 * @OneToOne(targetEntity="Voucher", mappedBy="basketVoucher")
 * @JoinColumn(name="voucherId", referencedColumnName="VoucherId")
 */
private $voucherEntity;

public function getVoucher()
{
    return $this->voucherEntity;
}

Voucher

/**
 * @OneToOne(targetEntity="BasketVoucher", inversedBy="voucherEntity")
 * @JoinColumn(name="voucherId", referencedColumnName="VoucherId")
 */
private $basketVoucher;

public function getBasketVoucher()
{
    return $this->basketVoucher;
}

Any ideas?

EDIT: I’ve found that the same issue occurs with another model when I save it for the first time. I am setting the primary key manually. The main issue appears to be saving a relationship within an entity.

In this case, I have a field – DraftOrderId – which is used as the primary key on three models. The first model – DraftOrder – has DraftOrderId as a primary key, which is an auto incrementing value. The other two models – DraftOrderDeliveryAddress, and DraftOrderBillingAddress – also use DraftOrderId as a primary key, but it isn’t auto incremented.

What’s happening is one of the following issues:

  1. If I save the delivery address entity with a draft order id and set it to persist, I get an error: Column DraftOrderId specified twice. Code:

    try {
        $addressEntity->getDraftOrderId();
    } catch (\Doctrine\ORM\EntityNotFoundException $e) {
        if ($addressType == "delivery") {
            $addressEntity = new Dpp\DraftOrderDeliveryAddress;
        } elseif ($addressType == "billing") {
            $addressEntity = new Dpp\DraftOrderBillingAddress;
        }
        $addressEntity->setDraftOrderId($draftOrder->getDraftOrderId());
        $em->persist($addressEntity);
    }
    

(It would also help to know if there’s a better way of checking if a related entity exists, rather than trapping the exception when trying to get a value.)

  1. If I remove the line that sets the draft order id, I get an error: Entity of type Dpp\DraftOrderDeliveryAddress is missing an assigned ID.

  2. If I keep the line that sets the draft order id but I remove the persist line, and I also keep the lines later on in the code that sets the name and address fields, I don’t get an error – but the data is not saved to the database. I am using flush() after setting all the fields – I’m just not using persist(). In the previous examples, I do use persist() – I’m just trying things out to see how this can work.

I can paste more code if it would help.

  • 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-05-23T20:54:00+00:00Added an answer on May 23, 2026 at 8:54 pm

    I think I’ve fixed it! A couple of findings:

    • For a primary key that is not an auto-incrementing value, you need to use:

      @generatedValue(strategy=”IDENTITY”)

    • You also have to explicitly set the mapped entities when creating them for the first time. At first, I was trying to create the address entity directly, but I wasn’t setting the mapped entity within the parent model to reference the address entity. (if that makes any sense)

    I’m fairly sure it was mostly due to the lack of the IDENTITY keyword, which for some reason was either saying the key wasn’t set, or saying it was set twice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep on getting this error when running one of my scripts; PHP Fatal
(This question is about the best way to temporarily and programatically keep new records
I keep getting the following exception with a new resource Im making and i
I have a question out of curiosity about the inner workings of Doctrine2. I
What does this error from Doctrine2 & Symfony2 mean? Could not convert database value
Say I have two entities in Doctrine2 that are related to each other, Models\User
I understand there is no PreInsert listener in Doctrine2, right? Is the equivalnet PrePersist?
I'm trying to use MapClassLoader in autoload.php but for some reason I keep getting
I'm trying to recreate the database from a new schema, but I keep getting
I'm working with Doctrine2 for the first time, but I think this question is

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.