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

  1. Changelog:
  2. Requirements
  3. Usage

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,
    ));
  }
1 0
2 followers
254 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Database
Developed by: jmariani
Created on: Jan 17, 2012
Last updated: 12 years ago

Downloads

show all

Related Extensions