Jump to content

Getting total count for $item->comments( ... )


TSP

Recommended Posts

Hello, 

You have a method on the Item-class named comments that returns the comments: 

<?php
public function comments( $limit=NULL, $offset=NULL, $order='date', $orderDirection='asc', $member=NULL, $includeHiddenComments=NULL, $cutoff=NULL, $extraWhereClause=NULL, $bypassCache=FALSE, $includeDeleted=FALSE )

You give us the option here to provide various params that can affect how many results you'll get from it: $member, $includeHiddenComments, $cutoff, $extraWhereClause and  $includeDeleted. 

However, if this method is to be useful to me, then I need to know how many results there are in total. 

But I can't find any commentCount-method on the Item-class that lets you do a count of the total amount of rows that is available to be returned by sending different parameters of the comments-method.

How am I supposed to do this? 

I thought using $item->comments( ... ) would provide me with the flexibility I needed, but when I'm not able to get a total count, then it seems I'll be better of writing my own queries.

Link to comment
Share on other sites

11 hours ago, TSP said:

I thought using $item->comments( ... ) would provide me with the flexibility I needed, but when I'm not able to get a total count, then it seems I'll be better of writing my own queries.

An alternative:

$totalComments = count( $item->comments() );

Or:

$totalComments = $item->mapped( 'num_comments' );

 

Link to comment
Share on other sites

50 minutes ago, Adriano Faria said:

An alternative:


$totalComments = count( $item->comments() );

Or:


$totalComments = $item->mapped( 'num_comments' );

 

Isn't $item->comments() returning an array of comment items? Wouldn't that simply count the number of rows returned in the array you get back from comments() (And that would be limited also by the offset and limit param)

Link to comment
Share on other sites

4 minutes ago, TSP said:

Isn't $item->comments() returning an array of comment items?

No, it returns... the comments itself. Example:

$firstComment = \IPS\forums\Topic::load( $this->topicid )->comments( 1, 0, 'date', 'asc' );

First post of a topic.

$topic->comments(): Put it a foreach and you will have each one of them. COUNT will count, of course or use mapped( 'num_comments' ).

Link to comment
Share on other sites

 

25 minutes ago, TSP said:

Isn't $item->comments() returning an array of comment items?

Yes

 * @return	array|NULL|\IPS\Content\Comment	If $limit is 1, will return \IPS\Content\Comment or NULL for no results. For any other number, will return an array.
	

You could use $item->mapped('num_comments') but personally I would suggest to use the stats method, because it also takes care of includeFirstCommentIntoCount setting

	public function stats( $includeFirstCommentInCommentCount=TRUE )
	{
		$return = array();

		if ( static::$commentClass )
		{
			$return['comments'] = (int) $this->mapped('num_comments');
			if ( !$includeFirstCommentInCommentCount )
			{
				$return['comments']--;
			}

			if ( $return['comments'] < 0 )
			{
				$return['comments'] = 0;
			}
		}
		
		if ( $this instanceof \IPS\Content\Views )
		{
			$return['num_views'] = (int) $this->mapped('views');
		}
		
		return $return;
	}

 

Link to comment
Share on other sites

@Daniel F, @Marcher Technologies: you are both misunderstanding me.

<?php
require_once( __DIR__ . '/../init.php');
\IPS\Dispatcher\External::i();
$topic = \IPS\forums\Topic::load( 1777157 );
// This topic has 8 posts, 5 of them are visible (including the start post)
// * 2 posts are hidden
// * 1 post is deleted
// * 7 of the posts are written by author_id 58981 (of which 2 are hidden and 1 deleted)

$comments = $topic->comments( 1, 0, 'date', 'asc' );
var_dump(count( $comments )); // Returns 1
var_dump($topic->stats()['comments']); // Returns 5 (Correct in this case)

$comments = $topic->comments( 2, 0, 'date', 'asc' );
var_dump(count( $comments )); // Returns 2 (because thats the amount of comments in the array, limited by limit and offset)
var_dump($topic->stats()['comments']); // Returns 5 (Correct in this case)

$p = ['member' => NULL, 'includeHidden' => NULL, 'cutoff' => NULL, 'where' => NULL, 'bypassCache' => FALSE, 'includeDeleted' => FALSE];
$p['includeHidden'] = TRUE;
$comments = $topic->comments( 2, 0, 'date', 'asc', $p['member'], $p['includeHidden'], $p['cutoff'], $p['where'], $p['bypassCache'], $p['includeDeleted'] );
var_dump(count( $comments )); // Returns 2
var_dump($topic->stats()['comments']); // Returns 5 (NOT what I want. I want 7 in this case, since I now set includeHidden to true and there is 2 hidden posts);

$p['where'] = ['author_id = ?', 58981];
$comments = $topic->comments( 2, 0, 'date', 'asc', $p['member'], $p['includeHidden'], $p['cutoff'], $p['where'], $p['bypassCache'], $p['includeDeleted'] );
var_dump(count( $comments )); // Returns 2 (the number of comments returned, but not the number of comments )
var_dump($topic->stats()['comments']); // Returns 5 (NOT what I want. I want 6, since I now set author_id to match my own ID (1 of the posts are not written by me);
exit;

I hope this will bring some clarity. 

  • I don't want to know how many comments are returned by the method, since that is also limited by limit and offset, and not just the criterias you may set as parameters.
     
  • I don't want to know the num_comments-score as that never changes based upon the value of the parameters you send to the comments-method.
    It's only a cached value of the number of total visible posts in the topic.

 

And also to clarify: I'm not asking for the behaviour of the method stats() or what happens when doing count() on what is returned from the comments-method to change. I'm asking how I'm supposed to retrieve the amount I'm interested in. 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...