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

    <tfoot id='hxwPA'></tfoot>
      <bdo id='hxwPA'></bdo><ul id='hxwPA'></ul>

    <legend id='hxwPA'><style id='hxwPA'><dir id='hxwPA'><q id='hxwPA'></q></dir></style></legend>
  • <small id='hxwPA'></small><noframes id='hxwPA'>

      1. Yii2 : ActiveQuery Example 以及在 Gii 中单独生成 ActiveQuery 类的原因是什么

        时间:2023-10-16

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

          <legend id='aJFeI'><style id='aJFeI'><dir id='aJFeI'><q id='aJFeI'></q></dir></style></legend>
        1. <i id='aJFeI'><tr id='aJFeI'><dt id='aJFeI'><q id='aJFeI'><span id='aJFeI'><b id='aJFeI'><form id='aJFeI'><ins id='aJFeI'></ins><ul id='aJFeI'></ul><sub id='aJFeI'></sub></form><legend id='aJFeI'></legend><bdo id='aJFeI'><pre id='aJFeI'><center id='aJFeI'></center></pre></bdo></b><th id='aJFeI'></th></span></q></dt></tr></i><div id='aJFeI'><tfoot id='aJFeI'></tfoot><dl id='aJFeI'><fieldset id='aJFeI'></fieldset></dl></div>

              <tfoot id='aJFeI'></tfoot>
                  <tbody id='aJFeI'></tbody>
                  <bdo id='aJFeI'></bdo><ul id='aJFeI'></ul>
                  本文介绍了Yii2 : ActiveQuery Example 以及在 Gii 中单独生成 ActiveQuery 类的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您能否提供一个示例用法.描述将不胜感激.我找不到一个很好的例子.

                  Could you provide an example usage. Description will be highly appreciated. I can not find a good example for it.

                  推荐答案

                  Active Query 表示与 Active Record 类关联的数据库查询.它通常用于覆盖特定模型的默认 find() 方法,它将用于在发送到 DB 之前生成查询:

                  The Active Query represents a DB query associated with an Active Record class. It is usually used to override the default find() method of a specific model where it will be used to generate the query before sending to DB :

                  class OrderQuery extends ActiveQuery
                  {
                       public function payed()
                       {
                          return $this->andWhere(['status' => 1]);
                       }
                  
                       public function big($threshold = 100)
                       {
                          return $this->andWhere(['>', 'subtotal', $threshold]);
                       }
                  
                  }
                  

                  如果您之前使用过 Yii 1,那么这将取代 Yii 1.x Yii2 中的命名范围.您所要做的就是覆盖 model 类中的 find() 方法以使用上面的 ActiveQuery 类:

                  If you worked before with Yii 1 then this is what replaces Yii 1.x Named Scopes in Yii2. All you have to do is to override the find() method in your model class to use the ActiveQuery class above :

                  // This will be auto generated by gii if 'Generate ActiveQuery' is selected
                  public static function find()
                  {
                      return new appmodelsOrderQuery(get_called_class());
                  }
                  

                  那么你可以这样使用它:

                  Then you can use it this way :

                  $payed_orders      =   Order::find()->payed()->all();
                  
                  $very_big_orders   =   Order::find()->big(999)->all();
                  
                  $big_payed_orders  =   Order::find()->big()->payed()->all();
                  

                  <小时>

                  上面定义的相同 ActiveQuery 类的不同用例是在相关的模型类中定义关系数据时使用它,例如:


                  A different use case of the same ActiveQuery class defined above is by using it when defining relational data in a related model class like:

                  class Customer extends yiidbActiveRecord
                  {
                      ...
                  
                      public function getPayedOrders()
                      {
                          return $this->hasMany(Order::className(),['customer_id' => 'id'])->payed();
                      }
                  }
                  

                  然后您可以通过执行以下操作来急切地加载客户与他们各自的已付款订单:

                  Then you can eager load customers with their respective payed orders by doing :

                  $customers = Customer::find()->with('payedOrders')->all(); 
                  

                  这篇关于Yii2 : ActiveQuery Example 以及在 Gii 中单独生成 ActiveQuery 类的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 yii2 中使用事件? 下一篇:我如何在 Yii2 中处理多对多关系

                  相关文章

                • <small id='T5UM5'></small><noframes id='T5UM5'>

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

                    <tfoot id='T5UM5'></tfoot>
                    1. <i id='T5UM5'><tr id='T5UM5'><dt id='T5UM5'><q id='T5UM5'><span id='T5UM5'><b id='T5UM5'><form id='T5UM5'><ins id='T5UM5'></ins><ul id='T5UM5'></ul><sub id='T5UM5'></sub></form><legend id='T5UM5'></legend><bdo id='T5UM5'><pre id='T5UM5'><center id='T5UM5'></center></pre></bdo></b><th id='T5UM5'></th></span></q></dt></tr></i><div id='T5UM5'><tfoot id='T5UM5'></tfoot><dl id='T5UM5'><fieldset id='T5UM5'></fieldset></dl></div>
                    2. <legend id='T5UM5'><style id='T5UM5'><dir id='T5UM5'><q id='T5UM5'></q></dir></style></legend>