Код: Выделить всё
public function getProducts()
{
return $this->hasMany(Product::className(), ['group_id' => 'id']);
}
Код: Выделить всё
public function getOffers()
{
return $this->hasMany(Offer::className(), ['product_id' => 'id']);
}
Пока выводит ошибку что не находит groupName
вид offer/index
Код: Выделить всё
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel app\modules\admin\models\OfferSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Offers';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="offer-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Offer', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
'accounting_id',
'product_id',
[
'attribute' => 'groupName',
'value' => function($data){
return $data->product->group->name;
},
],
'remnant',
'is_active',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
Код: Выделить всё
<?php
namespace app\modules\admin\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\admin\models\offer;
use app\modules\admin\models\group;
use app\modules\admin\models\product;
/**
* OfferSearch represents the model behind the search form of `app\modules\admin\models\offer`.
*/
class OfferSearch extends offer
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'product_id', 'is_active'], 'integer'],
[['name', 'accounting_id'], 'safe'],
[['remnant'], 'number'],
[['groupName'], '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 = offer::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,
'product_id' => $this->product_id,
'groupName' => [
'asc' => ['group.name' => SORT_ASC],
'desc' => ['group.name' => SORT_DESC],
'label' => 'Группа'
],
'remnant' => $this->remnant,
'is_active' => $this->is_active,
]);
$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');
$query->joinWith(['product']);
$query->joinWith(['group']);
return $dataProvider;
}
$query->andFilterWhere([
'offer.id' => $this->id,
'offer.product_id' => $this->product_id,
'offer.accounting_id' => $this->accounting_id,
'offer.name' => $this->name,
]);
$query->andFilterWhere([
'or',
['like', 'offer.name', $this->name],
['like', 'offer.accounting_id', $this->accounting_id],
]);
$query->andFilterWhere(
['group.id' => 'product.group_id'])
->viaTable(
'group',
['id' => 'product.group_id'],
function ($query) {
$query->andWhere([
'or',
['like', 'group.name', "%' . $this->groupName . '%"],
]);
}
);
// return $this->hasOne(Companies::className(), ['id' => 'id_company'])->viaTable('contacts', ['id' => 'contracts_id']);
return $dataProvider;
}
}