Don't like ads? PRO users don't see any ads ;-)
Guest

User/Hotel Association

By: a guest on May 16th, 2012  |  syntax: PHP  |  size: 1.72 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.        
  2. class User {
  3.         ...
  4.  
  5.         /**
  6.          * @var \Doctrine\Common\Collections\Collection<\DKM\Services\Domain\Model\Hotel>
  7.          * @ORM\ManyToMany(mappedBy="users" , cascade={"persist"})
  8.          */
  9.         protected $hotels;
  10.  
  11.         /**
  12.          * Get the Users's hotels
  13.          *
  14.          * @return \Doctrine\Common\Collections\Collection The Users's hotels
  15.          */
  16.         public function getHotels() {
  17.                 return $this->hotels;
  18.         }
  19.  
  20.         /**
  21.          * Sets this User's hotel
  22.          *
  23.          * @param \Doctrine\Common\Collections\Collection $users The User's hotels
  24.          * @return void
  25.          */
  26.         public function setHotels(\Doctrine\Common\Collections\Collection $hotels) {
  27.                 $this->hotels = $hotels;
  28.                
  29.                 //This is necessary (I don't know if there's a way to automatically do this)
  30.                 //because user is the "inversedSide" of the manytomany relationship.
  31.                 //@see http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html#association-mapping-owning-inverse
  32.                 foreach ($hotels as $hotel) {
  33.                         $hotel->getUsers()->add($this);
  34.                 }
  35.         }
  36. }
  37.  
  38.  
  39. class Hotel{
  40.  
  41.         /**
  42.          * Users is an array collection of User objects
  43.          * @var \Doctrine\Common\Collections\Collection<\DKM\Services\Domain\Model\User>
  44.          * @ORM\ManyToMany(inversedBy="hotels", cascade={"persist"})
  45.          * @ORM\JoinTable(name="dkm_services_domain_model_hotel_user_join")
  46.          */
  47.         protected $users;
  48.         /**
  49.          * Get the Hotel's users
  50.          *
  51.          * @return \Doctrine\Common\Collections\Collection The Hotel's users
  52.          */
  53.         public function getUsers() {
  54.                 return $this->users;
  55.         }
  56.  
  57.         /**
  58.          * Sets this Hotel's users
  59.          *
  60.          * @param \Doctrine\Common\Collections\Collection $users The Hotel's users
  61.          * @return void
  62.          */
  63.         public function setUsers(\Doctrine\Common\Collections\Collection $users) {
  64.                 $this->users = $users;
  65.         }
  66.  
  67.  
  68. }