tklustig
(Kipp Thomas)
July 29, 2017, 5:51pm
1
Hi guys, following code should search records for date. But whatever I click, else-branch in code will be selected
Searching-Class
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Departments;
class DepartmentsSearch extends Departments {
public function rules() {
return [
[['department_id'], 'integer'],
[['department_name', 'department_created_date', 'department_status', 'companies_company_id', 'branches_branch_id'], 'safe'],
];
}
public function scenarios() {
return Model::scenarios();
}
public function search($params) {
$query = Departments::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) return $dataProvider;
$model = new RadioForm();
$model->load($params);
if ($model->choice_date !=true )
$query->andFilterWhere(['<', 'department_created_date', $this->department_created_date]);
else
$query->andFilterWhere(['>', 'department_created_date', $this->department_created_date]);[/b]
$query->joinWith('companiesCompany');
$query->joinWith('branchesBranch');
$query->andFilterWhere(['like', 'department_name', $this->department_name])
->andFilterWhere(['like', 'companies.company_name', $this->companies_company_id])
->andFilterWhere(['like', 'branches.branch_name', $this->branches_branch_id])
->andFilterWhere(['like', 'department_status', $this->department_status]);
return $dataProvider;
}
}
Here is the RadioList:
<?=$form->field($model, 'choice_date')->radioList(array(0=>'Before',1=>'After'))->label('Please, choose Datesearching!'); ?>
Last the Model:
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
class RadioForm extends Model { // A new Class programmed by Thomas Kipp
public $choice_date;
public function rules() {
return [
['choice_date', 'boolean'],
];
}
}
?>
samdark
(Alexander Makarov)
July 30, 2017, 7:50am
2
Check what’s in POST and what’s actually assigned to form (via $this->attributes).
tklustig
(Kipp Thomas)
July 30, 2017, 11:37am
3
I suppose, that property won’t be assigned, 'cause If I change property in model, for instance like this
public $choice_date=false;
date will be filtered as coded(smaller as)
I absolutely have no idea why this property won’t be assigned. The other properties(data entries) -department_name,companies.company_name,branches.branch_name- will be assigned
following result are given by var_dump:
var_dump($model->attributes);
E:\xampp\htdocs\Yii2-Lession\backend\views\departments\index.php:18:
array (size=1)
'choice_date' => null
alirz23
(Alirz23)
July 31, 2017, 5:58am
4
the reason its not being assigned is your form is probably using your Departments model to render the field controls
// make sure your $model here is an instance of RadioForm
<?=$form->field($model, 'choice_date')->radioList(array(0=>'Before',1=>'After'))->label('Please, choose Datesearching!'); ?>
// or move the choice_date to the Departments model.
class DepartmentsSearch extends Departments {
public $choice_date;
public function rules() {
return [
[['department_id'], 'integer'],
[['choice_date'], 'boolean'],
[['department_name', 'department_created_date', 'department_status', 'companies_company_id', 'branches_branch_id', 'choice_date'], 'safe'],
];
}
public function scenarios() {
return Model::scenarios();
}
public function search($params) {
$query = Departments::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) return $dataProvider;
if ($this->choice_date != true)
$query->andFilterWhere(['<', 'department_created_date', $this->department_created_date]);
else
$query->andFilterWhere(['>', 'department_created_date', $this->department_created_date]);
$query->joinWith('companiesCompany');
$query->joinWith('branchesBranch');
$query->andFilterWhere(['like', 'department_name', $this->department_name])
->andFilterWhere(['like', 'companies.company_name', $this->companies_company_id])
->andFilterWhere(['like', 'branches.branch_name', $this->branches_branch_id])
->andFilterWhere(['like', 'department_status', $this->department_status]);
return $dataProvider;
}
}
jkofsky
(Jkofsky)
July 31, 2017, 6:36am
5
Could the problem be that $this->load($params) is clearing out $params so the second $model->load($params) is assigning null?
As indicated from others: I might just add ‘select_date’ to the Departments Model, and use $this->select_date in the if() statement.
tklustig
(Kipp Thomas)
July 31, 2017, 12:01pm
6
select_date doesn’t exist in my files. Furthermore, I’m already using
if ($this->choice_date != true)
in my code!
So, what du u want to explain me with ur assertion:
jkofsky
(Jkofsky)
August 1, 2017, 12:03am
7
tklustig:
select_date doesn’t exist in my files. Furthermore, I’m already using
if ($this->choice_date != true)
in my code!
So, what du u want to explain me with ur assertion:
OK. So I thought it was ‘select_date’.
class someModel extends ActiveRecord {
public $choice_date;
...
rules{
...
['choice_date', 'safe'],
...
}
In the form make your selector (dropdownBox, checkbox, etc) and $model->choice_date.
This is similar to how to set up GridView sorting/filtering of a related table.