How to kick one or more Comments from CommentModel::Get
I have $Comments and want to delete some $Comment from them. The code by now is that:
$CommentID = 100; // <- just some arbitrary number $CommentModel = new CommentModel(); $Comments = $CommentModel->Get($DiscussionID); foreach ($Comments as $Comment) { if ($Comment->CommentID <= $CommentID) { $unset($Comment); // not working, because it does not change $Comments $Comment = NULL; // not working for the same reason } }
The code in the loop doesn't work. It's obvious because they do not refer to the $Comments scope but only to the $Comment.
But there seems to be also another problem. Each $Comment is stored in an array [_Result]
which is protected.
So I assumed I would have to "clone" the $Comments object, but a $MyComments = clone $Comments
, also cloned the protected array.
This problem is far beyond my PHP knowledge. Can anyone guide me on how to achieve something like that?
Yes, I know I could try to achieve something like that with an $Offset in the Get-function, but I'd prefer getting all comments and sorting them out afterwards...
Best Answer
-
R_J Admin
YEAH! Garden is just too awesome:
$Comments = $CommentModel->Get($DiscussionID); $CommentsFiltered = array(); foreach ($Comments as $Comment) { if ($Comment->CommentID > $CommentID) { $CommentsFiltered[] = $Comment; } } $Comments->ImportDataset($CommentsFiltered);
And that's it!
2
Answers
You want to remove the comment permanently from the db or just from your $Comments object?
If it is the latter, something like this should work:
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Yes, I just want to remove one or more comments from the $Comments object.
Your code gives that error message
Fatal error: Cannot use object of type Gdn_DataSet as array
I just tested this code against 2.0.18.10:
Output from my install is:
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Get()->Result() does the job but I can't use it with my plugin by now
/plugins/DiscussionRefresh/class.discussionrefresh.php
:I need an object, as far as I can see, if I want to reuse the standard comment views
Ahh... now there is the rub. The default view assumes a gdn_dataset object is being passed. It calls Result on the passed object. You could create a wrapper object that contains a Result method on it that just returns the array. Something like this:
The bigger question I am asking, is why not retrieve the entire discussion view (which will handle the entire rendering of the view) and then slice it in JS?
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
I just found that function in class.dataset.php and now I'm trying to go that way:
I took your advice to fetch all comments for the caching benefit. But returning all comments and throwing most of them away after transfering them to the client would be a waste of bandwidth. It also takes longer to render each comment instead of just rendering what is needed.
Good point on the rendering aspect.
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
YEAH! Garden is just too awesome:
And that's it!
@hgtonight: thanks for your help, again!
And I'm happy Garden has a simple way to solve a problem that I still could not solve with simple php...