使用Cakephp 3复杂的JSON

时间:2017-03-31 15:33:23

标签: php mysql json cakephp cakephp-3.0

我正在开发一个项目,该项目涉及通过JSON将数据“配置文件”传递给Web应用程序。我正在使用Cakephp 3.0并且它是新手。我将配置文件存储在mysql数据库中,可以轻松查询数据并将其放入基本的JSON格式,每行都是JSON中的一个单独的值:

Controller.php这样:

....
public function getProfileData()
    {
        $uid = $this->Auth->user('id');
        $this->loadComponent('RequestHandler');
        $this->set('profile', $this->MapDisplay->find(
            'all',
            ['conditions' => 
                ['MapDisplay.user_id =' => $uid]
            ]
        )
        );
        $this->set('_serialize', ['profile']);
    }
....

get_profile_data.ctp:

<?= json_encode($profile); ?>

返回的内容如下:

{
"profile": [
    {
        "alert_id": 1,
        "alert_name": "Test",
        "user_id": 85,
        "initialized_time": "2017-03-24T00:00:00",
        "forecasted_time": "2017-03-24T00:10:00",
        "minimum_dbz_forecast": 0,
        "maximum_dbz_forecast": 10,
        "average_dbz_forecast": 5,
        "confidence_in_forecast": 0.99,
        "alert_lat": 44.3876,
        "alert_lon": -68.2039
    },
    {
        "alert_id": 1,
        "alert_name": "Test",
        "user_id": 85,
        "initialized_time": "2017-03-24T00:00:00",
        "forecasted_time": "2017-03-24T00:20:00",
        "minimum_dbz_forecast": 5,
        "maximum_dbz_forecast": 15,
        "average_dbz_forecast": 10,
        "confidence_in_forecast": 0.99,
        "alert_lat": 44.3876,
        "alert_lon": -68.2039
    },
    {
        "alert_id": 2,
        "alert_name": "Test2",
        "user_id": 85,
        "initialized_time": "2017-03-24T00:00:00",
        "forecasted_time": "2017-03-24T00:10:00",
        "minimum_dbz_forecast": 10,
        "maximum_dbz_forecast": 20,
        "average_dbz_forecast": 15,
        "confidence_in_forecast": 0.99,
        "alert_lat": 44.5876,
        "alert_lon": -68.1039
    },
    {
        "alert_id": 2,
        "alert_name": "Test2",
        "user_id": 85,
        "initialized_time": "2017-03-24T00:00:00",
        "forecasted_time": "2017-03-24T00:20:00",
        "minimum_dbz_forecast": 15,
        "maximum_dbz_forecast": 25,
        "average_dbz_forecast": 35,
        "confidence_in_forecast": 0.99,
        "alert_lat": 44.5876,
        "alert_lon": -68.1039
]
}

我希望A)轻松调用单个配置文件,而不是搜索唯一的配置文件ID和B)只需加载一个JSON文件即可获取所有配置文件内容。这样的输出会更理想:

 {
"profile": [
    {
        "alert_id": 1,
        "alert_name": "Test",
        "initialized_time":"2017-03-24T00:00:00",
        "alert_lat": 44.3876,
        "alert_lon": -68.2039,
        "profile_data": [
            {
                "forecasted_time": "2017-03-24T00:10:00",
                "minimum_dbz_forecast": 0,
                "maximum_dbz_forecast": 10,
                "average_dbz_forecast": 5,
                "confidence_in_forecast": 0.99
            },
            {
                "forecasted_time": "2017-03-24T00:20:00",
                "minimum_dbz_forecast": 5,
                "maximum_dbz_forecast": 15,
                "average_dbz_forecast": 10,
                "confidence_in_forecast": 0.99
            }
        ]
    },
    {
        "alert_id": 2,
        "alert_name": "Test2",
        "initialized_time": "2017-03-24T00:00:00",
        "alert_lat": 44.5876,
        "alert_lon": -68.1039,
        "profile_data": [
            {
                "forecasted_time": "2017-03-24T00:10:00",
                "minimum_dbz_forecast": 10,
                "maximum_dbz_forecast": 20,
                "average_dbz_forecast": 15,
                "confidence_in_forecast": 0.99
            },
            {
                "forecasted_time": "2017-03-24T00:20:00",
                "minimum_dbz_forecast": 15,
                "maximum_dbz_forecast": 25,
                "average_dbz_forecast": 35,
                "confidence_in_forecast": 0.99
            }
        ]
    }
]
}

如何查询我的数据库并填充此JSON结构?是否有任何Cakephp工具可以帮助您做到这一点?将JSON重构为这种结构似乎有意义吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

感谢用户ndm,我意识到我的方法存在一些问题。我认为在一个表中包含所有数据会简化事情,但实际上它会使事情变得更复杂并且需要冗余数据存储(例如,为每个配置文件条目存储的纬度和经度值,而不是仅在一个单独的表中存储一次)。

ndm也提到了

  

您只需要在您的查找中正确设置associationscontain关联的&gt;表,如果关联的属性名称为&gt; profile_data,则您根本不需要修改结果。

更改表模型文件后,我将其用于新的“ProfileDataTable.php”文件:

class ProfileDataTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);        

    $this->setTable('profile_data');

    $this->setDisplayField('title');
    $this->setPrimaryKey('alert_id');
    $this->addBehavior('Timestamp');

    $this->belongsTo('AlertData', [
        'foreignKey' => 'alert_id'
    ]);
}

}

这是一个新的“AlertDataTable.php”文件:

class AlertDataTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('alert_data');

    $this->setDisplayField('title');
    $this->setPrimaryKey('alert_id');
    $this->addBehavior('Timestamp');

    $this->hasMany('ProfileData', [
        'foreignKey' => 'alert_id'
    ]);
}

}

这里的重要部分是“belongsTo”和“hasMany”。

然后,我能够改变我的查询并使用“包含”轻松地将两个表链接在一起,并将JSON格式化为我想要的格式:

$this->AlertData->find(
        'all',
        ['conditions' => 
            ['AlertData.user_id =' => $uid],
        'contain' => 
            ['ProfileData']
        ]
    );