M
M
Maxim2014-01-10 14:26:43
Yii
Maxim, 2014-01-10 14:26:43

Is it necessary to write additional methods in the model in order to pull some data?

For example, if on some page it is necessary to display only the date of birth of the user, is it necessary to write a separate method that would pull out only it from the table? Will this greatly affect the operation of the site, if before that I just loaded the model and took what I needed from it?
Like this for example:

class UserController extends Controller {
   public function getDate($id) {
      $criteria = new CDbCriteria;
      $criteria->select = 'date';
      $data = User::model()->findById($id,$criteria);
      // ....
   }
   // ...
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Kupreichik, 2014-01-10
@Frapsy

Well, I personally write similar functions, I just make them static and push them into the appropriate model, i.e. the same code could be written like this

<?php

class User extends CActiveRecord
{
    // ...
    public static function getDate($id)
    {
        $user = self::model()->findByPk($id, array('select' => 'date'));
        return $user === null ? null : $user->date;
    }
    // ...
}

and in the future use this function like this User::getDate($id)

A
Alexander Zelenin, 2014-01-10
@zelenin

1. work with data should be in the model
2. if you want to get one date of birth, it will not affect.
3. What is the context of the question?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question