【问题标题】:Multiple References to Associated Table in CakephpCakephp 中对关联表的多次引用
【发布时间】:2017-10-22 11:20:11
【问题描述】:

在将两个表(许多属于天气雷达站点的天气雷达帧)与 belongsTo 和 hasMany 相关联后,我希望在同一个表(和不同的条件)上生成带有两个“包含”的 JSON,因此我只能列出具有特定条件的框架。我目前拥有的东西不起作用;它只将第二个包含写入 JSON。

雷达站点表:

...
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'
    ]);
}
...

雷达帧表:

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。

谢谢!!

【问题讨论】:

    标签: json cakephp associations cakephp-3.0 contain


    【解决方案1】:

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

    雷达站点表:

    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

    【讨论】:

    • 感谢您的回复!不幸的是,我收到一条错误消息:“SQLSTATE [42S02]:未找到基表或视图:1146 表 'radarwarn.radar_frame_velocity' 不存在”。它似乎正在寻找一个radar_frame_velocity 表,它(理所当然地)不存在。
    • 我用'className' => 'RadarFrame'更新答案
    • 谢谢谢谢谢谢!!在它对我有用之前我必须进行的一项调整 - 我删除了“雷达框架”。在条件中并将其更改为“RadarFrameVelocity”。或“RadarFrameReflectivity”。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    相关资源
    最近更新 更多