i'm trying add new comment existing articles, tried possible ways add new comment, didn't found working one, please post, if knows how done.
i tried examples mention on cakephp 3 docs
saving associations -> http://book.cakephp.org/3.0/en/orm/saving-data.html#saving-associations
$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 ) ) { }
and result is
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 )
finally got working
$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 ); }
Comments
Post a Comment