HosipLan

Untitled

Aug 5th, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.96 KB | None | 0 0
  1. <?php
  2.  
  3. use \Doctrine\Common\Collections\ArrayCollection;
  4.  
  5.  
  6. /**
  7.  * @Entity @Table(name="book")
  8.  */
  9. class Book
  10. {
  11.     /** @Id @Column(type="integer") @GeneratedValue */
  12.     private $id;
  13.  
  14.     /** @Column(type="string",length="80") */
  15.     private $title;
  16.  
  17.     /** @Column(type="string",length="80") */
  18.     private $genre;
  19.  
  20.     /** @OneToMany(targetEntity="BookOption", mappedBy="book", cascade={"persist"}) */
  21.     private $options;
  22.  
  23.  
  24.     function __construct()
  25.     {
  26.         $this->options = new ArrayCollection();
  27.     }
  28.  
  29.     public function addOption(BookOptions $option)
  30.     {
  31.         $this->options->add($option);
  32.         $option->book = $this;
  33.         return $option;
  34.     }
  35.  
  36.  
  37.     public function __get($name)
  38.     {
  39.         if(property_exists($this, $name))
  40.         {
  41.             return $this->$name;
  42.         }
  43.     }
  44.    
  45.     public function __set($name, $value)
  46.     {
  47.         if(property_exists($this, $name))
  48.         {
  49.             $this->$name = $value;
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55. /**
  56.  * @Entity @Table(name="book_options")
  57.  */
  58. class BookOption
  59. {
  60.     /** @Id @Column(type="integer") @GeneratedValue */
  61.     private $id;
  62.  
  63.     /**
  64.      * @ManyToOne(targetEntity="OptionType")
  65.      * @JoinColumn(name="type",referencedColumnName="type", cascade={"persist"})
  66.      */
  67.     private $type;
  68.  
  69.     /**
  70.      * @ManyToOne(targetEntity="Book", inversedBy="options")
  71.      * @JoinColumn(name="book_id", referencedColumnName="id")
  72.      */
  73.     private $book;
  74.  
  75.     /** @Column(type="string") */
  76.     private $value;
  77.  
  78.  
  79.     public function __construct(OptionType $type)
  80.     {
  81.         $this->type = $type;
  82.     }
  83.  
  84.  
  85.  
  86.     public function setBook(Book $book)
  87.     {
  88.         if ($book->options->contains($this)) {
  89.             $this->book = $book;
  90.         }
  91.     }
  92.  
  93.  
  94.  
  95.     public function __get($name)
  96.     {
  97.         if(property_exists($this, $name))
  98.         {
  99.             return $this->$name;
  100.         }
  101.     }
  102.    
  103.     public function __set($name, $value)
  104.     {
  105.         if(property_exists($this, $name))
  106.         {
  107.             $this->$name = $value;
  108.         }
  109.     }
  110.  
  111. }
  112.  
  113.  
  114.  
  115. /**
  116.  * @Entity @Table(name="book_option_types")
  117.  */
  118. class OptionType
  119. {
  120.     /** @Id @Column(type="string",length="80") */
  121.     private $type;
  122.  
  123.     /** @Column(type="string",length="80") */
  124.     private $description;
  125.  
  126.  
  127.     public function __construct($type)
  128.     {
  129.         $this->type = $type;
  130.     }
  131.  
  132.  
  133.  
  134.     public function __get($name)
  135.     {
  136.         if(property_exists($this, $name))
  137.         {
  138.             return $this->$name;
  139.         }
  140.     }
  141.    
  142.     public function __set($name, $value)
  143.     {
  144.         if(property_exists($this, $name))
  145.         {
  146.             $this->$name = $value;
  147.         }
  148.     }
  149.  
  150. }
  151.  
  152.  
  153.  
  154.     $book = new Book();
  155.     $book->title = "Bambi";
  156.     $book->genre = "Pohádka";
  157.  
  158.     $preservance = $entityManager->getRepository('OptionType')->find('Preservance');
  159.     $book->addOption(new BookOption($preservance))->value('Great');
  160.  
  161.     $color = $entityManager->getRepository('OptionType')->find('Color');
  162.     $book->addOption(new BookOption($color))->value('Green');
  163.  
  164.     $entityManager->persist($book);
  165.     $entityManager->flush();
Advertisement
Add Comment
Please, Sign In to add comment