使用这种关系保存正确的数据数组

时间:2010-12-31 06:05:52

标签: cakephp has-and-belongs-to-many

class Post extends AppModel {
    var $name = 'Post';
    var $hasMany = array(
        'CategoryPost' => array(
            'className' => 'CategoryPost'
        )
    );
    var $belongsTo = array(
        'Page' => array(
            'className' => 'Page'
        )
    );

class Category extends AppModel {
    var $name = 'Category';
    var $hasMany = array(
        'CategoryPost' => array(
            'className' => 'CategoryPost'
        )
    );

class CategoryPost extends AppModel {
    var $name = 'CategoryPost';
    var $validate = array(
        'category_id' => array(
            'rule'     => array('multiple', array('in' => array(1, 2, 3, 4))),
            'required' => FALSE,
            'message'  => 'Please select one, two or three options'
        )
    );
    var $belongsTo = array(
        'Post' => array(
            'className' => 'Post'
        ),
        'Category' => array(
            'className' => 'Category'
        )
    );

这是使用saveAll保存此格式所需的正确数组格式吗?这不会保存CategoryPost模型。如果不是数组的格式应该是什么?

Array
(
    [Post] => Array
        (
            [title] => query
            [body] => 

query

            [page_id] => 122
            [modified] => 2010-12-30 23:33:47
            [created] => 2010-12-30 23:33:47
            [uri] => query-9
        )

    [CategoryPost] => Array
        (
            [0] => Array
                (
                    [category_id] => 1
                )

            [1] => Array
                (
                    [category_id] => 2
                )

        )

    [Page] => Array
        (
            [meta_keywords] => query
            [meta_description] => query
            [title] => query
            [layout] => index
        )

)

1 个答案:

答案 0 :(得分:0)

嗯,外翻似乎很好,应该保存CategoryPost数据。

也许会从CategoryPost模型中注释掉验证然后尝试。