首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向CakePHP 3中的文章添加评论

向CakePHP 3中的文章添加评论
EN

Stack Overflow用户
提问于 2016-08-08 23:06:29
回答 1查看 581关注 0票数 0

我正在尝试添加新的评论到现有的文章,我尝试了几乎所有可能的方法来添加新的评论,没有找到任何有效的,请张贴,如果有人知道如何做到这一点。

我还尝试了cakephp 3文档中提到的所有示例。

保存关联-> http://book.cakephp.org/3.0/en/orm/saving-data.html#saving-associations

代码语言:javascript
复制
$newData = [ 'title' => 'test', 'body' => 'test body', 'author_name' => 'test', 'author_url' => 'author_url', 'author_email' => 'author_email' ];
        $newData[ 'Posts' ] = [ '_ids' => [ 1 ] ];

        print_r( $newData );

        $tagEntity = $this->Posts->Comments->newEntity();
        $tag = $this->Posts->Comments->patchEntity( $tagEntity, $newData );

        print_r( $tagEntity );
        print_r( $tag );

        if( $this->Posts->Comments->save( $tag ) ) {

        }

结果是

代码语言:javascript
复制
Array
(
    [title] => sdsds
    [body] => dsfsf
    [author_name] => ss
    [author_url] => author_url
    [author_email] => author_email
    [Posts] => Array
        (
            [_ids] => Array
                (
                    [0] => 1
                )

        )

)
Blog\Model\Entity\Comment Object
(
    [title] => sdsds
    [body] => dsfsf
    [author_name] => ss
    [author_url] => author_url
    [author_email] => author_email
    [Posts] => Array
        (
            [_ids] => Array
                (
                    [0] => 1
                )

        )

    [[new]] => 1
    [[accessible]] => Array
        (
            [*] => 1
        )

    [[dirty]] => Array
        (
            [title] => 1
            [body] => 1
            [author_name] => 1
            [author_url] => 1
            [author_email] => 1
            [Posts] => 1
        )

    [[original]] => Array
        (
        )

    [[virtual]] => Array
        (
        )

    [[errors]] => Array
        (
        )

    [[invalid]] => Array
        (
        )

    [[repository]] => Blog.Comments
)
EN

回答 1

Stack Overflow用户

发布于 2016-08-09 16:12:38

最后我让它工作了

代码语言:javascript
复制
$data = $this->request->data;
$data[ 'posts' ] = ['_ids' => [ $post->id ] ];
$comment = $this->Posts->Comments->patchEntity( $this->Posts->Comments->newEntity(), $data, [ 'Posts' ] );
$this->Posts->Comments->save( $comment );

if( $this->Posts->save( $comment ) ) {
    echo 'done';
} else {
    print_r( $comment );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38832679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档