public static function getAuthors($id = null) { $authors = array(); foreach (BooksAuthors::find()->all() as $data) { $authors[$data->id] = $data->fio; } return empty($id) ? $authors : $authors[$id]; }
public static function getAuthorsName($authors = null, $html = false) { $returnArray = array(); $returnHtml = ''; foreach (BooksAuthors::find()->where(!empty($authors) ? 'id in (' . $authors . ')' : '')->all() as $data) { $returnArray[$data->id] = $data->fio; $returnHtml .= $data->fio . '<BR>'; } return !$html ? $returnArray : $returnHtml; }
public function actionSerchbook() { $searchModel = new BooksSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $count = null; if (isset(Yii::$app->request->queryParams['BooksSearch']['authors'])) { $count = \app\models\BooksAuthors::getAuthorBooks((int) Yii::$app->request->queryParams['BooksSearch']['authors']); } return $this->render('serchbook', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'count' => $count]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = BooksAuthors::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'created_at' => $this->created_at]); $query->andFilterWhere(['like', 'fio', $this->fio]); return $dataProvider; }
/** * Finds the BooksAuthors model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return BooksAuthors the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = BooksAuthors::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
echo Html::encode($this->title); ?> </h1> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <?php $label = new \app\models\Books(); ?> <p> <?php echo Html::a('Новая запись', ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'id', ['label' => $label->getAttributeLabel('book_category_id'), 'attribute' => 'book_category_id', 'format' => 'raw', 'value' => function ($data) { return \app\models\Books::getCategorys($data->book_category_id); }, 'filter' => \app\models\Books::getCategorys()], 'name', ['label' => $label->getAttributeLabel('authors'), 'attribute' => 'authors', 'format' => 'raw', 'value' => function ($data) { $author = ''; foreach (\app\models\BooksAuthors::getAuthorsName($data->authors) as $key => $val) { $author .= $val . '<BR>'; } return $author; }, 'filter' => \app\models\BooksAuthors::getAuthorsName()], 'year', 'views', ['class' => 'yii\\grid\\ActionColumn', 'header' => 'Кнопки действий', 'template' => '{view} {update} {delete}']]]); ?> </div>
<?php use yii\helpers\Html; use yii\widgets\DetailView; /* @var $this yii\web\View */ /* @var $model app\models\Books */ $this->title = $model->name; $this->params['breadcrumbs'][] = ['label' => 'Книги', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; ?> <div class="books-view"> <?php $label = new \app\models\Books(); ?> <h1><?php echo Html::encode($this->title); ?> </h1> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['name', ['label' => $label->getAttributeLabel('authors'), 'format' => 'html', 'value' => \app\models\BooksAuthors::getAuthorsName($model->authors, true)], 'year', ['label' => $label->getAttributeLabel('annotation'), 'format' => 'html', 'value' => $model->annotation]]]); ?> </div> <div class="form-group"> <a href="/books" class="btn btn-primary">Вернуться к списку</a> </div>