dongtaigan1594 2017-03-24 17:26
浏览 19
已采纳

匿名函数中的Yii2 $ defaultValue

Not a big problem,but i wanna figure it out why. I was reading at Yii2 By Example. And i noticed that view file in the exmaple of a db Transaction. it's about dropdownlist,here is the code:

<?= $form->field($reservation, "room_id")->
dropDownList(ArrayHelper::map(Room::find()->all(), 'id', 
function($room, $defaultValue) {
return sprintf('Room n.%d at floor %d', $room->room_number, $room->floor);
})); ?>

I tried it with and without the $defaultValue.Both worked. so what is the use of the "$defaultValue"? or it just a non-sence. I googled it and have no idea. Thank you for your time.

  • 写回答

1条回答 默认 最新

  • douzhanjia0773 2017-03-24 17:56
    关注

    As you can see in the yii2 reference http://www.yiiframework.com/doc-2.0/yii-helpers-basearrayhelper.html#map()-detail

    in the map() function of the ArrayHelper you can use for from, to and group param a clousure too ..

    then in the clusere you can pass th the inner function a list o param that are "visible" inside the function in you case you don't use $defaultValue so you could avoid to pass this var

      ArrayHelper::map(Room::find()->all(), 'id', 
      function($room) {
      return sprintf('Room n.%d at floor %d', $room->room_number, $room->floor);
      })
    

    You could use also $test var and use it eg:

      $test = 'Big ';
      ArrayHelper::map(Room::find()->all(), 'id', 
      function($room) use($test) {
      return sprintf( $test . 'Room n.%d at floor %d', $room->room_number, $room->floor);
      })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题