Yii 1.1: current-behavior

A behavior to get the current record on effective dated tables.
2 followers

Effective dated tables are tables that contains and unique date column which is used to determine the current record.

As an example, if you have a table with currency quotations, the current record for today is the one with the newest date OLDER than today.

Changelog:

0.1: First version.

0.2: Fixed for default date. NOW() was not working. Now defaults to date(DateTime::ISO8601).

Requirements

Created and tested with Yii 1.1.9

Usage

Copy the file within the Yii folder structure (example: extensions/behaviors). In your model class, add the following:

public function behaviors() {
    return array(
        'CurrentBehavior' => array('class' => 'ext.behaviors.CurrentBehavior')
    );
  }

Use it like this: (Example of an index action in the controller):

public function actionIndex() {
    $dataProvider = new CActiveDataProvider(MasterAccount::model()->with(
            array('masterAccountCfdVersions' => array(
                'scopes' => array('current')
            ))),
                    array('criteria' =>
                        array('order' => 'alias ASC')
            ));
    $this->render('index', array(
        'dataProvider' => $dataProvider,
    ));
  }

Parameters:

You can pass also parameters for the base date to use and the column name of the model

Base date defaults to NOW()

Column name defaults to "effDt". The column must be a date column.

Example with parameters

public function actionIndex() {
    $dataProvider = new CActiveDataProvider(MasterAccount::model()->with(
            array('masterAccountCfdVersions' => array(
                'scopes' => array('current' => array('2012-12-30', 'created_date'))
            ))),
                    array('criteria' =>
                        array('order' => 'alias ASC')
            ));
    $this->render('index', array(
        'dataProvider' => $dataProvider,
    ));
  }

Total 4 comments

#6605 report it
jmariani at 2012/01/20 09:59am
Re: Example: 3 tables with difference column

Sounds good.

Thank you.

#6596 report it
mrdan32 at 2012/01/19 10:52pm
Example: 3 tables with difference column

I got an idea: I will select columns that I need on 3 tables, use AS query in SQL to make all results have the same column, after that I could use order by DESC.

Unfortunately, I couldn't test code now. Good job^^

#6582 report it
jmariani at 2012/01/19 10:34am
Re: Order By Many Behaviors in May Tables???

Not sure if I got your requirement right, but what you can do is order the records by date DESC and then use LIMIT=10 to get the last 10 records.

#6571 report it
mrdan32 at 2012/01/19 06:37am
Order By Many Behaviors in May Tables???

I've completed my project but i got this issue:

  • You have users, events and comment table
  • You must list 10 behaviors lastest like: user register, user comment and user make an event.

I'm not good at SQL so I dont know what to do! You should make a function to resolve it

Leave a comment

Please to leave your comment.

Create extension