我正在尝试保留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时,我遇到了参考代理.
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
)
);