首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zend - data 2插入数据/关联映射

Zend - data 2插入数据/关联映射
EN

Stack Overflow用户
提问于 2011-06-30 01:08:07
回答 1查看 2.7K关注 0票数 0

我终于为我的两个表建立了映射,现在我可以通过querybuilder连接表了。

但是,我不能将数据添加到join列,它总是显示为null。

我的账户主体:

代码语言:javascript
复制
namespace Entities\Users;

/**
 * @Entity(repositoryClass="\Entities\Users\Account")
 * @Table(name="account")
 * @HasLifecycleCallbacks
 */
class Account extends \Entities\AbstractEntity {

    /**
     * @Id @Column(name="accid", type="bigint",length=15)
     * @GeneratedValue(strategy="AUTO")
     */
    protected $accid;

    /** @Column(name="name", type="string", length=255) */
    protected $name;  

    /** @Column(name="profileid", type="integer", length=255) */
    protected $profileid; 


    /** @Column(name="acc_added_date", type="datetime", columnDefinition="datetime", nullable=true) */
    private $acc_added_date;

    /**
     * @ManyToOne(targetEntity="Profiledetails")
     * @JoinColumn(name="profileid", referencedColumnName="pid") 
     */
    private $account;

和我的profiledetails实体:

代码语言:javascript
复制
namespace Entities\Users;

/**
 * @Entity(repositoryClass="\Entities\Users\Profiledetails")
 * @Table(name="profiledetails")
 * @HasLifecycleCallbacks
 */
class Profiledetails extends \Entities\AbstractEntity {

    /**
     * @Id @Column(name="pid", type="bigint",length=15)
     * @GeneratedValue(strategy="AUTO")
     */
    protected $accid;
    /** @Column(name="name", type="string", length=255) */
    protected $name;
    /** @Column(name="profileid", type="integer", length=255) */
    protected $profileid;
    /** @Column(name="acc_added_date", type="datetime", columnDefinition="datetime", nullable=true) */
    private $acc_added_date;
    /**
     * @OneToMany(targetEntity="Account", mappedBy="account")
     * @JoinColumn(name="pid", referencedColumnName="pid")
     */
    private $stances;

我过去常常使用:

代码语言:javascript
复制
    $postdata array ('name'=>'jason');
    $entity =new \Entities\Users\Account;
    $obj->setData($postdata);

    $this->_doctrine->persist($obj);
    $this->_doctrine->flush();
    $this->_doctrine->clear();

而且它没有添加..向所有链接表都更新的父表添加数据的方法是什么?因为以前我可以输入一个profileid,现在它是null,因为我将它用作联接的列。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-30 22:47:26

如果在关系定义中设置了cascade=[persist],则可以“更新”链接对象。您还需要为关系的两端设置@mappedBy和@inversedBy。基本上,@mappedBy设置为oneToMany侧(称为逆向侧),@ manyToOne侧设置为owning侧(称为owning侧)

http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html#one-to-many-bidirectional

正确的方式基本上是

代码语言:javascript
复制
//assume $this->_doctrine is instance of EntityManager
$user = new User();
$user->setEmail('john@example.com');
$account = new Account();
$account->setName('john');
$account->setUser($user);
$user->addAccount($account); //if no cascade set
$this->_doctrine->persist($account); 
$this->_doctrine->persist($user); //if no cascade set
$this->_doctrine->flush();

http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-associations.html#establishing-associations

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6524580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档