duanqin7791 2016-06-16 16:31
浏览 72
已采纳

YII2:具有多个主键的下拉列表

I Have table with multiple primary key. I already put primary key function in my model like this.

class Block extends \yii\db\ActiveRecord
...
public static function primaryKey()
{
  return [
     'province_id',
     'city_id',
     'area_id',
     'block_id',
  ];
}

This is my form for search of ListView

<?php echo $form->field($model, 'block_id')->dropDownList(ArrayHelper::map(Block::find()->all(), 'block_id','name'), ['prompt'=>'Choose Block'])->label(false);?> 

I can't get all block for my search form. How should I change 'block_id' to get all of my block in dropdownlist?

Thanks in advance...

  • 写回答

1条回答 默认 最新

  • duanjiang7505 2016-06-18 09:31
    关注

    After couple of try, I give up on use ActiveRecord and use sql query instead.

    My search listview code looks like this.

    use yii\db\Query;
    .....
    $connection = \Yii::$app->db;
    $sql = $connection ->createCommand('SELECT CONCAT(province_id,".",city_id,".",area_id,".",block_id) AS block_full, name FROM block');
    $query = $sql->queryAll();
    echo $form->field($model, 'block_id')->dropDownList(ArrayHelper::map($query, 'block_full','name'), ['prompt'=>'Choose Block'])->label(false);
    

    I used CONCAT to make filter more easy in model. I will explode block_full to get proper id for each primary key.

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

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus