pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      • <bdo id='ikl33'></bdo><ul id='ikl33'></ul>

      <small id='ikl33'></small><noframes id='ikl33'>

    1. <tfoot id='ikl33'></tfoot>

      <i id='ikl33'><tr id='ikl33'><dt id='ikl33'><q id='ikl33'><span id='ikl33'><b id='ikl33'><form id='ikl33'><ins id='ikl33'></ins><ul id='ikl33'></ul><sub id='ikl33'></sub></form><legend id='ikl33'></legend><bdo id='ikl33'><pre id='ikl33'><center id='ikl33'></center></pre></bdo></b><th id='ikl33'></th></span></q></dt></tr></i><div class="tr5ff77" id='ikl33'><tfoot id='ikl33'></tfoot><dl id='ikl33'><fieldset id='ikl33'></fieldset></dl></div>
      <legend id='ikl33'><style id='ikl33'><dir id='ikl33'><q id='ikl33'></q></dir></style></legend>
    2. GridView 中相關模型的過濾器設置

      Filter setup for related model in GridView(GridView 中相關模型的過濾器設置)

        <tbody id='cFPVI'></tbody>

        1. <small id='cFPVI'></small><noframes id='cFPVI'>

          <i id='cFPVI'><tr id='cFPVI'><dt id='cFPVI'><q id='cFPVI'><span id='cFPVI'><b id='cFPVI'><form id='cFPVI'><ins id='cFPVI'></ins><ul id='cFPVI'></ul><sub id='cFPVI'></sub></form><legend id='cFPVI'></legend><bdo id='cFPVI'><pre id='cFPVI'><center id='cFPVI'></center></pre></bdo></b><th id='cFPVI'></th></span></q></dt></tr></i><div class="fvtl7vt" id='cFPVI'><tfoot id='cFPVI'></tfoot><dl id='cFPVI'><fieldset id='cFPVI'></fieldset></dl></div>
              <bdo id='cFPVI'></bdo><ul id='cFPVI'></ul>
              <tfoot id='cFPVI'></tfoot>

                <legend id='cFPVI'><style id='cFPVI'><dir id='cFPVI'><q id='cFPVI'></q></dir></style></legend>
              1. 本文介紹了GridView 中相關模型的過濾器設置的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                限時送ChatGPT賬號..

                我正在嘗試在 Yii2 的 GridView 中設置相關模型的過濾器 小部件,但我不斷收到錯誤,例如過濾器值必須是整數.

                我已經關注了這個問題.現在,我有兩個模型 Services.phpServiceCharge.php.

                ServiceCharge.php 中的關系設置如下:

                公共函數getServiceName(){返回 $this->hasOne(Services::className(),['id'=>'service_name']);}

                ServiceChargeSearch.php中的代碼是這樣的:

                $查詢,]);$dataProvider->sort->attributes['serviceName.services'] = ['asc' =>['serviceName.services' =>SORT_ASC],'desc' =>['serviceName.services' =>SORT_DESC],];$query->joinWith(['serviceName']);$this->load($params);如果 (!$this->validate()) {//如果驗證失敗時不想記錄任何記錄,請取消注釋以下行//$query->where('0=1');返回 $dataProvider;}$query->andFilterWhere(['id' =>$this->id,//'service_name' =>$this->service_name,'room_category' =>$this->room_category,'charges_cash' =>$this->charges_cash,'charges_cashless' =>$this->charges_cashless,])->andFilterWhere(['LIKE', 'serviceName.services', $this->getAttribute('serviceName.services')]);返回 $dataProvider;}}

                在我的 Gridview 中,它是這樣設置的:

                <預><代碼>['屬性'='服務名稱','value'=>'serviceName.services',],

                正確顯示相關模型中的服務名稱.

                我看不出我做錯了什么,但是服務屬性的過濾器字段根本沒有顯示.

                解決方案

                實際上比看起來要簡單得多.

                1. column_name 添加到安全屬性.注意:這應該是關系名稱

                2. 使用查詢添加連接 - 就像 - $query->joinWith(['serviceName','roomCategory']);

                3. 添加過濾條件,如:

                  ->andFilterWhere(['like', 'services.services', $this->service_name])->andFilterWhere(['like', 'room_category.room_category', $this->room_category]);

                4. 如果想添加排序添加如下代碼:

                  $dataProvider->sort->attributes['service_name'] = ['asc' =>['services.services' =>SORT_ASC],'desc' =>['services.services' =>SORT_DESC],];$dataProvider->sort->attributes['room_category'] = ['asc' =>['room_category.room_category' =>SORT_ASC],'desc' =>['room_category.room_category' =>SORT_DESC],];

                5 您還應該設置關系名稱,例如 public $roomCategory

                就是這樣.相關表的排序和過濾都可以完美運行.

                注意:刪除相關列的默認驗證,如整數和gii生成的默認過濾,否則會產生錯誤.

                最新版本更新:

                • 不需要添加公共 $attribute.
                • 也不需要為關系添加安全屬性.
                • 但是您當前模型中要過濾的屬性是添加到必須的安全屬性.
                • 最重要的是在您的 gridview 中,相關屬性必須采用閉包格式.

                就是例子

                <預><代碼>['attribute=''attribute_name','值=函數($數據){返回 $data->relationname->related_table_attribute_name}],

                請記住,您正在使用 relation_name.related_table_attribute_name 過濾器不知何故對我不起作用.

                I am trying to setup the filter for related model in Yii2's GridView widget, but I am keep getting the error like the filter value must be an integer.

                I have followed this question. Now, I have a two models Services.php and ServiceCharge.php.

                In ServiceCharge.php the relation is setup like:

                public function getServiceName()
                    {
                        return $this->hasOne(Services::className(),['id'=>'service_name']);
                    }
                

                In the ServiceChargeSearch.php the code is like this:

                <?php
                
                namespace appmodels;
                
                use Yii;
                use yiiaseModel;
                use yiidataActiveDataProvider;
                use appmodelsServiceCharges;
                
                /**
                 * ServiceChargesSearch represents the model behind the search form about `appmodelsServiceCharges`.
                 */
                class ServiceChargesSearch extends ServiceCharges
                {
                    /**
                     * @inheritdoc
                     */
                    public function attributes()
                    {
                        // add related fields to searchable attributes
                      return array_merge(parent::attributes(), ['serviceName.services']);
                
                    }
                    public function rules()
                    {
                        return [
                            [['id'], 'integer'],
                            [['charges_cash', 'charges_cashless'], 'number'],
                            [['id', 'serviceName.services', 'room_category'], 'safe'],
                        ];
                    }
                
                    /**
                     * @inheritdoc
                     */
                    public function scenarios()
                    {
                        // bypass scenarios() implementation in the parent class
                        return Model::scenarios();
                    }
                
                    /**
                     * Creates data provider instance with search query applied
                     *
                     * @param array $params
                     *
                     * @return ActiveDataProvider
                     */
                    public function search($params)
                    {
                        $query = ServiceCharges::find();
                
                        $dataProvider = new ActiveDataProvider([
                            'query' => $query,
                        ]);
                        $dataProvider->sort->attributes['serviceName.services'] = [
                        'asc' => ['serviceName.services' => SORT_ASC],
                        'desc' => ['serviceName.services' => SORT_DESC],
                        ];
                
                $query->joinWith(['serviceName']); 
                
                        $this->load($params);
                
                        if (!$this->validate()) {
                            // uncomment the following line if you do not want to any records when validation fails
                            // $query->where('0=1');
                            return $dataProvider;
                        }
                
                        $query->andFilterWhere([
                            'id' => $this->id,
                           // 'service_name' => $this->service_name,
                            'room_category' => $this->room_category,
                            'charges_cash' => $this->charges_cash,
                            'charges_cashless' => $this->charges_cashless,
                        ])
                      ->andFilterWhere(['LIKE', 'serviceName.services', $this->getAttribute('serviceName.services')]);
                
                        return $dataProvider;
                    }
                }
                

                and in my Gridview it is setup like this:

                [
                                'attribute'=>'service_name',
                                'value'=>'serviceName.services',
                
                            ],
                

                Which is showing the services name from the related model correctly.

                I am not able to see what I am doing wrong, but the filter field for the attribute for service is not showing at all.

                解決方案

                Actually it is much simpler than it seems.

                1. add the column_name to safe attribute. Note: this should be relation Name

                2. add the join with query - like - $query->joinWith(['serviceName','roomCategory']);

                3. add the filter condition like:

                  ->andFilterWhere(['like', 'services.services', $this->service_name])
                  ->andFilterWhere(['like', 'room_category.room_category', $this->room_category]);
                  

                4. if like to add sorting add the code like:

                  $dataProvider->sort->attributes['service_name'] = [
                      'asc'  => ['services.services' => SORT_ASC],
                      'desc' => ['services.services' => SORT_DESC],
                  ];
                  $dataProvider->sort->attributes['room_category'] = [
                      'asc'  => ['room_category.room_category' => SORT_ASC],
                      'desc' => ['room_category.room_category' => SORT_DESC],
                  ];
                  

                5 you should also set the relation name say public $roomCategory

                That's it. Both sorting and filtering for related table works perfectly.

                Note: Remove default validation like integer for related column and default filtering generated by gii otherwise it will generate an error.

                Update on Latest version:

                • Adding Public $attribute is not needed.
                • Adding safe attribute for relation is also not needed.
                • but the attribute in your current model, which you want filter is to added to safe attribute that is a must.
                • and most importantly in your gridview, the related attribute has to be in closure format.

                that is example

                [
                'attribute=>'attribute_name',
                'value=function($data){
                     return $data->relationname->related_table_attribute_name
                }
                ],
                

                remember it you are using relation_name.related_table_attribute_name filter somehow doesn't work for me.

                這篇關于GridView 中相關模型的過濾器設置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                enable SOAP on PHP(在 PHP 上啟用 SOAP)
                Get received XML from PHP SOAP Server(從 PHP SOAP 服務器獲取接收到的 XML)
                not a valid AllXsd value(不是有效的 AllXsd 值)
                PHP SoapClient: SoapFault exception Could not connect to host(PHP SoapClient:SoapFault 異常無法連接到主機)
                Implementation of P_SHA1 algorithm in PHP(PHP中P_SHA1算法的實現)
                Sending a byte array from PHP to WCF(將字節數組從 PHP 發送到 WCF)
                <tfoot id='FUPXc'></tfoot>

                <small id='FUPXc'></small><noframes id='FUPXc'>

                • <i id='FUPXc'><tr id='FUPXc'><dt id='FUPXc'><q id='FUPXc'><span id='FUPXc'><b id='FUPXc'><form id='FUPXc'><ins id='FUPXc'></ins><ul id='FUPXc'></ul><sub id='FUPXc'></sub></form><legend id='FUPXc'></legend><bdo id='FUPXc'><pre id='FUPXc'><center id='FUPXc'></center></pre></bdo></b><th id='FUPXc'></th></span></q></dt></tr></i><div class="r7hbbjf" id='FUPXc'><tfoot id='FUPXc'></tfoot><dl id='FUPXc'><fieldset id='FUPXc'></fieldset></dl></div>
                      <tbody id='FUPXc'></tbody>

                      1. <legend id='FUPXc'><style id='FUPXc'><dir id='FUPXc'><q id='FUPXc'></q></dir></style></legend>

                        • <bdo id='FUPXc'></bdo><ul id='FUPXc'></ul>
                          主站蜘蛛池模板: 整合营销推广|营销网络推广公司|石家庄网站优化推广公司|智营销 好物生环保网、环保论坛 - 环保人的学习交流平台 | 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 | 一体化隔油提升设备-餐饮油水分离器-餐厨垃圾处理设备-隔油池-盐城金球环保产业发展有限公司 | 净化车间_洁净厂房_净化公司_净化厂房_无尘室工程_洁净工程装修|改造|施工-深圳净化公司 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | 生物风-销售载体,基因,质粒,ATCC细胞,ATCC菌株等,欢迎购买-百风生物 | 【365公司转让网】公司求购|转让|资质买卖_股权转让交易平台 | 真空包装机-诸城市坤泰食品机械有限公司 | 山东包装,山东印刷厂,济南印刷厂-济南富丽彩印刷有限公司 | 超声波乳化机-超声波分散机|仪-超声波萃取仪-超声波均质机-精浩机械|首页 | 全自动端子机|刺破式端子压接机|全自动双头沾锡机|全自动插胶壳端子机-东莞市傅氏兄弟机械设备有限公司 | 陕西华春网络科技股份有限公司 | 智能化的检漏仪_气密性测试仪_流量测试仪_流阻阻力测试仪_呼吸管快速检漏仪_连接器防水测试仪_车载镜头测试仪_奥图自动化科技 | 烟气换热器_GGH烟气换热器_空气预热器_高温气气换热器-青岛康景辉 | 步进电机_agv电机_伺服马达-伺服轮毂电机-和利时电机 | 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 | 哈希余氯测定仪,分光光度计,ph在线监测仪,浊度测定仪,试剂-上海京灿精密机械有限公司 | 美的商用净水器_美的直饮机_一级代理经销商_Midea租赁价格-厂家反渗透滤芯-直饮水批发品牌售后 | 浙江栓钉_焊钉_剪力钉厂家批发_杭州八建五金制造有限公司 | 北京租车公司_汽车/客车/班车/大巴车租赁_商务会议/展会用车/旅游大巴出租_北京桐顺创业租车公司 | 南京办公用品网-办公文具用品批发-打印机耗材采购 | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 航空连接器,航空插头,航空插座,航空接插件,航插_深圳鸿万科 | 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 | 蓄电池在线监测系统|SF6在线监控泄露报警系统-武汉中电通电力设备有限公司 | 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 | 定量包装机,颗粒定量包装机,粉剂定量包装机,背封颗粒包装机,定量灌装机-上海铸衡电子科技有限公司 | 旅游规划_旅游策划_乡村旅游规划_景区规划设计_旅游规划设计公司-北京绿道联合旅游规划设计有限公司 | 东莞市超赞电子科技有限公司 全系列直插/贴片铝电解电容,电解电容,电容器 | KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | 武汉高低温试验机-现货恒温恒湿试验箱-高低温湿热交变箱价格-湖北高天试验设备 | 连续油炸机,全自动油炸机,花生米油炸机-烟台茂源食品机械制造有限公司 | 【直乐】河北石家庄脊柱侧弯医院_治疗椎间盘突出哪家医院好_骨科脊柱外科专业医院_治疗抽动症/关节病骨伤权威医院|排行-直乐矫形中医医院 | ★店家乐|服装销售管理软件|服装店收银系统|内衣店鞋店进销存软件|连锁店管理软件|收银软件手机版|会员管理系统-手机版,云版,App | 一体化隔油提升设备-餐饮油水分离器-餐厨垃圾处理设备-隔油池-盐城金球环保产业发展有限公司 | 化妆品加工厂-化妆品加工-化妆品代加工-面膜加工-广东欧泉生化科技有限公司 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | AGV叉车|无人叉车|AGV智能叉车|AGV搬运车-江西丹巴赫机器人股份有限公司 | 小程序开发公司-小程序制作-微信小程序开发-小程序定制-咏熠软件 | 厦门ISO认证|厦门ISO9001认证|厦门ISO14001认证|厦门ISO45001认证-艾索咨询专注ISO认证行业 |