Good day. In the gridView table, I collect one of the fields (full name of manager) from the three columns of the ‘USERS’ table (last name first name surname). I need to do a search on this column. The search model works well if I pass only one column name (for example, last name) to the AndFilterWhere method
$query->andFilterWhere([‘like’, ‘users.last_name’, $this->resp_manager]);
But this works only if I type user last name in field and doesn’t work if I type first name or surname.
As soon as I pass an array (last name, middle name) when searching,
Example:
$query->andFilterWhere([‘like’, [‘users.last_name’,‘users.first_name’,‘users.surname’], $this->resp_manager]);
I get an array to string conversion error. Please help me figure out this error and find a way to solve it
yii\base\ErrorException: Array to string conversion in C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\db\conditions\LikeConditionBuilder.php:83
Stack trace:
#0 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\db\conditions\LikeConditionBuilder.php(83): yii\base\ErrorHandler->handleError()
#1 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\db\QueryBuilder.php(288): yii\db\conditions\LikeConditionBuilder->build()
#2 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\db\QueryBuilder.php(1580): yii\db\QueryBuilder->buildExpression()
#3 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\db\QueryBuilder.php(1357): yii\db\QueryBuilder->buildCondition()
#4 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\db\QueryBuilder.php(235): yii\db\QueryBuilder->buildWhere()
#5 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\db\ActiveQuery.php(328): yii\db\QueryBuilder->build()
#6 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\db\Query.php(481): yii\db\ActiveQuery->createCommand()
#7 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\db\ActiveQuery.php(352): yii\db\Query->queryScalar()
#8 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\db\Query.php(368): yii\db\ActiveQuery->queryScalar()
#9 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\data\ActiveDataProvider.php(168): yii\db\Query->count()
#10 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\data\BaseDataProvider.php(169): yii\data\ActiveDataProvider->prepareTotalCount()
#11 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\data\ActiveDataProvider.php(105): yii\data\BaseDataProvider->getTotalCount()
#12 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\data\BaseDataProvider.php(101): yii\data\ActiveDataProvider->prepareModels()
#13 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\data\BaseDataProvider.php(114): yii\data\BaseDataProvider->prepare()
#14 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\data\BaseDataProvider.php(155): yii\data\BaseDataProvider->getModels()
#15 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\widgets\BaseListView.php(190): yii\data\BaseDataProvider->getCount()
#16 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\widgets\BaseListView.php(158): yii\widgets\BaseListView->renderSummary()
#17 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\grid\GridView.php(326): yii\widgets\BaseListView->renderSection()
#18 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\widgets\BaseListView.php(135): yii\grid\GridView->renderSection()
#19 [internal function]: yii\widgets\BaseListView->yii\widgets{closure}()
#20 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\widgets\BaseListView.php(138): preg_replace_callback()
#21 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\grid\GridView.php(301): yii\widgets\BaseListView->run()
#22 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\base\Widget.php(146): yii\grid\GridView->run()
#23 C:\OSPanel\domains\timer-cabinet\views\claims\index.php(38): yii\base\Widget::widget()
#24 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\base\View.php(347): require(‘C:\OSPanel\doma…’)
#25 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\base\View.php(257): yii\base\View->renderPhpFile()
#26 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\base\View.php(156): yii\base\View->renderFile()
#27 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\base\Controller.php(407): yii\base\View->render()
#28 C:\OSPanel\domains\timer-cabinet\controllers\ClaimsController.php(85): yii\base\Controller->render()
#29 [internal function]: app\controllers\ClaimsController->actionIndex()
#30 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\base\InlineAction.php(57): call_user_func_array()
#31 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\base\Controller.php(178): yii\base\InlineAction->runWithParams()
#32 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\base\Module.php(552): yii\base\Controller->runAction()
#33 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction()
#34 C:\OSPanel\domains\timer-cabinet\vendor\yiisoft\yii2\base\Application.php(384): yii\web\Application->handleRequest()
#35 C:\OSPanel\domains\timer-cabinet\web\index.php(12): yii\base\Application->run()
#36 {main}
—*******—
view file index.php
<?php
/** @var View $this */
/** $dataProvider from User Model */
use yii\grid\GridView;
use yii\web\View;
use yii\helpers\Html;
use yii\widgets\Pjax;
use app\models\Claim;
use yii\grid\ActionColumn;
use yii\helpers\Url;
?>
<h1>Справочник заявок</h1>
<?php
echo Html::a('Создать заявку', ['add'], ['class' => 'btn btn-success']);
?>
<?php Pjax::begin() ?>
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
// ['class' => 'yii\grid\SerialColumn'],
'claim_id',
['attribute' => 'org_name' ,'content' => function($data){
return $data->organization->organization_name;
}],
['attribute' => 'resp_manager' ,'content' => function($data){
return $data->manager->getFullName();
// this is getter that I created in the User model class and get from the 'hasOne' connection in Claim Model
}],
'description',
'date_of_creation',
['class' => ActionColumn::class,
'urlCreator' => function($action,Claim $model,$key,$index,$column){
return Url::toRoute([$action, 'claim_id' => $model->claim_id]);
}],
],
]);
?>
<?php Pjax::end() ?>
---*******---
search model class
<?php
namespace app\models;
use app\models\Claim;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\User;
/**
* ClaimSearch represents the model behind the search form of `app\models\Claim`.
*/
class ClaimSearch extends Claim
{
/**
* {@inheritdoc}
*/
public string $org_name = '';
public string $resp_manager = '';
public function rules() :array
{
return [
[['claim_id', 'organization_id', 'manager_id'], 'integer'],
[['description', 'date_of_creation','org_name','resp_manager'], '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 = Claim::find()->joinWith('manager',)->joinWith('organization');
// add conditions that should always apply here
$model = new Claim();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' =>[
'attributes' => [
'claim_id',
'org_name' => [
'asc' => ['organizations.organization_name' => SORT_ASC],
'desc' => ['organizations.organization_name' => SORT_DESC],
],
'resp_manager' => [
'asc' => ['users.last_name' => SORT_ASC, 'users.first_name' => SORT_ASC, 'users.surname' => SORT_ASC],
'desc' => ['users.last_name' => SORT_DESC, 'users.first_name' => SORT_DESC, 'users.surname' => SORT_DESC],
],
'description',
'date_of_creation',
],
],
]);
$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([
'claim_id' => $this->claim_id,
'organization_id' => $this->organization_id,
'manager_id' => $this->manager_id,
'date_of_creation' => $this->date_of_creation,
]);
$fullname = ['users.last_name','users.first_name','users.surname'];
$query->andFilterWhere(['like', 'description', $this->description]);
$query->andFilterWhere(['like', 'organizations.organization_name',$this->org_name]);
$query->andFilterWhere(['like', ['users.last_name','users.first_name','users.surname'], $this->resp_manager]);
return $dataProvider;
}
}
---*******---
Model class
<?php
namespace app\models;
use Yii;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "claims".
*
* @property int $claim_id
* @property int|null $organization_id
* @property int|null $manager_id
* @property string|null $description
* @property string|null $date_of_creation
*
* Название моделей здесь в единственном числе, название таблицы в БД во множественном
* @property User $manager
* @property Organization $organization
*/
class Claim extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'claims';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['organization_id', 'manager_id'], 'integer'],
[['description'], 'string'],
[['date_of_creation'], 'safe'],
[['manager_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['manager_id' => 'id']],
[['organization_id'], 'exist', 'skipOnError' => true, 'targetClass' => Organization::class, 'targetAttribute' => ['organization_id' => 'organization_id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'claim_id' => 'Номер заявки',
'organization_id' => 'Идентификатор фирмы',
'manager_id' => 'Идентификатор менеджера',
'description' => 'Описание',
'date_of_creation' => 'Дата создания',
'org_name' => 'Название организации',
'resp_manager' => 'Ответственный менеджер',
];
}
/**
* Gets query for [[Manager]].
*
* @return \yii\db\ActiveQuery
*/
public function getManager()
{
return $this->hasOne(User::class, ['id' => 'manager_id']);
}
/**
* Gets query for [[Organization]].
*
* @return \yii\db\ActiveQuery
*/
public function getOrganization()
{
return $this->hasOne(Organization::class, ['organization_id' => 'organization_id']);
}
}