ProductQuery.php 3.66 KB
<?php
    
    namespace artbox\catalog\models\queries;
    
    use artbox\catalog\models\Product;
    use artbox\catalog\models\traits\BitmaskTrait;
    use yii\db\ActiveQuery;
    
    /**
     * This is the ActiveQuery class for [[Product]].
     *
     * @see Product
     */
    class ProductQuery extends ActiveQuery
    {
        
        use BitmaskTrait;
        
        public function active()
        {
            return $this->andWhere(
                [
                    'status' => true,
                ]
            );
        }
        
        public function joinOptions(bool $multiple = true)
        {
            return $this->with(
                [
                    'productOptionCompls' => function ($query) {
                        /**
                         * @var ActiveQuery $query
                         */
                        $query->with('lang.alias')
                              ->with('group.lang');
                    },
                ]
            )
                        ->with(
                            [
                                'productOptionExcls' => function ($query) {
                                    /**
                                     * @var ActiveQuery $query
                                     */
                                    $query->with('lang.alias')
                                          ->with('group.lang');
                                },
                            ]
                        )
                        ->with(
                            [
                                $multiple ? 'variants' : 'variant' => function ($query) {
                                    /**
                                     * @var ActiveQuery $query
                                     */
                                    $query->with('image')
                                          ->with(
                                              [
                                                  'variantOptionCompls' => function ($query) {
                                                      /**
                                                       * @var ActiveQuery $query
                                                       */
                                                      $query->with('lang.alias')
                                                            ->with('group.lang');
                                                  },
                                              ]
                                          )
                                          ->with(
                                              [
                                                  'variantOptionExcls' => function ($query) {
                                                      /**
                                                       * @var ActiveQuery $query
                                                       */
                                                      $query->with('lang.alias')
                                                            ->with('group.lang');
                                                  },
                                              ]
                                          );
                                },
                            ]
                        );
        }
        
        /**
         * @inheritdoc
         * @return Product[]|array
         */
        public function all($db = null)
        {
            return parent::all($db);
        }
        
        /**
         * @inheritdoc
         * @return Product|array|null
         */
        public function one($db = null)
        {
            return parent::one($db);
        }
    }