微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-教义2-持久化实体,而连接表的外键与外来实体相对

我正在尝试保留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 
    )
);

参见学说的Data Retrieval And Manipulation

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐