示例#1
0
 /**
  * Stores the book and the aggregates (authors, subjects, items)
  */
 public function store()
 {
     // stores the Book
     parent::store();
     // delete the aggregates
     $criteria = new TCriteria();
     $criteria->add(new TFilter('book_id', '=', $this->id));
     $repository = new TRepository('BookAuthor');
     $repository->delete($criteria);
     $repository = new TRepository('BookSubject');
     $repository->delete($criteria);
     $repository = new TRepository('Item');
     $repository->delete($criteria);
     // store the authors
     if ($this->authors) {
         foreach ($this->authors as $author) {
             $book_author = new BookAuthor();
             $book_author->book_id = $this->id;
             $book_author->author_id = $author->id;
             $book_author->store();
         }
     }
     // store the subjects
     if ($this->subjects) {
         foreach ($this->subjects as $subject) {
             $book_subject = new BookSubject();
             $book_subject->book_id = $this->id;
             $book_subject->subject_id = $subject->id;
             $book_subject->store();
         }
     }
     // store the items
     if ($this->items) {
         foreach ($this->items as $item) {
             $item->book_id = $this->id;
             $item->store();
         }
     }
 }
示例#2
0
 /**
  * Stores the book and the aggregates (authors, subjects, items)
  */
 public function store()
 {
     // stores the Book
     parent::store();
     // delete the aggregates
     $criteria = new TCriteria();
     $criteria->add(new TFilter('book_id', '=', $this->id));
     $repository = new TRepository('BookAuthor');
     $repository->delete($criteria);
     $repository = new TRepository('BookSubject');
     $repository->delete($criteria);
     // collect persistent item ids
     if ($this->items) {
         foreach ($this->items as $item) {
             if ($item->id) {
                 $item_ids[] = $item->id;
             }
         }
     }
     // delete all items, except for those that persist
     $criteria->add(new TFilter('id', 'NOT IN', $item_ids));
     $repository = new TRepository('Item');
     $repository->delete($criteria);
     // store the authors
     if ($this->authors) {
         foreach ($this->authors as $author) {
             $book_author = new BookAuthor();
             $book_author->book_id = $this->id;
             $book_author->author_id = $author->id;
             $book_author->store();
         }
     }
     // store the subjects
     if ($this->subjects) {
         foreach ($this->subjects as $subject) {
             $book_subject = new BookSubject();
             $book_subject->book_id = $this->id;
             $book_subject->subject_id = $subject->id;
             $book_subject->store();
         }
     }
     // store the items
     if ($this->items) {
         foreach ($this->items as $item) {
             $item->book_id = $this->id;
             $item->store();
         }
     }
 }