Skip to content

Instantly share code, notes, and snippets.

@hobodave
Created April 2, 2012 20:14
Show Gist options
  • Save hobodave/2286906 to your computer and use it in GitHub Desktop.
Save hobodave/2286906 to your computer and use it in GitHub Desktop.
<?php
$dept = $this->em->getRepository('Common\Model\Department')->find($data['id']);
$data = $form->getValues();
if (isset($data['locations'])) {
$locations = array_flip($data['locations']);
foreach ($dept->getLocations() as $deptLoc) {
if ($dept == $deptLoc->getDepartment()
&& isset($locations[$deptLoc->getLocation()->getId()])
) {
unset($locations[$deptLoc->getLocation()->getId()]);
continue;
}
$dept->getLocations()->removeElement($deptLoc);
}
foreach ($locations as $locationId => $junk) {
$location = $this->em->getReference('Common\Model\Location', $locationId);
$dept->addLocation(new \Common\Model\DepartmentLocation($dept, $location));
}
}
<?php
namespace Common\Model;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity(repositoryClass="Common\Model\Repository\DepartmentRepository")
* @Table(name="department")
*/
class Department extends AdminEntity
{
/**
* @Id
* @Column(type="integer", name="department_id")
* @GeneratedValue
*/
protected $id;
/** @Column(type="string",length=45,unique=true) */
protected $name;
/**
* @Column(type="boolean", name="is_active")
*/
protected $active = true;
/**
* @Column(type="string", length=45, name="cost_center")
*/
protected $costCenter;
/**
* @ManyToOne(targetEntity="DepartmentType", inversedBy="departments")
* @JoinColumn(name="department_type_id", referencedColumnName="department_type_id")
*/
protected $type;
/**
* @ManyToOne(targetEntity="Site", inversedBy="departments")
* @JoinColumn(name="site_id", referencedColumnName="site_id")
*/
protected $site;
/**
* @ManyToMany(targetEntity="Smodule", inversedBy="departments")
* @JoinTable(
* name="smodule_department",
* inverseJoinColumns={
* @JoinColumn(name="smodule_id", referencedColumnName="smodule_id",onDelete="cascade")
* },
* joinColumns={
* @JoinColumn(name="department_id", referencedColumnName="department_id",onDelete="cascade")
* }
* )
*/
protected $smodules;
/**
* @OneToMany(targetEntity="DepartmentLocation", mappedBy="department", cascade={"persist", "remove"}, orphanRemoval=true)
*/
protected $locations;
public function addLocation(DepartmentLocation $deptLoc)
{
if ($this->locations->contains($deptLoc)) {
return $this;
}
$this->locations->add($deptLoc);
return $this;
}
}
<?php
namespace Common\Model;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
* @Table(name="department_location")
*/
class DepartmentLocation
{
/**
* @Id
* @ManyToOne(targetEntity="Department", inversedBy="locations")
* @JoinColumn(name="department_id", referencedColumnName="department_id")
* @var Department
*/
protected $department;
/**
* @Id
* @ManyToOne(targetEntity="Location", inversedBy="departments")
* @JoinColumn(name="location_id", referencedColumnName="location_id")
* @var Location
*/
protected $location;
/**
* @Column(type="boolean", name="is_default")
*/
protected $isDefault = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment