我看到不同格式的数据以不同的模式返回,我不知道为什么。
我定义了以下2个模型(我意识到这些关联没有完全合理 - 我改变了它们的上下文 - 但问题存在:):
//
// RecipeItem Model
//
class RecipeItem extends AppModel
public $belongsTo = array(
'Recipe' => array(
'className' => 'Recipe',
'foreignKey' => 'recipe_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Ingredient' => array(
'className' => 'Ingredient',
'foreignKey' => 'ingredient_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
};
//
// Ingredient Model
//
class Ingredient extends AppModel {
public $belongsTo = array(
'IngredientType' => array(
'className' => 'IngredientType',
'foreignKey' => 'ingredient_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
};
//
// IngredientType
//
class IngredientType extends AppModel {
public $hasMany = array(
'Ingredient' => array(
'className' => 'Ingredient',
'foreignKey' => 'ingredient_type_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
};
我以这种格式返回我的数据:
"RecipeItem": [
{
"id": "16181",
"recipe_id": "4150",
"ingredient_id": "6866",
"amount_in_ounces": "16.00",
"created": "2014-08-06 21:34:50",
"modified": "2014-08-06 21:34:50",
"Ingredient": {
"id": "6866",
"ingredient_type_id": "2",
"user_id": "1",
"name": "Cinnamon",
"notes": "Cinnamon spice notes",
"favorite": "0",
"created": "2014-07-20 23:13:08",
"modified": "2014-07-20 23:13:08",
"IngredientType": {
"id": "2",
"name": "Spice"
}
}
},
];
当我在控制器中使用Containable时:
$data = $this->Recipe->find( 'first',
array(
'contain' => array(
'RecipeItem' => array('Ingredient' => array('IngredientType')),
),
'conditions' => array(
'Recipe.id' => $this->request['id'],
'Recipe.user_id' => $this->request['user_id']
)
)
);
但CakePHP有时会返回默认格式:
{
"Ingredient": {
"id": "6784",
"ingredient_type_id": "5",
"user_id": "1",
"name": "Cinnamon",
"notes": "Some notes...",
"favorite": "0",
"created": "2014-07-20 23:13:08",
"modified": "2014-07-20 23:13:08"
},
"IngredientType": {
"id": "5",
"name": "Allspice"
},
"User": {
"id": "1",
"username": "ccampise",
"password": "3eccc6ad7b84c40434740c782266ec3cced19133",
"notes": null,
"created": "2014-05-16 18:27:56",
"modified": "0000-00-00 00:00:00"
}
},
....
在其他时候我使用相同的可包含符号:
$data = $this->Ingredient->find( 'first',
array(
'contain' => array('IngredientType'),
)
);
我不确定我有偏好,但我更喜欢使用 one 或其他,而不是两者都是为了我的模型的一致性。
有什么想法?谢谢!
答案 0 :(得分:1)
我遇到了同样的问题。此函数将始终嵌套由cake的不一致包含结果返回的数据。
// model
public function formatData(&$data)
{
if (isset($data[0])) {
foreach ($data as $key => $item) {
$this->formatData($data[$key]);
}
} else {
$firstKey = key($data);
$mainData = reset($data);
unset($data[key($data)]);
$data = array($firstKey => array_merge($mainData, $data));
}
}
// controller
$this->User->formatData($data);
作为旁注,CakePHP 3似乎没有遭受这种不足。
答案 1 :(得分:0)
这是CakePHP的默认行为,确定如何根据您查询的模型在结果数组中放置1-n关系。
如果你真的想改变它,你可以随时加入afterFind()
,但这会在将来造成更多混乱。
我担心你不得不习惯这个。