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,
));
}
Order By Many Behaviors in May Tables???
I've completed my project but i got this issue:
I'm not good at SQL so I dont know what to do! You should make a function to resolve it
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.
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^^
Re: Example: 3 tables with difference column
Sounds good.
Thank you.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.