Doctrine2.0:错误 - 列指定两次

发布于 2024-11-25 01:05:09 字数 2946 浏览 1 评论 0原文

在Doctrine2.0.6中,我不断收到错误:“Column VoucherId指定了两次”。

有问题的模型是:

  • Basket
  • BasketVoucher
  • Voucher

Basket 链接到 BasketVoucher。

优惠券链接至 BasketVoucher。

在Voucher和BasketVoucher中,有一个名为VoucherId的字段。这在两个模型中都有定义,并且在两个数据库表中都以相同的名称存在。

保存新的 BasketVoucher 记录时发生错误:

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

我检查了模型,并且 VoucherId 未定义两次。但是,它用于映射。这就是 Doctrine 认为该字段重复的原因吗?

这是相关的代码 - 我没有完整地粘贴模型,因为大部分代码都是获取/设置的。

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;
}

优惠券

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

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

有什么想法吗?

编辑:我发现当我第一次保存另一个模型时,也会出现同样的问题。我正在手动设置主键。主要问题似乎是保存实体内的关系。

在本例中,我有一个字段 - DraftOrderId - 用作三个模型的主键。第一个模型 - DraftOrder - 将 DraftOrderId 作为主键,它是一个自动递增值。其他两个模型 - DraftOrderDeliveryAddress 和 DraftOrderBillingAddress - 也使用 DraftOrderId 作为主键,但它不会自动递增。

发生的情况是以下问题之一:

  1. 如果我使用草稿订单 ID 保存送货地址实体并将其设置为保留,则会收到错误:列 DraftOrderId 指定了两次。代码:

    <前><代码>尝试{ $addressEntity->getDraftOrderId(); } catch (\Doctrine\ORM\EntityNotFoundException $e) { if ($addressType == "送货") { $addressEntity = new Dpp\DraftOrderDeliveryAddress; } elseif ($addressType == "账单") { $addressEntity = 新 Dpp\DraftOrderBillingAddress; } $addressEntity->setDraftOrderId($draftOrder->getDraftOrderId()); $em->persist($addressEntity); }

这也有助于了解是否有更好的方法来检查相关实体是否存在,而不是在尝试获取值时捕获异常。)

  1. 如果我删除设置草稿订单 id 的行,我收到错误:Dpp\DraftOrderDeliveryAddress 类型的实体缺少分配的 ID。

  2. 如果我保留设置草稿订单 id 的行,但删除持久行,并且稍后在设置名称和地址字段的代码中保留这些行,我不会收到错误 - 但数据不保存到数据库中。我在设置所有字段后使用flush() - 我只是不使用persist()。在前面的示例中,我确实使用了 persist() - 我只是尝试看看它是如何工作的。

如果有帮助的话我可以粘贴更多代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夢归不見 2024-12-02 01:05:09

我想我已经修好了!一些发现:

  • 对于不是自动递增值的主键,您需要使用:

    @ generatedValue(strategy="IDENTITY")

  • 您还必须首次创建映射实体时显式设置它们。起初,我尝试直接创建地址实体,但我没有在父模型中设置映射实体来引用地址实体。 (如果这有意义)

我相当确定这主要是由于缺少 IDENTITY 关键字,出于某种原因,它要么说密钥未设置,要么说它设置了两次。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文