2016-08-08 20 views
0

Ich versuche, neue Kommentare zu bestehenden Artikeln hinzuzufügen, ich versuchte fast alle möglichen Möglichkeiten, neue Kommentare hinzuzufügen, fand keine funktionierende, bitte posten, wenn jemand weiß wie um das zu erledigen.Hinzufügen eines Kommentars zu einem Artikel in CakePHP 3

Ich habe auch versucht, alle Beispiele zu nennen auf cakephp 3 docs

Saving Verbände ->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)) { 

     } 

und Ergebnis ist

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 
) 
+0

Warum versuchst du nicht, Kommentar über CommentsController hinzuzufügen und $ post_id zu übergeben, um die Funktion hinzuzufügen? –

Antwort

0

schließlich habe ich es

$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); 
} 
Arbeits