Yii2使用hasMany关系获取数据

时间:2017-05-17 02:05:56

标签: php mysql yii2

我有一个查询 - name: integration plan: - aggregate: - get: code-from-git-resource params: {depth: 1} passed: [unit-tests] trigger: true - get: redis-docker-image params: {save: true} - get: busybox-docker-image params: {save: true} - task: Run integration tests privileged: true config: platform: linux image_resource: type: docker-image source: repository: amidos/dcind inputs: - name: code-from-git-resource - name: redis-docker-image - name: busybox-docker-image run: path: sh args: - -exc - | source /docker-lib.sh start_docker # Strictly speaking, preloading of images is not required. # However you might want to do it for a couple of reasons: # - If the image is from a private repository, it is much easier to let concourse pull it, # and then pass it through to the task. # - When the image is passed to the task, Concourse can often get the image from its cache. docker load -i redis-docker-image/image docker tag "$(cat redis-docker-image/image-id)" "$(cat redis-docker-image/repository):$(cat redis-docker-image/tag)" docker load -i busybox-docker-image/image docker tag "$(cat busybox-docker-image/image-id)" "$(cat busybox-docker-image/repository):$(cat busybox-docker-image/tag)" # Run the tests container and its dependencies. docker-compose -f code-from-git-resource/example/integration.yml run tests ,可以从表中获取所有ID(10& 16)。问题是如何将它放在activeDataProvider查询中,以便我可以获取所有传递的ID,因为我无法在查询中执行foreach循环。

关联:

$occupier

控制器:

public function getop_occupier()
    {
        return $this->hasMany(OpOccupier::classname(),['id'=>'unit_id']);
    }

我试过了:

$occupier = OpOccupier::find()
                ->where(['unit_id'=>$id])
                ->all();


$dataProviderTranscation = new ActiveDataProvider([
'query' => OpOccupierTrxGroup::find()->where(['or','occupier_id'=>10,'occupier_id'=>16]),]);

它返回错误UNDEFINED INDEX ID。

任何帮助或建议将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

您可以使用lefJoin关系来加入表格

$dataProviderTranscation = 
 new ActiveDataProvider([
'query' => 
      OpOccupierTrxGroup::find()
       ->where(['op_occupier'=>$occupier['id'])
       ->andWhere(['occupier.id'=>[10,16]])
       ->leftJoin('occupier','occupier.id=op_occupier_trx_group.occupier_id')
       ->all();
相关问题