Php 多个实体持久化doctrine2 Zend

Php 多个实体持久化doctrine2 Zend,php,doctrine-orm,Php,Doctrine Orm,我有3个实体用户,用户档案和员工 用户配置文件通过用户id链接到用户表 Staff表使用userprofileid链接到userprofile 管理员创建用户配置文件并生成注册号、用户名和密码 我想将用户记录添加到users表,然后添加用户配置文件,然后将配置文件id添加到staff表 我想依次坚持三个实体 我尝试为用户创建一个实例,如 $this->userent->setUsername('xxx'); $this->em->persist($this->use

我有3个实体
用户
用户档案
员工

用户配置文件通过用户id链接到用户表 Staff表使用userprofileid链接到userprofile

管理员创建用户配置文件并生成注册号、用户名和密码

我想将用户记录添加到users表,然后添加用户配置文件,然后将配置文件id添加到staff表

我想依次坚持三个实体

我尝试为用户创建一个实例,如

$this->userent->setUsername('xxx');
$this->em->persist($this->userent);
$this->em->flush();
然后:

基本上,一个表单在三个实体之间共享,我想按顺序插入到三个表中

更新

除了
用户
实体之外,我还有一个链接到用户的
用户类型
实体……我只想保留外键。我有

setUserType(Usertype$Usertype)
method用户中用户类型实体的实例

当我这样做的时候

 $this->userent = new Users();
$this->userent->setUserType($this->em->getRepository('\Campus\Entity\Usertype')->findByUserType("admin")) 
我得到了错误

Argument 1 passed to Campus\Entity\Users::setUserType() must be an instance of Campus\Entity\Usertype, array given
如果我传递数组的值,该数组是Usertype的实例

我得到一个错误,说需要一个数组用于ArrayCollection..请帮助

Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in D:\xampp\htdocs\zend\library\Doctrine\ORM\UnitOfWork.php on line 406 defined in D:\xampp\htdocs\zend\library\Doctrine\Common\Collections\ArrayCollection.php on line 46

您可以持久化每个实体,然后在最后刷新()一次

如果实体之间存在关系,则可以检查以下注释

cascade={"persist"}
“将持久化操作级联到关联实体。”


请看一下此处的文档:

少考虑数据库,多考虑对象。这就是教义的全部要点

你想要这样的东西:

<?php
// create some entities

$user = new Entity\User();
$user->setUsername('userman');

$profile = new Entity\UserProfile();
$profile->setFirstname('joe');
$profile->setLastname('smith');

$staff = new Entity\Staff();
$staff->setSomething('value-for-something');

// associate those entities together
$profile->setStaff($staff);
$user->setProfile($profile);

// assuming you have set up cascade={"persist"} on your associations
$this->em->persist($user);

// if you haven't set up cascade={"persist"}, you will need to call persist on each entity:
// $this->em->persist($profile);
// $this->em->persist($staff);

$em->flush();

我真的想知道如何在多个实体中持久化..嗨,Tim,我认为这应该行得通,我还查看了一篇关于Hibernate的文章,与此解决方案类似..你能看看更新的部分吗..我有外键问题..你能帮忙吗。。
<?php
// create some entities

$user = new Entity\User();
$user->setUsername('userman');

$profile = new Entity\UserProfile();
$profile->setFirstname('joe');
$profile->setLastname('smith');

$staff = new Entity\Staff();
$staff->setSomething('value-for-something');

// associate those entities together
$profile->setStaff($staff);
$user->setProfile($profile);

// assuming you have set up cascade={"persist"} on your associations
$this->em->persist($user);

// if you haven't set up cascade={"persist"}, you will need to call persist on each entity:
// $this->em->persist($profile);
// $this->em->persist($staff);

$em->flush();