我终于为我的两个表建立了映射,现在我可以通过querybuilder连接表了。
但是,我不能将数据添加到join列,它总是显示为null。
我的账户主体:
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实体:
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;我过去常常使用:
$postdata array ('name'=>'jason');
$entity =new \Entities\Users\Account;
$obj->setData($postdata);
$this->_doctrine->persist($obj);
$this->_doctrine->flush();
$this->_doctrine->clear();而且它没有添加..向所有链接表都更新的父表添加数据的方法是什么?因为以前我可以输入一个profileid,现在它是null,因为我将它用作联接的列。
发布于 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
正确的方式基本上是
//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
https://stackoverflow.com/questions/6524580
复制相似问题