douxunwei7083 2013-09-11 15:22
浏览 25
已采纳

CakePHP输出一个选择输入,用于从连接表中选择一个值

I have managed to join 2 of my tables clients and risk_codes to each other where clients.risk_code_id is a foreign key for risk_codes.id.

In my edit view for Clients I ouput a form using the HTML Helper. E.g. to add an input to edit clients.name I would use echo $this->Form->input('name');

Given that risk_codes is a separate table/model how would I output a select dropdown with the options being risk_codes.name and the values being risk_codes.id?

The tables are linked like so:

Client belongsTo RiskCode
RiskCode hasMany Client
  • 写回答

1条回答 默认 最新

  • dsfs64664 2013-09-11 15:48
    关注

    In your RiskCode model make sure that displayField is either set to null or name (the latter is one of the defaults):

    public $displayField = 'name'; // or null;
    

    In the controller set a list of risk codes for the view:

    $this->set('riskCodes', $this->Client->RiskCode->find('list'));
    

    And in the view simply reference the appropriate foreign key field name:

    echo $this->Form->input('risk_code_id');
    

    CakePHP will automatically create an appropriate select list, using the models id and displayField field values from the list set as riskCodes.

    ps. Many questions like this are answered in the Cookbook, and can also be figured out by using CakePHP to bake the controllers and views.

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

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?