Zend - doctrine 2 insert data / associations mapping
i finally set up my mapping for my two tables, i can now join tables via the querybuilder..
however, i cant add data to the join column, its keeps saying nul开发者_如何学JAVAl.
my account entity:
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;
and my profiledetails entity:
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;
i use to use :
$postdata array ('name'=>'jason');
$entity =new \Entities\Users\Account;
$obj->setData($postdata);
$this->_doctrine->persist($obj);
$this->_doctrine->flush();
$this->_doctrine->clear();
and it doesnt add.. what the way to add data to the parent table where all linked tables get updated? because before i could enter a profileid and now its null because i used it as a the joined column..
Linked objects can be "updated" if you setup cascade=[persist]
in your relationship definitions. You also need @mappedBy and @inversedBy to be set for both sides of the relations. Basically @mappedBy is set to the oneToMany side (called inverse side) and @inversedBy to the manyToOne side (called owning side)
http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html#one-to-many-bidirectional
The proper way is basically
//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
精彩评论