Как, извиняюсь, и рыбку съесть и все остальное ...?
Контроллер
Код: Выделить всё
public function actionIndex()
{
$searchModel = new CompaniesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->where(['basket'=> '0']);
$data_ = Requisites::find()->all();
foreach ($data_ as $value) {
$data[$value->id]=$value->company_id;
}
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'data' => $data
]);
}
Код: Выделить всё
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "companies".
*
* @property int $id
* @property string $name Краткое наименование
* @property int $basket
*
* @property Requisites[] $requisites
*/
class Companies extends BaseActiveRecord
{
/**
* {@inheritdoc}
*/
public $requisitesName;
public static function tableName()
{
return 'companies';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name'], 'required'],
['name', 'unique'],
[['name'], 'string'],
[['basket'], 'integer'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Краткое наименование организации',
'basket' => 'Удаление (отметить, если удалить)',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getJobs()
{
return $this->hasMany(Jobs::className(), ['company_id' => 'id']);
}
public function getRequisites()
{
return $this->hasMany(Requisites::className(), ['company_id' => 'id']);
}
public function getUsers() {
return $this->hasMany(Users::className(), ['id' => 'user_id'])
->viaTable(
'Jobs',
['company_id' => 'companies.id'],
function ($query) {
$query->andWhere(['users.id' => 'jobs.id']);
});
}
/* Геттер для полного наименования компании */
public function getRequisitesName() {
return $this->Requisites->telfax;
}
}
Код: Выделить всё
<?php
namespace app\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Companies;
/**
* CompaniesSearch represents the model behind the search form of `app\models\Companies`.
*/
class CompaniesSearch extends Companies
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id'], 'integer'],
[['name'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Companies::find();
// add conditions that should always apply here
$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;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
// 'basket' => $this->basket,
]);
$query->andFilterWhere(['like', 'name', $this->name]);
return $dataProvider;
}
}
Код: Выделить всё
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel app\models\CompaniesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Организации';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="companies-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Добавить Организацию', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name:ntext',
['class' => 'yii\grid\ActionColumn'],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{confirm}',
'visibleButtons' => [
'confirm' => true,
],
'buttons' => [
'confirm' => function ($data, $dataProvider) {
return Html::a('Реквизиты', ['/requisites/index', 'company_id'=>$dataProvider->id], ['class' => 'btn btn-primary']);
},
],
],
],
]); ?>
</div>