voskobovich / yii2-many-to-many-behavior

This behavior soon will be DEPRECATED. See the link:

Home Page:https://github.com/voskobovich/yii2-linker-behavior

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

String representation of fields other than ids

larry-tx opened this issue · comments

First many thanks for such an excellent extension of Yii2. I'm glad to see that Yii is getting so many useful extensions in the areas of ManyToMany relations, nested sets, and similar areas. For a long time, CakePHP had it over Yii in these areas, but extensions like you are making it tops.

I find your use of fields, particularly to return strings. I have no problems returning a comma-separated string of ids, but it appears that I should be able to bring back a string of other fields. For instance, if I have a table with id and names columns, I'd like to return a comma separated string of names. (Much more useful than a string of ids.) I just can't get that to work. I know I'm being dense and should be able to figure it out, but could you provide an example for that functionality? It would be greatly appreciated.

Once again, thanks for such great work!

Grateful for a good grade. Please give example of your data situation. So I will be easier to understand your problem.

Actually, it's a very simple situation. I've got two tables as described in the attachment. One table, saint has information about individual saints, while the other, saint_category, has standard categories that can be applied to any saint (for example, doctor of the church, hermit, martyr, etc.). The two tables are joined by a junction table, saint_saint_category. Any saint can have multiple categories, while any single category can be applied to multiple saints. Each model class (Saint and SaintCategory) has the appropriate hasMany viaTable getter applied. I would like to be able to display, in an index-type grid something like, saint: "Paul", saint_categories: "Apostle, Martyr, Evangelist", rather than the IDs for the categories. That seems to be a fairly common problem. Hope my crude example helps.

saint - category

Your problem is not related to my behavior. But I will help you solve the problem.

Do you want to bring all the saints in the table. Do I understand correctly?
Then you need to set a table cell to display category names.

'columns' => [
   [
      'attribute' => 'saint_categories',
      'format' => 'raw',
      'value' => function($model) {
         /** @var $model /app/models/Saint **/
         $items = [];
         foreach($model->categories as $category) {
             $items[] = $category->name;
         }
         return implode(', ', $items);
      }
   ]
]

This is standart operation with connections yii2.
Read more:
http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data
http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html

Many thanks for your response. I'm used to doing it that way, but the way you described "fields" in your instructions made it sound like, to me, that there might be a way to do it through your behavior. My mistake. Keep up the outstanding work!