php - Cakephp 3 的复杂 JSON

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

获取个人资料数据.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 重新构建为这种结构似乎有意义吗?

提前致谢!

最佳答案

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

ndm也提到了

You'd just have to set up the associations properly, and contain the associated >table in your find, and in case the property name for the association would be >profile_data, you wouldn't even have to modify the results at all.

更改表模型文件后,我将其用于新的“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”。

然后,我能够更改查询并使用“contain”轻松地将两个表链接在一起,并按照我想要的方式获取 JSON 格式:

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

关于php - Cakephp 3 的复杂 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43144527/

相关文章:

php - 在 <img> 标签中显示以 # 开头的图像

php - 使用 symfony 和 FOSUserBundle 在登录失败时覆盖 BadCredentialsException

php - PHP中随机长十六进制字符串的生成器

c# - PHP 相当于 C# string.IsNullOrEmpty 方法?

php - 在我的例子中,在 php 中插入的准备语句失败

mysql - 选择所有多对多关系

php - Opencart:修改电子邮件布局

c# - 在 json.net 中转换为 DateTime

ios - 解析 View Controller 之间的 JSON 数据

javascript - 使用json数据在chart.js中绘制折线图