drxzpo70788179614 2012-04-26 11:57
浏览 22
已采纳

这是一个Doctrine 2.2外键错误?

My Code:

        $item = new Entity\Item();

        $item->setAccountId($account["id"]);
        $item->setCategory((string)$data->category);
        $item->setRegion((string)$data->region);
        $item->setFlag(0);
        $item->setRank(0);
        $item->setPhone("");
        $item->setEmail("");
        $item->setURL((string)$data->url);
        $item->setTags($tags);

        //var_dump($item);
        //die();

        $this->em->persist($item);
        $this->em->flush();
        $this->em->clear();

Annotation Relationship:

/////////////////////////////////////////////////////////////////////////
// Relations                                                     //
/////////////////////////////////////////////////////////////////////////

/**
* @ManyToOne(targetEntity="Account", inversedBy="items")
* @JoinColumn(name="account_id", referencedColumnName="id")
*/
private $account;

/////////////////////////////////////////////////////////////////////////
// ForeingKey                                                           //
/////////////////////////////////////////////////////////////////////////

/**
 * @Column(type="integer")
 */
protected $account_id;   

If i use old doctrine (2.0) the code work, if i set to 2.2 , code throw this exception:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'account_id' cannot be null

In 2.2 i need to use the Parent entity to insert a Chield entity?

  • 写回答

1条回答 默认 最新

  • douliao2493 2012-04-26 19:26
    关注

    From Doctrine2's point of view, your objects should form an object graph, where foreign keys have no place. Because of this, I find it really weird that you

    • have an attribute_id property which is a foreign key
    • say that it worked in 2.0

    I'm not sure how it was in 2.0, but I don't remember having ids of a related object in properties.

    So, you should drop the $account_id property, and use $item->setAccount($account), where $account is an object of class Account, in order for Doctrine to correctly handle the relation.

    You can also try letting Doctrine generate entity skeletons for you with the command:

    php doctrine.php orm:generate-entities /tmp/entites
    

    And check the generated files in /tmp/entities, you will see how should look.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办