Hi,
Here I included my two entities for better debugging:
With doctrine I want to setup two ManyToMany entities to each other: Admin
and Department.
Following these two tutorials
https://gist.github.com/Ocramius/3121916
http://www.inanzzz.com/index.php/post/h0jt/bidirectional-many-to-many-cascade-remove-and-orphan-removal-operations-in-doctrine
I made the following two Admin and Department entities.
When I delete a department its relationship in join table is deleted too
even without calling removeAdmin.
But when I delete an admin its relationship in join table is not deleted
even with calling removeDepartment.
What wrong I did?
<?php
// Admin.php
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
class Admin {
// setup properties goes here
public function __construct()
{
$this->departments = new ArrayCollection();
}
// setter getter methods goes here
public function removeDepartments(HdDepartment $department)
{
if (!$this->departments->contains($department)) {
return;
}
$this->departments->removeElement($department);
$department->removeAdmin($this);
}
public function addDepartment(HdDepartment $department)
{
if ($this->departments->contains($department)) {
return;
}
$this->departments->add($department);
$department->addAdmin($this);
}
public static function loadMetadata(ClassMetadata $metadata) {
$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE);
// define properties and fields goes here
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_IDENTITY);
$metadata->customRepositoryClassName = 'Repository\Admin';
$metadata->mapManyToMany(array('fieldName' => 'departments',
'targetEntity' => 'HdDepartment',
'inversedBy' => 'admins',
'joinTable' => array( 'name' =>
'admin_hddepartment',
'schema' => NULL,
'joinColumns' =>
array( 0 => array( 'name' => 'admin_id',
'referencedColumnName' => 'admin_id',
'nullable' => true,
'columnDefinition' => NULL,
),),
'inverseJoinColumns'
=> array( 0 => array( 'name' => 'hddepartment_id',
'referencedColumnName' => 'id',
'nullable' => true,
'onDelete' => 'cascade',
'columnDefinition' => NULL,
), ),
),
)
);
}
}
<?php
// HdDepartment.php
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\Common\Collections\ArrayCollection;
class HdDepartment {
// setup properties goes here
public function __construct() {
$this->admins = new ArrayCollection();
}
// setter getter methods goes here
public function removeAdmin(Admin $admin)
{
if (!$this->admins->contains($admin)) {
return;
}
$this->admins->removeElement($admin);
$admin->removeDepartments($this);
}
public function addAdmin(Admin $admin)
{
if ($this->admins->contains($admin)) {
return;
}
$this->admins->add($admin);
$admin->addDepartment($this);
}
public static function loadMetadata(ClassMetadata $metadata) {
$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE);
// define properties and fields goes here
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_IDENTITY);
$metadata->mapManyToMany(array('fieldName' => 'admins',
'targetEntity' => 'Entities\\Admin',
'mappedBy' => 'departments'
)
);
}
}
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.