Does writing complex find() or findAll() within Controller violate the MVC rule?


I have been using Yii2 for a few months now and I have a question. Since Yii2 has a unique feature to generate model (Gii) which extends \yii\db\ActiveRecord, I am wondering if calling conditionals, join, order by and group by from Controller violates the MVC rule?

For example, if I call this from Controller :
$model = VisitorData::find()
->joinWith(‘iDVISITOR’) //relation generated by Gii
->andWhere([’<>’, ‘a.ID_STATUS’, ‘3’])

Will that violate the MVC rule?(I should put that snippet in a function within VisitorData Model) or is it okay since I did not build the query from scratch?

Hoping for feedbacks.


1 Like

What’s important is this: Can you test it automatically? Without a complex fixture? It doesn’t matter so much where you put database IO - what’s important is that it’s separated from business logic, the “intelligence” of your system, so that the intelligence can be tested with unit tests (fast and nice) instead of integration tests (slow and hellish), or even worse, manual tests.