public function up() { $this->createTable(\app\models\Book::tableName(), ['id' => Schema::TYPE_PK, 'name' => Schema::TYPE_STRING . ' NOT NULL', 'date_create' => Schema::TYPE_DATETIME . ' NOT NULL', 'date_update' => Schema::TYPE_TIMESTAMP . ' ON UPDATE CURRENT_TIMESTAMP', 'preview' => Schema::TYPE_STRING, 'date' => Schema::TYPE_DATE . ' NOT NULL', 'author_id' => Schema::TYPE_INTEGER . ' NOT NULL']); $books = [['name' => 'Go Set a Watchman: A Novel', 'preview' => 'http://ecx.images-amazon.com/images/I/51EU92Bi4GL._SL160_PIsitb-sticker-arrow-dp,TopRight,12,-18_SH30_OU01_SL150_.jpg', 'author_id' => 1, 'date' => '2015-07-14'], ['name' => 'StrengthsFinder 2.0', 'preview' => 'http://ecx.images-amazon.com/images/I/41d%2B2MQkK3L._SL160_PIsitb-sticker-arrow-dp,TopRight,12,-18_SH30_OU01_SL150_.jpg', 'author_id' => 2, 'date' => '2007-02-01'], ['name' => 'First 100 Words', 'preview' => 'http://ecx.images-amazon.com/images/I/51dxds64%2ByL._SL160_PIsitb-sticker-arrow-dp,TopRight,12,-18_SH30_OU01_SL150_.jpg', 'author_id' => 3, 'date' => '2011-05-10']]; foreach ($books as $b) { $book = new \app\models\Book(); $book->attributes = $b; $book->date_create = date('Y-m-d H:i:s'); $book->save(); } }
/** * Displays a single Library model. * @param integer $id * @return mixed */ public function actionView($id) { $model = $this->findModel($id); $books = $model->books; $book_ids = []; foreach ($books as $book) { $book_ids[] = $book->book_id; } $searchModel = new \app\models\BookSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $dataProvider->query->andFilterWhere(['book_id' => $book_ids]); $book = new \app\models\Book(); if ($book->load(Yii::$app->request->post())) { if ($model->insertBook($book)) { $this->refresh(); // $book = new \app\models\Book(); } } return $this->render('view', ['model' => $model, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'book' => $book]); }