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

Hello,

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()
->alias(‘a’)
->joinWith(‘iDVISITOR’) //relation generated by Gii
->where([
‘TYPE’=>‘NEW VISITOR’
])
->andWhere([’<>’, ‘a.ID_STATUS’, ‘3’])
->orderBy([‘a.NAME’=>SORT_ASC])
->all();

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.

Thanks!

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.

2 Likes