Laravel 存储库模型执行两个查询

Laravel repository model executing two queries

我正在尝试基于此 package

实施标准模型

如果我按以下标准执行连接并获取所需的列,则假设执行单个查询。但是它执行两个查询

select `products`.`product_name`, `skus`.`quantity` from `products` inner join `skus` on `skus`.`products_id` = `products`.`products_id` where `products`.`products_id` = '1'380μshomestead

select * from `products` where `products`.`products_id` = '0' limit 1

为什么它执行两个查询,而我只需要执行第一个?

代码示例如下

控制器

public function __construct(ProductRepository $product_repository){
        $this->product_repository = $product_repository;
    }

    public function all()
    {
        $data = $this->product_repository->getByCriteria(new GetProduct());
        $product = $this->product_repository->find('1');

    }

标准存储库

class getProduct implements CriteriaInterface {

    public function apply($model, RepositoryInterface $repository)
    {
        $model = $model
                ->select('products.product_name', 'skus.quantity')
                ->where('products.products_id', '1')
                ->join('skus', 'skus.products_id', '=', 'products.products_id');
        return $model; 
    }
}

dd($data) 产生以下

Collection {#300 ▼
  #items: array:2 [▼
    0 => Product {#301 ▼
      #primaryKey: "products_id"
      #fillable: []
      #connection: null
      #table: null
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:2 [▶]
      #original: array:2 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => Product {#302 ▼
      #primaryKey: "products_id"
      #fillable: []
      #connection: null
      #table: null
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:2 [▶]
      #original: array:2 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
  ]
}

好吧,我想我们有办法了。您正在执行 2 个查询

这里有一个:

$data = $this->product_repository->getByCriteria(new GetProduct());

相对于:

select `products`.`product_name`, `skus`.`quantity` from `products` inner join `skus` on `skus`.`products_id` = `products`.`products_id` where `products`.`products_id` = '1'

这里还有一个

$product = $this->product_repository->find('1');

相对于:

select * from `products` where `products`.`products_id` = '0' limit 1

如果您计划使用条件然后执行查询,请将 getByCriteria 更改为 pushCriteria,像这样

$data = $this->product_repository->pushCriteria(new GetProduct());

这样做是在告诉存储库您希望所有查询都遵循该条件中指定的内容,因此您接下来的所有查询都将使用该条件。因此,当您执行 $product = $this->product_repository->find('1'); 时,您实际上会得到

->select('products.product_name', 'skus.quantity')
->where('products.products_id', '1')
->join('skus', 'skus.products_id', '=', 'products.products_id')
->find('1');

希望我能帮到您,现在您清楚了为什么您会从该代码中收到 2 个查询。