protected function execute(InputInterface $input, OutputInterface $output)
 {
     $bookId = $input->getArgument(AddBookAuthorCommand::BOOK_ID);
     $authorId = $input->getArgument(AddBookAuthorCommand::AUTHOR_ID);
     $book = $this->getBookIfExists($output, $bookId);
     $author = $this->getAuthorIfExists($output, $authorId);
     $oldBookAuthor = BookAuthorQuery::create()->findPk(array($bookId, $authorId));
     if ($oldBookAuthor == NULL) {
         $output->writeln("Book " . $bookId . " - author " . $authorId . "association already exists.");
         return;
     }
     $bookAuthor = new BookAuthor();
     $bookAuthor->setAuthor($author);
     $bookAuthor->setBook($book);
     $bookAuthor->save();
     $output->writeln("Book " . $bookId . " - author " . $authorId . "association successfully added.");
 }
Exemple #2
0
 public function save($data)
 {
     $ret = parent::save($data);
     if (isset($data['author'])) {
         $id = $this->getId();
         $authors = explode(";", $data['author']);
         foreach ($authors as $author) {
             $a = new Author();
             $a->setName($author);
             $a->commit();
             $bookAuthor = new BookAuthor();
             $bookAuthor->setBookId($id);
             $bookAuthor->setAuthorId($a->getId());
             $bookAuthor->commit();
         }
     }
     return $ret;
 }
 private function getAuthorsFromBid($bid)
 {
     //все класы авторов, что привязаны к индификатору книги
     $book_author = BookAuthor::model()->with('books')->findAllByAttributes(array('bid' => $bid));
     $aids = array();
     foreach ($book_author as $class) {
         $aids[] = $class->aid;
     }
     $authors = Authors::model()->findAllByAttributes(array('aid' => $aids));
     //формирую красивый масив для результата функции
     $result = array();
     if (!empty($authors)) {
         foreach ($authors as $author) {
             $result['name'][] = $author->name;
             $result['surname'][] = $author->surname;
         }
     }
     return $result;
 }
Exemple #4
0
 public function removeAuthor($author_id)
 {
     $pk = array('book_id' => $this->id, 'author_id' => $author_id);
     BookAuthor::model()->deleteByPk($pk);
 }
Exemple #5
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();
         }
     }
 }
Exemple #6
0
 protected function saveAssociation($model, $author)
 {
     // record book/author association
     $ba = new BookAuthor();
     $ba->book_id = $model->id;
     $ba->author_id = $author->id;
     $ba->save();
 }
Exemple #7
0
// Drop tables.
$bookAuthor = new BookAuthor();
$bookAuthor->drop()->yesImSure();
$book = new Book();
$book->drop()->yesImSure();
$bookAuthor = new BookAuthor();
$bookAuthor->drop()->yesImSure();
*/
/**
 * Auto create database tables. This is just for this demo/sample
 */
$book = new Book();
if (!$book->exists()) {
    $book->createTable();
}
$author = new Author();
if (!$author->exists()) {
    $author->createTable();
}
$bookAuthor = new BookAuthor();
if (!$bookAuthor->exists()) {
    $bookAuthor->createTable();
}
LudoDB::enableLogging();
$request = $_GET['request'];
$data = array();
foreach ($_POST as $key => $value) {
    $data[$key] = $value;
}
$handler = new LudoDBRequestHandler();
echo $handler->handle($request, $data);
Exemple #8
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();
         }
     }
 }
Exemple #9
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getBookAuthors()
 {
     return $this->hasMany(BookAuthor::className(), ['author_id' => 'id']);
 }