Findallbysql With Relation Models


Recently I used findAllbySql but I have two questions

MyModel::model()->findAllbySql("SELECT * FROM MyModel_table WHERE...");

Why have to use both MyModel and MyModel_table (in query) ?

Is not statement MyModel:: … agile enough to include the part of query FROM MyModel_table ? What happens if (FROM MyModel_table) not included?

I thing this is surplus, is there specific reason to implemented findAllbySql and findbySql like that ?

2)According to above

MyModel::model()->with('relatedModel')->findAllbySql("SELECT * FROM MyModel_table LEFT JOIN relatedModel_table WHERE...")

needs JOIN relatedModel_table to works

I know with(’…’) used to load also the relation model, but I refer because

MyModel::model()->with(‘relatedModel’)->findAll(‘t.attr>10 AND relatedModel.arr2<20’); works fine.

So, is there surplus too ?

Many thanks

The findAllBySql() method needs to be able to handle arbitrarily complex SQL expressions. It wouldn’t make sense for the framework to try to inject code just under specific circumstances.

If your queries are simple, they can be easily expressed using standard Active Record methods. If they’re complicated, you probably won’t want the framework to second guess you.

Thank Keith

I wanted to be sure