Stored procs with Yii?


I’m working on an app that is releasing shortly, and I need a way to easily convert all my current Active Record queries over to MySQL stored procedures. I’ve never used stored procs before, but from what I have read it looks easy enough. The trouble is, I’m currently coding everything using Yii’s AR (which is GREAT) but the stored procs are not done yet by our DBA. Is there an easy way (without breaking anything/doing much coding) to just override the “find” method for my models (and other methods)? I understand Yii does a bunch of DB stuff in the background, but I just need a way so I can implement stored procs WITHOUT re-coding all my find() calls and everything - I can do that, its fine, I’m just trying to find a way to limit coding because I need to get this thing working in a month.

Also, I’m assuming stored procs with yii are pretty simple? Something like User::model()->execute(‘stored proc’) ?

What I was thinking of doing was this:

Change all my Models from extending Yii’s Active Record, to extending my custom class which has its own “find” method (and other methods like “findAll”) that just executes the stored proc in the DB. I’m assuming all my code will work correctly with little fuss this way? Will I have to also override “setAttributes” method or is this done automatically ? Keep in mind… in the production product we cannot be using active record, as my DBA is convinced otherwise (although if you can offer some convincing reasons why AR may be comparable to stored procs I might be able to convince them otherwise).

Thank you,


I’m having a similar problem. Can anyone help here, please?

I haven’t yet tried the stored procedure… but here are two interesting threads about them:

Maybe you will get some idea from them

@mdomba: Thank you for useful links. Really good read. As always! :]

@MichaelMackus: I had some very basic attempts of using stored procedures in Yii, but when connecting to Oracle DB. There shouldn’t be much difference between it and MySQL. But these are only beginners attempts, so I can’t actually paste any code example here right now. I’m in the same situation as you are right now, I’m waiting for my DBA to finish stored procedures to start using it. If I get to some interesting conclusions, I’ll for sure post here.

@mdomba: Thank you for the constructive posts, I scanned through them and I’m sure I can make use of them.

@Trejder: Thanks, and I will definitely post my results as well. This is my first time working with Yii so I’m sure this will be interesting ;)

I realize this thread is old, but you said you would post your results, that was two years ago :) If you solved your issue could you share your results?