我有3个实体Users
,UserProfile
和Staffs
用户配置文件通过用户ID链接到Users表使用userprofileid将Staff表链接到userprofile
管理员创建用户配置文件并生成注册号,用户名和密码.
我想在users表中添加用户记录,然后添加用户配置文件,然后将配置文件ID添加到staff表中.
我想按顺序坚持三个实体
我试图为用户创建一个实例
$this->userent->setUsername('xxx');
$this->em->persist($this->userent);
$this->em->flush();
Run Code Online (Sandbox Code Playgroud)
然后:
$this->profileent->setFirstname('xxx');
$this->em->persist($this->profileent);
$this->em->flush();
Run Code Online (Sandbox Code Playgroud)
基本上,一个表单在三个实体之间共享,我想按顺序插入三个表,
更新
除了users
实体我有一个usertype
链接到用户的实体...我想只保留外键.我有
setUserType(Usertype $userType)
方法用户中user_type实体的实例
当我做
$this->userent = new Users();
$this->userent->setUserType($this->em->getRepository('\Campus\Entity\Usertype')->findByUserType("admin"))
Run Code Online (Sandbox Code Playgroud)
我收到了错误
Argument 1 passed to Campus\Entity\Users::setUserType() must be an instance of Campus\Entity\Usertype, array given
Run Code Online (Sandbox Code Playgroud)
如果我传递数组的值,这是一个Usertype的实例
我得到一个错误,说需要ArrayCollection..help数组请!!!
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
Run Code Online (Sandbox Code Playgroud)
少考虑数据库,更多地考虑你的对象.这就是学说的全部观点.
你想要这样的东西:
<?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();
Run Code Online (Sandbox Code Playgroud)
所以,基本的想法是你建立你的对象,让它们进入Doctrine的工作单元(通过调用persist()并可能设置一些级联),然后通过调用flush将它们全部写入数据库中()
归档时间: |
|
查看次数: |
3407 次 |
最近记录: |