Where should i put complex sql queries


I’am working on a new project and this question comes another time.

Where should i put sql queries that concerns many active records may be more than 3?

  1. Should i put them in one of the activerecords?

  2. Should i create a class for each type of query (for example one query class for all complex products queries another for customers accounts…). By complex i meant a query that joins many tables!

  3. if it is 2 the best, where should i put those classes? My project is divided into modules and some queries can be used in different modules.

Hope it is too much clear and comprehensible if not please let me know.

Thanks in advance!

It is difficult to understand your question, can you elaborate with an example?

If these are SQLs and you’re getting raw data, put these into separate classes. You may call these repositories or providers or something like that. Yii doesn’t limit you on how you name your classes and where you put them so do as you think is the best for your case.