Yii Framework Forum: Cdbcriteria Search In Date - Yii Framework Forum

Jump to content

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

Cdbcriteria Search In Date Rate Topic: -----

#1 User is online   AlexInt 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 25-October 13

Posted 18 March 2014 - 09:56 AM

I have a date field and it's working fine. But now I'm making an advanced search so users can easily search by month or year.

In the model I've created 2 public attributes.
public $byMonth;
public $byYear;


I've added them to 'safe', 'on'=>'search'.

This is my search form:
<?php echo $form->label($model,'byMonth'); ?>
<?php echo $form->textField($model,'byMonth',array('maxlength'=>4)); ?>

<?php echo $form->label($model,'byYear'); ?>
<?php echo $form->textField($model,'byYear',array('maxlength'=>4)); ?>


In the month field user will input something like "01" if they are searching for january, "02" for february, etc.
In the year field they will just input the year.

I've been trying a few ways to do it with CDbCrieria but I can't make it work.

Any idea how to solve this?

Thanks
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,481
  • Joined: 04-March 10
  • Location:UK

Posted 18 March 2014 - 10:02 AM

Something like this?

    $criteria->compare('MONTH(your_date_column)', $this->byMonth);
    $criteria->compare('YEAR(your_date_column)', $this->byYear);


That'll probably only work without leading zeroes on the month, but it should get you moving in the right direction.
1

#3 User is online   AlexInt 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 25-October 13

Posted 18 March 2014 - 11:34 AM

View PostKeith, on 18 March 2014 - 10:02 AM, said:

Something like this?

    $criteria->compare('MONTH(your_date_column)', $this->byMonth);
    $criteria->compare('YEAR(your_date_column)', $this->byYear);


That'll probably only work without leading zeroes on the month, but it should get you moving in the right direction.


Thank you for the help, it's working like a charm.

I need to improve my SQL skills :)
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