/** * Comment belongs to a Post * Post & Comment are both new records */ public function testSetWithNew2BelongsTo() { $writer = $this->TWriter->find('first', array('recursive' => -1, 'conditions' => array('id' => 1), 'activeRecord' => true)); $newPost = new ARTPost(array('Writer' => $writer, 'title' => 'TestTitle', 'message' => 'TestMessage')); $newComment = new ARTComment(array('message' => 'TestMessage', 'Post' => $newPost)); $this->assertEquals($newPost->save(), true); $this->assertEquals($newComment->save(), true); $this->assertEquals($newPost->Comments[0]->message, 'TestMessage'); $this->assertEquals($newComment->Post->title, 'TestTitle'); ActiveRecordManager::clearPool(); $comment = $this->TComment->find('first', array('recursive' => -1, 'conditions' => array('message' => 'TestMessage'), 'activeRecord' => true)); $this->assertEquals($comment->Post->title, 'TestTitle'); ActiveRecordManager::clearPool(); $post = $this->TPost->find('first', array('recursive' => -1, 'conditions' => array('title' => 'TestTitle'), 'activeRecord' => true)); $this->assertEquals($post->Comments[0]->message, 'TestMessage'); }
/** * Test save with HABTM association */ public function testHABTM() { $ARTPost = new ARTPost(array('title' => 'lala', 'message' => '', 'writer_id' => 1)); $Comment1 = new ARTComment(array('message' => 'coment1 lala1', 'post_id' => 0)); $Comment2 = new ARTComment(array('message' => 'coment1 lala2', 'post_id' => 0)); $Comment3 = new ARTComment(array('message' => 'coment1 lala3', 'post_id' => 0)); $Comment4 = new ARTComment(array('message' => 'coment1 lala4', 'post_id' => 0)); $Comment1->Post = $ARTPost; $Comment2->Post = $ARTPost; $Comment3->Post = $ARTPost; $Comment4->Post = $ARTPost; $Comment2->Parents[] = $Comment1; $Comment3->Parents[] = $Comment1; $Comment4->Parents[] = $Comment3; $Comment3->Childrens[] = $Comment4; $Comment1->Childrens[] = $Comment3; $Comment1->Childrens[] = $Comment2; $Comment1->save(); $this->_assertHABTM($Comment1); ActiveRecordManager::clearPool(); $this->_assertHABTM($Comment1); $ARTComment = $this->TComment->find('first', array('conditions' => array('TComment.id' => $Comment1->id), 'activeRecord' => true)); $this->_assertHABTM($ARTComment); }