last day (14 days later) » 

11:59 AM
1
A: Yii2 passing an array in the FilterWhere Condition

Семенихин Максимtry this code: if( getFacilityID() ) foreach ($this->getfacilityID() as $key => $id) { $query->orFilterWhere(['like', 'facility_id', $id]); } $query->andFilterWhere(['like', 'area_name', $this->area_name]); $query->all(); Here is ...

 
Can you explain your example clearly? It would be helpful and thank you very much.
 
@MohanPrasad look at last section with result sql query if it is what you looking for (ignore different columns names ) then just grab first code section from answer and test it yourself
 
That works great!!!! So using foreach you can loop the elements and pass it on to the like condition!! but it breaks the code when I have no facilities created. but anyways i have an alternative solution. Thanks very much!!
 
@MohanPrasad of course it will break, usually you should to check if getfacilityID() is empty and skip this logic
 
Yep I have done that... No issues!!
 
11:59 AM
@in my way or else ?
 
Yep just put the foreach inside a if statement. if(!getFacilityID() == null)
 
@MohanPrasad updated the code. So if it slowed your problem then choose as answer )
 
I am using a different solution. I have used my answer which I have answered to this question.
 
ok then no problem
 
I checked it once again, your code is not working for some reason..
 
11:59 AM
Can tell what is not working, error?
 
The same issue, if I have more than 1 facilities it doesn't display the areas.
 
@MohanPrasad lets move to chat discuss it
 
I have only 18 reputations, so cannot move to chat, I think i need 20..
 
@MohanPrasad now it should be enough )
 
Yeah
Thaniks buddy
 
hello.. Yeah I dont understand for some reason your code is breaking..
 
pass my your code
 
<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Area;

/**
 * AreaSearch represents the model behind the search form about `app\models\Area`.
 */
class AreaSearch extends Area
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['area_id', 'area_contact', 'facility_id'], 'integer'],
            [['area_name'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
This is my Area serach code right now
 
12:27 PM
can not find
where are you using me code
 
12:43 PM
Now you asked me for my code right?
I will send you
 public function search($params)
    {
        $query = Area::find()->where(['facility_id'=> $this->getFacilityID()]);
        // 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;
 
1:09 PM
public function search($params)
{
$query = Area::find()->where(['facility_id'=> $this->getFacilityID()]);
// 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([
'area_id' => $this->area_id,
'area_contact' => $this->area_contact,
launch this one
and give me output
 
yii\db\ActiveQuery Object
(
    [sql] =>
    [on] =>
    [joinWith] =>
    [select] =>
    [selectOption] =>
    [distinct] =>
    [from] =>
    [groupBy] =>
    [join] =>
    [having] =>
    [union] =>
    [params] => Array
        (
        )

    [_events:yii\base\Component:private] => Array
        (
        )

    [_behaviors:yii\base\Component:private] => Array
        (
        )

    [where] => Array
        (
            [0] => or
            [1] => Array
                (
                    [0] => or
That is the output
 
seems like ok
can you debug full sql ?
via yii2 debug panel?
 
if(!$this->getFacilityID()==null)
foreach ($this->getfacilityID() as $key => $id) {

$query->orFilterWhere(['in', 'facility_id', $id]);
}
$query->andFilterWhere(['like', 'area_name', $this->area_name]);
$query->all();
now this is working fine
 
if(!$this->getFacilityID()==null)
this is wrong
if($this->getFacilityID()!=null)
should be
 
both give the sameoutput though???
So this is working fine
 
1:16 PM
no
if(!$this->getFacilityID()==null) is complete wrong
 
ok !!!!
Will change it.. everywhere in my code have used that
Thanks buddy..!!!! that was really helpful
 

last day (14 days later) »