gpt4 book ai didi

php - Doctrine 2 - 持久实体与连接表的外键反对外国实体

转载 作者:可可西里 更新时间:2023-10-31 23:01:28 24 4
gpt4 key购买 nike

我正在尝试保留一个 TradeEntity。 TradeEntity 与 CurrencyEntity 具有 OneToOne 关系。

  /**
* @ORM\OneToOne(targetEntity="Repositories\Currency\CurrencyEntity")
* @ORM\JoinColumn(name="currency", referencedColumnName="id")
*
* @var CurrencyEntity
*/
protected $currency;

我从另一个对象收到了一个 CurrencyEntity,我试图将其插入到这个新的 TradeEntity 中并将其保存到数据库中,但出现异常:

Type: Doctrine\ORM\ORMInvalidArgumentException
Message: Expected value of type "Repositories\Currency\CurrencyEntity"
for association field "Repositories\Trade\TradeEntity#$currency", got "integer" instead.

没有其他方法可以让我在不从数据库中获取 CurrencyEntity 并以这种方式设置它的情况下保留 TradeEntity 吗?

最佳答案

鉴于我最近的发现,我觉得有必要更新这个答案。

阅读 about Doctrine's advanced configuration ,我遇到了 Reference Proxies

The method EntityManager#getReference($entityName, $identifier) lets you obtain a reference to an entity for which the identifier is known, without loading that entity from the database.

This is useful, for example, as a performance enhancement, when you want to establish an association to an entity for which you have the identifier. You could simply do this:

<?php
// $em instanceof EntityManager, $cart instanceof MyProject\Model\Cart
// $itemId comes from somewhere, probably a request parameter
$item = $em->getReference('MyProject\Model\Item', $itemId);
$cart->addItem($item);

旧答案:

可以做的是(它违背了 ORM 的目的,但它可能的):

$conn = $entityManager->getConnection();
$conn->insert(
'table_name',
array(
'column_name' => 'column_value',
// define all the columns here
)
);

参见 Doctrine 的 Data Retrieval And Manipulation

关于php - Doctrine 2 - 持久实体与连接表的外键反对外国实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44955296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com