This class allows accessing DB tables in an AR-like fashion, without the need to create a class for each table.
Copy class to your project (eg to protected/components). Create a new instance by specifying table name:
$userModel = DynamicActiveRecord::forTable('user');
And then use it like any other AR class:
//list existing users foreach ($userModel->findAll() as $user) echo $user->id . ': ' . $user->name . '<br>'; //add new user $userModel->name = 'Pavle Predic'; $userModel->save();
Total 3 comments
in many many relations . if our database don't support the cascade deletion , we will have to delete the connection manually by ourselves . the better way is that generate a ActiveRecord for the bridge table . then
but it seems the ConnectionTable is only useful in such scenario (or you manually construct the connection)! so dynamicActiveRecord come here !
Yes, you do lose a lot of AR functionality. But the idea behind this class is that sometimes you simply need to perform a basic read/write on a table and it doesn't warrant creating a whole new class for it. If you need validation, relations etc. then use the good old way. To be honest, I'm not entirely sure what this class is good for. I wrote it simply because someone on Yii forum was asking if this was possible in Yii and I guess this proves that: Yes It Is ;)
you cannot use 'massive assignements', validation, relations, and stuff like that... They are configured in separate functions of each model definition, and this is it's all about with ActiveRecord...
Leave a comment
Please login to leave your comment.