与CakePHP 3复杂的JSON(Complicated JSON with Cakephp 3)

编程入门 行业动态 更新时间:2024-10-27 06:23:36
与CakePHP 3复杂的JSON(Complicated JSON with Cakephp 3)

我正在研究一个涉及通过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重构为这种结构似乎有意义吗?

提前致谢!

I am working on a project which involves passing data "profiles" via JSON to a web application. I am using Cakephp 3.0 and am very new to it. I store the profiles in a mysql database and can easily query for the data and put it into a basic JSON format with each row being a separate value in the 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); ?>

Which returns something like this:

{ "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 ] }

I am hoping to A) Easily call individual profiles instead of searching for unique profile ids and B) Only have to load one JSON file to get all profile contents. An output of something like this would be more ideal:

{ "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 } ] } ] }

How would I go about querying my database and populating this JSON structure? Are there any Cakephp tools that help do this? Does reframing the JSON into this structure seem to make sense?

Thanks in advance!

最满意答案

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

ndm也提到了

您只需正确设置关联 ,并在查找中包含关联的表格,并且万一关联的属性名称> 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'] ] );

Thanks to user ndm, I realized there were a few problems with my approach. I thought having all data in one table would simplify things, but in reality it would make things more complicated and require redundant data storage (eg, a latitude and longitude value stored for every profile entry, instead of just once in a separate table).

ndm also mentioned

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.

After altering the Table Model file, I had this for a new "ProfileDataTable.php" file:

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

And this for a new "AlertDataTable.php" file:

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

The important lines here being "belongsTo" and "hasMany".

I then was able to alter my query and use "contain" to easily link the two tables together and get the JSON formatted exactly how I wanted:

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

更多推荐

本文发布于:2023-08-06 04:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSON   CakePHP   Cakephp   Complicated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!