I have an application with Zend Framework2 and Doctrine2 as ORM. I have this Entity called User:
namespace Adm\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User{
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
/**
* @ORM\Column(type="string")
*/
protected $email;
/**
* @ORM\Column(type="string")
*/
protected $password;
/**
* @ORM\ManyToMany(targetEntity="Module")
* @ORM\JoinTable(
* name="user_module",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="module_id", referencedColumnName="id")}
* )
*/
protected $modules;
public function __construct() {
$this->modules = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @return the $id
*/
public function getId() {
return $this->id;
}
/**
* @return the $name
*/
public function getName() {
return $this->name;
}
/**
* @return the $email
*/
public function getEmail() {
return $this->email;
}
/**
* @return the $password
*/
public function getPassword() {
return $this->password;
}
/**
* @param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* @param field_type $name
*/
public function setName($name) {
$this->name = $name;
}
/**
* @param field_type $email
*/
public function setEmail($email) {
$this->email = $email;
}
/**
* @param field_type $password
*/
public function setPassword($password) {
$this->password = $password;
}
/**
* Add module
*
* @param dm\Entity\Module
* @return User
*/
public function addModules(Module $modules = null){
$this->modules[] = $modules;
}
/**
* Get modules
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getModules(){
return $this->modules;
}
}
See the modules property is a relation Many to Many with a table called user_modules. And i have the Entity Module as well:
namespace Adm\Entity;
use Doctrine\ORM\Mapping as ORM;
class Module{
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $name;
/**
* @ORM\Column(type="integer")
*/
private $status;
/**
* @return the $id
*/
public function getId() {
return $this->id;
}
/**
* @return the $name
*/
public function getName() {
return $this->name;
}
/**
* @return the $status
*/
public function getStatus() {
return $this->status;
}
/**
* @param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* @param field_type $name
*/
public function setName($name) {
$this->name = $name;
}
/**
* @param field_type $status
*/
public function setStatus($status) {
$this->status = $status;
}
}
I receive a array variable containing the Post from a form to insert in a table. Each post element have it's property in Entity, as expected. Together, i have a $module variable which is an array containing id's of the modules. My question is: How do i insert this data in the user_module table? My add function is this:
public function addUser($newUser){
$user = new User();
$user->setName($newUser['name']);
...
$this->getEm()->persist($user);
$this->getEm()->flush();
}
Firstly you need to have cascade={"persist"}
as mentioned by @skrilled.
Then you need to retrieve the module entities from the database. You mentioned you have the id's in the $module variable.
You need a DQL statement something like this
$builder = $this->getEntityManager()->createQueryBuilder();
$builder->select('m')
->from('Adm\Entity\Module', 'm')
->where('m.id IN (:modules)')
->setParameter('modules', $modules);
$moduleEntities= $builder->getQuery()->getResult(Query::HYDRATE_OBJECT);
and in your User entity you will need
public function addModules(Array $moduleEntities)
{
foreach ($moduleEntities as $module) {
if ($module instanceof Module) {
$this->modules[] = $module;
}
}
return $this;
}
finally in your addUser method you will need to add the array of modules from the above DQL
public function addUser($newUser, $moduleEntities)
{
$user = new User();
$user->setName($newUser['name']);
....
$user->addModules($moduleEntities);
$this->getEm()->persist($user);
$this->getEm()->flush();
}
I hope this helps
You should read about using cascade
. This will allow you to save/modify/remove the associated relationships and how you expect this to work.
In this case, you would want the relationship to persist
since you want the associated entities to be saved when user itself is saved.
@ORM\ManyToMany(targetEntity="Module", cascade={"persist"})
By default, no operations are cascaded.
The following cascade options exist:
persist : Cascades persist operations to the associated entities.
remove : Cascades remove operations to the associated entities.
merge : Cascades merge operations to the associated entities.
detach : Cascades detach operations to the associated entities.
refresh : Cascades refresh operations to the associated entities.
all : Cascades persist, remove, merge, refresh and detach operations to associated entities.