/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Fruits::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'weight' => $this->weight, 'somedata1' => $this->somedata1]);
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'color', $this->color]);
     return $dataProvider;
 }
Beispiel #2
0
}
/* //NOT working again 
$sql = (new \yii\db\Query())->insert('vegetables', [
 'name' => 'potato',
 'color' => 'brown',
 'weight' => 25,
 'somedata2' => 1,
]); */
$rows = (new \yii\db\Query())->from('vegetables')->join('LEFT JOIN', 'fruits', 'vegetables.weight = fruits.weight')->all();
//echo '<pre>';
print_r($rows);
//echo '</pre>';
echo '<br>';
//--------------------------------- Active Record
echo '<h3>Active Record</h3>';
$fruitObject = Fruits::find()->where(['color' => 'orange'])->indexBy('name')->all();
//if set 'all()' returns array of objects!!!
echo '<pre>';
print_r($fruitObject);
echo "<br>";
print_r($fruitObject['apricot']->countryFruits);
print_r($fruitObject['apricot']->country);
echo '</pre>';
echo "<br>";
//echo $fruitObject->name;
echo $fruitObject['apricot']->name;
echo "<br>";
print_r($fruitObject['apricot']->countryFruits[0]['code']);
echo "<br>";
print_r($fruitObject['apricot']->country[0]['population']);
/*