Yii Framework Forum: Searching records for date fails - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Searching records for date fails [canceled]

#1 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 269
  • Joined: 20-April 17
  • Location:Hannover

Posted 12 August 2017 - 09:57 AM

Hi guys, following code should search records for date. But whatever I click,else branch in condition will be executed,so I will get outprinting:
choice_date is true
.It will be searched for records which are <=department_created_date,but never for records >=department_created_date
Any ideas,how to fix this?
Here is my model:
<?php
namespace backend\models;

use Yii;
use yii\base\Model;
class RadioForm extends Model{ 

    public $choice_date;
    public function rules() {
        return [
            ['choice_date', 'boolean']   
        ];
    }

}
?>

Here is my RadioList
$form = ActiveForm::begin();
$model = new backend\models\RadioForm();
?>
<?= $form->field($model, 'choice_date')->radioList(array('0' => 'Before', '1' => 'After'))->label('Please, choose Datesearching!'); ?>

and here is my 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 $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'], '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;
/*
Whatever I click in RadioList,property will be not 0,so I'll get setFlash->'choice_date is true'
*/
        if ($this->choice_date == "0") {
                Yii::$app->session->setFlash('success', 'choce_date is false');

            $query->andFilterWhere(['<=', 'department_created_date', $this->department_created_date]);
        } else {
              Yii::$app->session->setFlash('success', 'choice_date is true');
            $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;
    }

}

0

#2 User is offline   jkofsky 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 688
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 13 August 2017 - 12:34 AM

It could be that choice_date is being returned from the form as a string '0', not the int 0.
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#3 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 269
  • Joined: 20-April 17
  • Location:Hannover

Posted 13 August 2017 - 04:56 AM

View Postjkofsky, on 13 August 2017 - 12:34 AM, said:

It could be that choice_date is being returned from the form as a string '0', not the int 0.

Well, if I change condition like this....
      if ($this->choice_date == "0") {
            Yii::$app->session->setFlash('success', 'choce_date is false');

            $query->andFilterWhere(['<=', 'department_created_date', $this->department_created_date]);
        } else {
            Yii::$app->session->setFlash('success', 'choice_date is true');
            $query->andFilterWhere(['>=', 'department_created_date', $this->department_created_date]);
        }


...whatever I select in Radio list, this time else-branch will be executed,so I will get outprinting:
choice_date is true

Now, it will be searched for records which are >=department_created_date,but never for records <= department_created_date.

Obviously, choice_date(property of Radio Button) will not be rendered from formular, only in searching class. Here is my formular(index.php in folder departments)
<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\ActiveForm;

$this->title = Yii::t('app', 'Departments');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="departments-index">
    <h1><?= Html::encode($this->title) ?></h1>
    <?php
    $form = ActiveForm::begin();
    $model = new backend\models\DepartmentsSearch();
    ?><?= $form->field($model, 'choice_date')->radioList(array(0 => 'Before', 1 => 'After'))->label('Please, choose Datesearching!'); ?>
    <p><?= Html::a(Yii::t('app', 'Create Departments'), ['create'], ['class' => 'btn btn-success']) ?></p>
    <?=
    GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'branchesBranch.branch_name',
            'department_name',
            'companiesCompany.company_name',
            'department_created_date',
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]);
    ?>
</div>

0

#4 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,418
  • Joined: 16-February 11
  • Location:Japan

Posted 15 August 2017 - 06:59 AM

It looks like that you are using 2 different model instances in the view and in the controller.
0

#5 User is offline   skworden 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 518
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 15 August 2017 - 04:28 PM

Try making your view and models match what the other is looking for


'0' => 'Before', '1' => 'After'

$this->choice_date === '0'

0

#6 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 269
  • Joined: 20-April 17
  • Location:Hannover

Posted 20 August 2017 - 06:39 AM

View Postskworden, on 15 August 2017 - 04:28 PM, said:

Try making your view and models match what the other is looking for


'0' => 'Before', '1' => 'After'

$this->choice_date === '0'



Well, this forum was not able to solve my problem, but stackoverflow did. Here is code of all relevant instances, maybe it's useful:
RadioList:
    <?php $form = ActiveForm::begin();?>
    <?= $form->field($searchModel, 'choice_date')->radioList([0 => 'Before', 1 => 'After'], 
            ['itemOptions' => ['class' => 'choiceRadio']])->label('Please, choose Datesearching!');
    ActiveForm::end();?>


SearchingClass:
<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Departments;

class DepartmentsSearch extends Departments {

    public $choice_date;

    public function rules() {
        return [
            [['department_id', 'branches_branch_id', 'companies_company_id'], 'integer'],
            [['department_name', 'department_created_date', 'department_status'], 'safe'],
            [['choice_date'], 'boolean'],
        ];
    }

    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;
        }
        $query->andFilterWhere([
            'department_id' => $this->department_id,
            'branches_branch_id' => $this->branches_branch_id,
            'companies_company_id' => $this->companies_company_id,
        ]);

        if ($this->choice_date == 0) {
            Yii::$app->session->setFlash('success', 'choice_date is false');

            $query->andFilterWhere(['<=', 'department_created_date', $this->department_created_date]);
        } else {
            Yii::$app->session->setFlash('success', 'choice_date is true');
            $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', 'department_status', $this->department_status]);

        return $dataProvider;
    }

}



GridView:
GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'filterSelector' => '.choiceRadio',
.
.
.

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users