E
E
EVOSandru62017-08-23 13:28:20
Yii
EVOSandru6, 2017-08-23 13:28:20

How to correctly use rules in Yii2 in behaviors and is it possible to use a relationship getter there?

Good afternoon
Regarding the first question - if yes, then it is correct like this:

public function rules() {
  return [];
}

or like this:
public function rules() {
  return array_merger($this->owner::rules,[]);
}

Regarding the second question - Is it possible to write a relationship getter directly in the behavior?
public function getRating()
    {
        $className = $this->className;
        return $this->hasMany($className::className(), [$this->attribute => 'id']);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-08-23
@EVOSandru6

about rules:
https://stackoverflow.com/questions/42510332/yii2-...
about links:
yes you can, but your example is not working. $className first already contains the name of the class, and the second time you try to pull the method already at the string.
Here is an example of a behavior that works with links:
https://github.com/la-haute-societe/yii2-save-rela...
Perhaps in your case it is more convenient to use a trait rather than a behavior?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question