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

发布于 2024-10-09 16:23:14 字数 1874 浏览 1 评论 0原文

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
        )

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

Will this be the correct format of array that is needed to save this with saveAll? This doesn't save the CategoryPost model. If it isn't what should be the format of the array?

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
        )

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡の花香 2024-10-16 16:23:14

好吧,一切看起来都很好,CategoryPost 数据应该被保存。

也许可以注释掉 CategoryPost 模型中的验证,然后尝试。

Well, everthing seems fine, and the CategoryPost data should be saved.

Maybe comment out the validation from the CategoryPost model and try then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文