title: Yii2 "has no relation named" at search model even the function it is defined in the main model
body: <p>I am having a yii2 error : common\models\Book has no relation named "favorite".</p>
  
  <p>When I try to add:</p>
  
  <pre><code>public function search($params) {
      $query = Book::find();
      $query-&gt;joinWith(['author', 'profile', 'favorite']);
  </code></pre>
  
  <p>In the book model I do have the public function:</p>
  
  <pre><code>public function getFavoritedIcon() {
      if (isset($this-&gt;favorite)) {
          return '&lt;i class="glyphicon glyphicon-asterisk books-form"&gt;&lt;/i&gt;';
      } else {
          return '';
      }
  }
  </code></pre>
  
  <p>And also this extra function to get the icon</p>
  
  <pre><code>public function getFavoritedIcon() {
      if (isset($this-&gt;favorite)) {
          return $icon;
      } else {
          return '';
      }
  }
  </code></pre>
  
  <p>And this works fine in the grid where I want to add sorting and filter:</p>
  
  <pre><code>[
      'label' =&gt; 'Favorites',
      'attribute' =&gt; 'favorite',
      'value' =&gt; 'favoritedIcon',
      'hAlign' =&gt; 'center',
      'vAlign' =&gt; 'middle',
      'format' =&gt; 'raw',
      'width' =&gt; '50px',
  ],
  </code></pre>
  
  <p>I do some different things from another models I am using:</p>
  
  <ul>
  <li>in the grid i get the value as an icon from the book model but i used this before.</li>
  <li>the other thing is that the Favorite model has not the same name that the table but it work fine in the grid</li>
  </ul>
  
  <p>abstract class Favorite extends \yii\db\ActiveRecord
      {</p>
  
  <pre><code>    public static function tableName()
      {
          return 'user_favorite';
      }
  
  
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              [['user_id', 'book_id'], 'required'],
              [['user_id', 'book_id'], 'integer'],
              [['selectedTime'], 'safe']
          ];
      }
  </code></pre>
  
  <p>Any clues what I am doing wrong ?</p>
  
  <p>======================================================</p>
  
  <p>UPDATE after Pedro del Sol answer</p>
  
  <p>There was some errors in the code but the main one was answered by Pedro,  I do had a favorite function in the Book model but not favorites with multiple output.</p>
  
  <p>So now it is working like that:</p>
  
  <p>In the Book model </p>
  
  <pre><code>    public function getFavorite() {
      $userID = Yii::$app-&gt;user-&gt;identity-&gt;id;
      return Favorite::find()-&gt;where(['user_id' =&gt; $userID, 'book_id' =&gt; $this-&gt;id])-&gt;one();
  }
  
  public function getFavorites() {
      $userID = Yii::$app-&gt;user-&gt;identity-&gt;id;
      return $this-&gt;hasMany(Favorite::className(), ['book_id' =&gt; 'id'], ['book_id' =&gt; $this-&gt;id]);
  }
  public function getFavoritedIcon() {
      if (isset($this-&gt;favorite)) {
          return '&lt;i class="glyphicon glyphicon-asterisk books-form"&gt;&lt;/i&gt;';
      } else {
          return '';
      }
  }
  </code></pre>
  
  <p>In the BookSearch model:</p>
  
  <pre><code>    public function search($params) {
      $query = Book::find();
      $query-&gt;joinWith(['favorites']);
  
      $dataProvider = new ActiveDataProvider([
          'query' =&gt; $query,
      ]);
  
      $dataProvider-&gt;setSort([
          'attributes' =&gt; [
              'title',
              'author_id',
              'rights_owner_id',
              'user_favorite.user_id',
          ]
      ]);
  </code></pre>
  
  <p>and the grid view :</p>
  
  <pre><code>        [
          'label' =&gt; 'Favorites',
          'attribute' =&gt; 'user_favorite.user_id',
          'value' =&gt; 'favoritedIcon',
          'hAlign' =&gt; 'center',
          'vAlign' =&gt; 'middle',
          'format' =&gt; 'raw',
          'width' =&gt; '50px',
      ],
  </code></pre>