Cakephp 中关联 Table 的多个引用

Multiple References to Associated Table in Cakephp

将两个 tables(属于天气雷达站点的许多天气雷达帧)与 belongsTo 和 hasMany 相关联后,我希望生成带有两个 "contains" 的 JSON相同 table (和不同的条件)所以我只能列出具有特定条件的帧。我目前拥有的不起作用;它只将第二个包含写入 JSON.

雷达站点Table:

...
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('radar_site');

    $this->setDisplayField('radar_id');
    $this->setPrimaryKey('radar_id');

    $this->hasMany('FutureFrame', [
        'foreignKey' => 'radar_id'
    ]);

    $this->hasMany('RadarFrame', [
        'foreignKey' => 'radar_id'
    ]);
}
...

雷达坐标系Table:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('radar_frame');

    $this->setDisplayField('radar_id');
    $this->setPrimaryKey('radar_id');

    $this->belongsTo('RadarSite', [
        'foreignKey' => 'radar_id'
    ]);
}

控制器:

public function getRadarData()
{
    $radarInfo = 
    $this->RadarSite->find(
        'all',
        [
            'contain' => [
                'RadarFrame' =>[
                    'sort' => ['RadarFrame.frame_time' => 'DESC'],
                    'conditions' => [
                        'RadarFrame.frame_time >' => time() - 60*60,
                        'RadarFrame.file_on_server =' => 1,
                        'RadarFrame.radar_type =' => 'velocity'
                    ]
                ],
                'RadarFrame' =>[
                    'sort' => ['RadarFrame.frame_time' => 'DESC'],
                    'conditions' => [
                        'RadarFrame.frame_time >' => time() - 60*60,
                        'RadarFrame.file_on_server =' => 1,
                        'RadarFrame.radar_type =' => 'reflectivity'
                    ]
                ]                   
            ]
        ]

    );    
    $this->set('radarInfo', $radarInfo);
}

JSON 输出:

 ...,{
        "radar_id": "KHGX",
        "radar_name": "Houston\/Galveston, TX",
        "elevation": 18,
        "tower_height": 20,
        "max_latitude": 31.6485,
        "min_latitude": 27.2476,
        "max_longitude": -92.4946,
        "min_longitude": -97.6634,
        "latitude": 29.4719,
        "longitude": -95.0792,
        "radar_frame": [
            {
                "radar_id": "KHGX",
                "radar_type": "reflectivity",
                "frame_time": 1495473662,
                "is_analyzed": 1,
                "average_speed": null,
                "average_direction": null,
                "file_on_server": 1
            },
            {
                "radar_id": "KHGX",
                "radar_type": "reflectivity",
                "frame_time": 1495473305,
                "is_analyzed": 1,
                "average_speed": null,
                "average_direction": null,
                "file_on_server": 1
            },
            {
                "radar_id": "KHGX",
                "radar_type": "reflectivity",
                "frame_time": 1495473002,
                "is_analyzed": 1,
                "average_speed": null,
                "average_direction": null,
                "file_on_server": 1
            }
        ]
    },...

正确的做法是什么?有没有一种简单的方法可以向我的控制器添加别名以区分两个包含查询?我正在使用 CakePHP 3.0。

谢谢!!

由于您的条件是静态的,您可以将它们放入关系中:

雷达站点Table:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('radar_site');

    $this->setDisplayField('radar_id');
    $this->setPrimaryKey('radar_id');

    $this->hasMany('FutureFrame', [
        'foreignKey' => 'radar_id'
    ]);

    $this->hasMany('RadarFrameVelocity', [
       'className' => 'RadarFrame',
       'foreignKey' => 'radar_id',
       'conditions' => [
            'RadarFrame.frame_time >' => time() - 60*60,
            'RadarFrame.file_on_server =' => 1,
            'RadarFrame.radar_type =' => 'velocity'
        ]
    ]);

    $this->hasMany('RadarFrameReflectivity', [
        'className' => 'RadarFrame',
        'foreignKey' => 'radar_id',
        'conditions' => [
            'RadarFrame.frame_time >' => time() - 60*60,
            'RadarFrame.file_on_server =' => 1,
            'RadarFrame.radar_type =' => 'reflectivity'
        ]
    ]);
}

控制器:

public function getRadarData()
{
    $radarInfo = 
    $this->RadarSite->find(
        'all',
        [
            'contain' => [
                'RadarFrameVelocity' =>[
                    'sort' => ['RadarFrameVelocity.frame_time' => 'DESC']
                ],
                'RadarFrameReflectivity' => [
                    'sort' => ['RadarFrameReflectivity.frame_time' => 'DESC']
                ]                   
            ]
        ]

    );    
    $this->set('radarInfo', $radarInfo);
}

另见 Associations - Linking Tables Together