title: Is it good or bad to use database query in controller yii2?
body: <p>I am creating a project in which I am using database query in Controller. See the example.</p>
  
  <pre><code>public function actionView($id)
          {
              $imodel = $this-&gt;findModel($id);
              return $this-&gt;render('view', [
                  'model' =&gt; $this-&gt;findModel($id),
                  'UserInfo' =&gt; $this-&gt;UserNameById($imodel-&gt;Uid),
  
              ]);
          }
  
         public function UserNameById($id) { 
          $query = (new \yii\db\Query())-&gt;select(['UserName'])-&gt;from('user')-&gt;where(['Id' =&gt; [$id]]);
              $command = $query-&gt;createCommand();
              $data = $command-&gt;queryAll();
              return $data;
         }
  </code></pre>
  
  <p>Just I want to know that is it good or writing query in model file will be better.</p>
  
  <p>like</p>
  
  <pre><code>public function actionView($id)
  {
          $imodel = $this-&gt;findModel($id);
          $model = new User()
          return $this-&gt;render('view', [
              'model' =&gt; $this-&gt;findModel($id),
              'UserInfo' =&gt; $model &gt;UserNameById($imodel-&gt;Uid),
  
          ]);
    }
  </code></pre>
  
  <p>..and now we can write the function in User Model.
  So I want to know which one is better?
  Writing <strong>Database</strong> query in <strong>Model</strong> Or <strong>Controller</strong>.</p>