cakephp数据格式不一致


cakephp data format inconsitency

我看到不同模型以不同格式返回的数据,我不知道为什么。

我定义了以下两个模型(我意识到关联没有完全意义——我改变了它们的上下文——但问题存在:):

//
// 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"
    }
},
....

在其他时候,当我使用相同的Containable表示法时:

$data = $this->Ingredient->find( 'first',
    array(
        'contain'   => array('IngredientType'),
    )
);

我不确定我是否有偏好,但我确实更喜欢使用一个或另一个,而不是两者都使用,以确保我的模型的一致性。

有什么想法吗?谢谢

我也遇到了同样的问题。此函数将一致地嵌套蛋糕的不一致包含结果返回的数据。

// 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);

顺便说一句,CakePHP3似乎并没有受到这种不足的影响。

这是CakePHP的默认行为,决定如何根据查询的模型在结果数组中放置1-n关系。

如果你真的想改变这一点,你可以一直挂入afterFind(),但这会在未来引起更多的混乱。

恐怕你得习惯了。