duanmei1536 2010-07-24 08:14
浏览 29
已采纳

yiiadmin - yii-framework管理界面中的多对多

I am currently extending yii-s admin extension, yiiadmin.

What would be the easiest way to get a listBox multiple-select field in model-create view, that would display many-to-many relation, for example I have a 'pivot' table that holds these article-category relationships, along with Article and Category tables.

I already have these relations defined in models, and I even managed to get some other relations into list view, like Author.name acquired through author_id field in Article table.

Now I want the Article creation form to contain multiple select listBox that would save into this pivot table automatically multiple choices of categories the Article belongs to.

Those would be, therefore, multiple entries/rows into article_category pivot table by one Article submission.

At the same time, the Article table itself does not contain any field that refers to category. The pivot table therefore picks up article's id attribute and connects it with categorie's (another model/table) id and makes a row out of this.

So, for example, I have these tables/models:

Article >>> id | title | author | text


Category >>> id | name | description


ArticleCategory >>> id | article_id | category_id


And I want to populate this third pivot table with multiple rows from multiple select listBox from the Article create view/form of yiiadmin (often through only one submission).

I also want to automatically render category.name from the Category table into this listbox, appropriately related to category id-s so that I can show names from category table, and by choice & submission populate pivot table with id-s of a category (therefore not names)

Is this possible, and what would be the way to do this?

  • 写回答

1条回答 默认 最新

  • doujianguang5506 2010-08-09 22:48
    关注

    Try this extension to more easily save MANY_MANY relationships:
    http://www.yiiframework.com/extension/cadvancedarbehavior/
    (the gii-template-collection uses this and provides a CRelated widget to create populated Relation form elements for you, it works pretty good I've tried it: http://www.yiiframework.com/extension/gii-template-collection/)

    This one claims to do something similar with MANY-to-MANY relationships: http://www.yiiframework.com/extension/save-relations-ar-behavior/

    Also, it's fairly easy to write the code yourself. Just populate a CHtml::listBox with the models it's related to, then in the action iterate through them and add the relationships to the 'pivot' table with some simple SQL. Or create a model for the pivot table and you use regular save() AR methods too. You can do this stuff in the afterSave() model event as well, instead of the controller action.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi