Yii Framework Forum: Latest Comment of a Post - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Latest Comment of a Post Rate Topic: -----

#1 User is offline   paul0001 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 21-July 11

Posted 03 February 2012 - 04:11 AM

Hi all,

Below are the schema for Post and Comment
CREATE TABLE Post (
   postID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
   msg VARCHAR(100),
) ENGINE = InnoDB;

CREATE TABLE Comment (
   commentID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
   postID INT NOT NULL,
   date DATE NOT NULL,
   msg VARCHAR(100),
   CONSTRAINT fk_comment_post 
   FOREIGN KEY (postID) 
   REFERENCES Post (postID)
   ON DELETE NO ACTION
   ON UPDATE NO ACTION
) ENGINE = InnoDB;


and the relation in Post Model is specified as:
public function relations() {
   return array(
      'comments' => array(self::HAS_MANY, 'Comment', 'postID'),
   );
}


How to add a relation 'lastestComment' to the Post model to just get the latest Comment?

Thanks.
0

#2 User is offline   Weavora Team 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 141
  • Joined: 06-December 10

Posted 03 February 2012 - 04:28 AM

I have no solution how to do that with relations but you could do it with regular method:


class Post extends CActiveRecord {
  
  private $_latestComment = null;
  ...
  public function getLatestComment($refresh = false) {
    if (is_null($this->_latestComment) || $refresh) {
      $c = new CDbCriteria();
      $c->order = '`date` desc';
      $c->compare('postID', $this->postID);
      $this->_latestComment = Comment::model()->find($c);
    }
    return $this->_latestComment;
  }

}

// usage

echo $post->latestComment->msg; // sql query executed
echo $post->latestComment->date; // no more query

echo $post->getLatestComment(true)->msg; // refresh latest comment -> sql query executed

1

#3 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 03 February 2012 - 04:28 AM

Based on http://www.yiiframew...elations-detail

public function relations() {
   return array(
      'comments' => array(self::HAS_MANY, 'Comment', 'postID'),
      'lastComment' => array(self::HAS_MANY, 'Comment', 'postID', 'order'=>'lastComment.date DESC','limit'=>1)
   );
}


Totally untested (I normally use DAO for this type of operations)

PS: You can also use scopes

You set your scope on Comments

public function last(){
    $this->getCDbCriteria()->order = 'date DESC';
    $this->getCDbCriteria()->limit = 1;
   return $this;
}


Then you can call it like this, having your comments relation set
  // returns all posts with its last comment
  $posts = Post::model()->with('comments:last')->findAll(); 


Reference: http://www.yiiframew...th-named-scopes
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#4 User is offline   Weavora Team 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 141
  • Joined: 06-December 10

Posted 03 February 2012 - 04:31 AM

View PostAntonio Ramirez, on 03 February 2012 - 04:28 AM, said:

public function relations() {
   return array(
      'comments' => array(self::HAS_MANY, 'Comment', 'postID'),
      'lastComment' => array(self::HAS_MANY, 'Comment', 'postID', 'order'=>'lastComment.date DESC','limit'=>1)
   );
}



In this case you should access lastComment as:
echo $post->lastComment[0]->msg;

0

#5 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 03 February 2012 - 04:57 AM

View PostWeavora Team, on 03 February 2012 - 04:31 AM, said:

In this case you should access lastComment as:
echo $post->lastComment[0]->msg;


I'm not sure this will work using "with":

Post::model()->with('lastComment')->findAll();

Also I wouldn't create a relation just to get a last comment.
0

#6 User is offline   Sankalp Singha 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 13-April 13

Posted 16 June 2013 - 03:11 AM

View PostWeavora Team, on 03 February 2012 - 04:28 AM, said:

I have no solution how to do that with relations but you could do it with regular method:


class Post extends CActiveRecord {
  
  private $_latestComment = null;
  ...
  public function getLatestComment($refresh = false) {
    if (is_null($this->_latestComment) || $refresh) {
      $c = new CDbCriteria();
      $c->order = '`date` desc';
      $c->compare('postID', $this->postID);
      $this->_latestComment = Comment::model()->find($c);
    }
    return $this->_latestComment;
  }

}

// usage

echo $post->latestComment->msg; // sql query executed
echo $post->latestComment->date; // no more query

echo $post->getLatestComment(true)->msg; // refresh latest comment -> sql query executed



Thank you for this solution!! I modified this method to reflect few things that were suiting me. And could manage what I was trying.
0

#7 User is offline   sefburhan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 137
  • Joined: 08-February 12
  • Location:islamabad

Posted 16 June 2013 - 06:34 AM

View Postpaul0001, on 03 February 2012 - 04:11 AM, said:

Hi all,

Below are the schema for Post and Comment
CREATE TABLE Post (
   postID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
   msg VARCHAR(100),
) ENGINE = InnoDB;

CREATE TABLE Comment (
   commentID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
   postID INT NOT NULL,
   date DATE NOT NULL,
   msg VARCHAR(100),
   CONSTRAINT fk_comment_post 
   FOREIGN KEY (postID) 
   REFERENCES Post (postID)
   ON DELETE NO ACTION
   ON UPDATE NO ACTION
) ENGINE = InnoDB;


and the relation in Post Model is specified as:
public function relations() {
   return array(
      'comments' => array(self::HAS_MANY, 'Comment', 'postID'),
   );
}


How to add a relation 'lastestComment' to the Post model to just get the latest Comment?

Thanks.

http://www.yiiframew...l-query-options
1

#8 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 17 June 2013 - 09:00 AM

I think this were a better solution:
public function relations() {
   return array(
      'comments' => array(self::HAS_MANY, 'Comment', 'postID'),
      'lastComment' => array(self::HAS_ONE, 'Comment', 'postID', 'order'=>'commentID DESC')
   );
}

(Not tested, btw)
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users