Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6798769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:46:12+00:00 2026-05-26T18:46:12+00:00

So I’m getting a bit lost with CActiveRecord in Yii. Let’s say I have

  • 0

So I’m getting a bit lost with CActiveRecord in Yii.

Let’s say I have the following simple models:

  • Post
    • id
    • content
  • Comment
    • id
    • content
    • post_id

So Post HAS_MANY Comment, I think the relationship should be fairly obvious from that description.

What if I want to get all comments, and then within that get a specific comment for something? Like I want to display a list of comments for a post, but show the content of just one of them(by PK).

So I have:

$this->post = Post::model()->with('comments')->findByPk( $pid );
 // this next line is an extra DB Query, right?
$this->comment = Comment::model()->findByPk( $cid );

But I want something like:

$this->post = Post::model()->with('comments')->findByPk( $pid );
// this is not valid code because the comments is an array
$this->comment = $this->post->comments->findByPk( $cid ); // <---

Do I just have to wrap that in a loop myself to implement findByPk the second time?

My second question is, can I do something like the following?

$this->post = Post::model()->with('comments')->findByPk( $pid );
$this->comment = Comment::model()->findByPk( $cid );
// Get the PK of the next and previous comments from the $cid comment
// again, not valid code
$this->next = $this->post->NextComment($this->comment);
$this->prev = $this->post->PreviousComment($this->comment);

Or do I again need to wrap my own loop over the comments?

I’m really just trying to take advantage of what Yii has to offer before I write extra code. The documentation always assumes you always want all of the relational data, it doesn’t specify how to pick parts of the relational data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:46:12+00:00Added an answer on May 26, 2026 at 6:46 pm

    Answer for the first question is

    $post = Post::model()->with('comments')->find('t.id = :post_id AND comments.id = :comment_id', array(
        ':post_id' => $somePostId,
        ':comment_id' => $someCommentId,
    ));
    

    or

    $post =  Post::model()->findByPk($somePostId);
    $comments = $post->comments(array(
        'condition' => 'comments.id = :comment_id',
        'params' => array(':comment_id' => $someCommentId),
    ));
    

    The second question: what do you mean by “next” and “previous” comment?
    Next to what? To id or to create_time?

    You can do for it something like this (for previous by create_time):

    $post =  Post::model()->findByPk($somePostId);
    $comment = Comment::model()->findByPk(someCommentId);
    $previousComment = $post->comments(array(
        'condition' => 'comments.create_time < :comment_create_time',
        'params' => array(':comment_create_time' => $comment->create_time),
        'limit' => 1,
    ));
    

    You can get more info about this topic here and also from official class reference for CActiveRecord.find()

    Update

    If you want get all comments from db and than get one by id you can do it this way:

    $post->comments is just an array. So you can loop through it and get yours as you described in the topic.

    But also you can add a new method to Post model class (not comments because it’s an array) like “findCommentById”:

    public function findCommentById($id)
    {
        foreach ($this->comments as $comment)
            if ($comment->id == $id)
                return $comment;
    
        return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.