Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use \Doctrine\Common\Collections\ArrayCollection;
- /**
- * @Entity @Table(name="book")
- */
- class Book
- {
- /** @Id @Column(type="integer") @GeneratedValue */
- private $id;
- /** @Column(type="string",length="80") */
- private $title;
- /** @Column(type="string",length="80") */
- private $genre;
- /** @OneToMany(targetEntity="BookOption", mappedBy="book", cascade={"persist"}) */
- private $options;
- function __construct()
- {
- $this->options = new ArrayCollection();
- }
- public function addOption(BookOptions $option)
- {
- $this->options->add($option);
- $option->book = $this;
- return $option;
- }
- public function __get($name)
- {
- if(property_exists($this, $name))
- {
- return $this->$name;
- }
- }
- public function __set($name, $value)
- {
- if(property_exists($this, $name))
- {
- $this->$name = $value;
- }
- }
- }
- /**
- * @Entity @Table(name="book_options")
- */
- class BookOption
- {
- /** @Id @Column(type="integer") @GeneratedValue */
- private $id;
- /**
- * @ManyToOne(targetEntity="OptionType")
- * @JoinColumn(name="type",referencedColumnName="type", cascade={"persist"})
- */
- private $type;
- /**
- * @ManyToOne(targetEntity="Book", inversedBy="options")
- * @JoinColumn(name="book_id", referencedColumnName="id")
- */
- private $book;
- /** @Column(type="string") */
- private $value;
- public function __construct(OptionType $type)
- {
- $this->type = $type;
- }
- public function setBook(Book $book)
- {
- if ($book->options->contains($this)) {
- $this->book = $book;
- }
- }
- public function __get($name)
- {
- if(property_exists($this, $name))
- {
- return $this->$name;
- }
- }
- public function __set($name, $value)
- {
- if(property_exists($this, $name))
- {
- $this->$name = $value;
- }
- }
- }
- /**
- * @Entity @Table(name="book_option_types")
- */
- class OptionType
- {
- /** @Id @Column(type="string",length="80", unique=TRUE) */
- private $type;
- /** @Column(type="string",length="80") */
- private $description;
- public function __construct($type)
- {
- $this->type = $type;
- }
- public function __get($name)
- {
- if(property_exists($this, $name))
- {
- return $this->$name;
- }
- }
- public function __set($name, $value)
- {
- if(property_exists($this, $name))
- {
- $this->$name = $value;
- }
- }
- }
- $book = new Book();
- $book->title = "Bambi";
- $book->genre = "Pohádka";
- $preservance = $entityManager->getRepository('OptionType')->find('Preservance');
- $book->addOption(new BookOption($preservance))->value = 'Great';
- $color = $entityManager->getRepository('OptionType')->find('Color');
- $book->addOption(new BookOption($color))->value = 'Green';
- $shape = new BookOption('shape');
- $book->addOption($shape)->value = 'Circle';
- $entityManager->persist($book);
- $entityManager->flush();
Advertisement
Add Comment
Please, Sign In to add comment