S
S
Sergey Melnikov2015-04-28 18:18:31
Yii
Sergey Melnikov, 2015-04-28 18:18:31

What does the construct in the class call after Users::model() mean?

Greetings. I'm learning php, yii has this code out of the box:
$model=Users::model()->findByPk($id);
In this code, we are accessing the static model method of the Users class, but what comes next?
I even saw constructions like Users::model()->findByPk($id)-> something else;
I looked at good lessons on OOP PHP, nowhere it is told what it is....

Answer the question

In order to leave comments, you need to log in

6 answer(s)
R
Rockman, 2015-04-28
@1Rockman

Read about the call chains (Method Chaining).

D
Dmitry Entelis, 2015-04-28
@DmitriyEntelis

judging by the Users::model() code, it returns some object on which the findByPk method is called with the $id parameter

S
Sergey, 2015-04-28
Protko @Fesor

User::model - read about static methods. Roughly speaking, this method, through late static binding, takes the name of the class through which we accessed this method (in the example it is User) and requests ... something like a repository (something like, because in yii everything is not like people , and this repository is also a metadata manager and a query builder ... to simplify). Further, the method simply returns the repository for the model with this class.
Next is method chaining. Each call to an object's method returns that same object. This is how the configuration is done.

G
GreatRash, 2015-04-28
@GreatRash

Pattern Singleton campaign.

A
avada kedavra, 2015-04-28
@kedavra

Users::model() returns an object that has a findByPk($id) method, which is called immediately. Open the Users class and see what and how it works inside.

S
Sergey Semenko, 2015-04-28
@abler98

This is such a singleton

public static function model($className=__CLASS__)
  {
    if(isset(self::$_models[$className]))
      return self::$_models[$className];
    else
    {
      $model=self::$_models[$className]=new $className(null);
      $model->attachBehaviors($model->behaviors());
      return $model;
    }
  }

And here is the findByPk method
public function findByPk($pk,$condition='',$params=array())
  {
    Yii::trace(get_class($this).'.findByPk()','system.db.ar.CActiveRecord');
    $prefix=$this->getTableAlias(true).'.';
    $criteria=$this->getCommandBuilder()->createPkCriteria($this->getTableSchema(),$pk,$condition,$params,$prefix);
    return $this->query($criteria);
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question