public function actionAdd() { Yii::app()->getClientScript()->registerCoreScript('jquery'); Yii::app()->getClientScript()->registerCoreScript('jquery.ui'); $cs = Yii::app()->clientScript; $cs->registerCssFile('/js/datepicker/css/' . 'datepicker.css'); $cs->registerScriptFile('/js/' . 'books.js'); // $book = new Books(); $command = Yii::app()->db->createCommand(); if (isset($_POST['Books'])) { $book->attributes = $_POST['Books']; if ($book->validate()) { $book->save(); if (isset($_POST['add_auth']) && !empty($_POST['add_auth'])) { $criteria = new CDbCriteria(); $criteria->compare('lastname', $_POST['add_auth']); $add_author = Authors::model()->find($criteria); //If author exists if (!empty($add_author)) { //insert link book-author to link table $command->insert('lt_books_authors', array('author_id' => $add_author->id, 'book_id' => $book->id)); } } $this->actionIndex(); return true; } } $this->render('edit', array('model' => $book)); }
public function actionAddNewBook() { $transaction = Yii::app()->db->beginTransaction(); try { $book_model = new Books(); $book_attributes = $_POST['Books']; $book_attributes['creation_date'] = date('Y-m-d H:i:s'); $book_model->attributes = $book_attributes; if ($book_model->validate()) { if ($book_model->save()) { $last_insert_book_id = $book_model->id; $relation_array = array(); foreach ($_POST['author_list'] as $author) { $relation_array[] = array('book_id' => $last_insert_book_id, 'author_id' => $author); } $builder = Yii::app()->db->schema->commandBuilder; $command = $builder->createMultipleInsertCommand('authors_books_relation', $relation_array); $command->execute(); $transaction->commit(); Yii::app()->user->setFlash(TbHtml::ALERT_COLOR_SUCCESS, 'Новая книга добавлена'); $this->redirect(array('library/tabs&tab_id=books')); } } } catch (CDbException $e) { $transaction->rollback(); } }