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:
-
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.)
-
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.
-
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.
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.