如何将建筑查询拆分为多行?

How to split building query into multiple lines?

我想构建基本查询即:

            $query = $this->find('all')
            ->contain("Assets")
            ->order(['Transactions.created' => "DESC"])
            ->limit($count);

然后如果(满足某些条件)添加

            ->where(['asset_id' => $asset_id])

我可以吗?或者我应该在两个版本中重复整个查询(有和没有 'where' 部分)?

尝试这样的事情:

$query = $this->find('all')
    ->contain([
        'Assets' => function ($assets) use ($asset_id) {
            if (isset($asset_id)) {
                $assets->where([
                    'Assets.asset_id' => $asset_id
                ]);
            }

            return $assets;
        }
    ])->order(['Transactions.created' => "DESC"])
    ->limit($count);

简答:

if(isset($asset_id)){
   $query->where(['asset_id' => $asset_id]);
}