verbb / comments

A Craft CMS plugin for managing comments directly within the CMS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Save comment from module controller

Jones-S opened this issue · comments

commented

Question

Hi,
I have a question about how to save a comment from my own controller.

My approach:
I try to use the saveComment() action from the CommentsController.
https://github.com/verbb/comments/blob/craft-3/src/controllers/CommentsController.php#L91

But how would my code exactly look like in php?

My approach:

<?php
namespace modules\mymodule\controllers;

use Craft;
use craft\web\Controller;
use yii\web\Response;
use verbb\comments\controllers\CommentsController;

class AddToQueueController extends Controller
{
    protected array|bool|int $allowAnonymous = ["add-comment"];

    private function actionAddToQueue() {
        $verbbCommentsController = new CommentsController();

        $verbbCommentsController->comment = "My comment";
        $verbbCommentsController->ownerId = 107;
        $verbbCommentsController->parent = null;
        $verbbCommentsController->email = test@mail.com;
        $verbbCommentsController->name = 'myNickname';

        $result = $verbbCommentsController->runAction("saveComment");
        return $this->asJson($result);
    }
}

The first problem is, that I can really set up a new CommentsController(), because the __construct is missing to params.
I think I have to pass $id and $module: like $verbbCommentsController = new CommentsController($id, $module);

The id can kind of be anything, no?
But how do I get the $module?

Also I am absolutely not sure how I can pass my actual values for the comment correctly. The answer above is mix of guessing, the half-truths of Chat GPT and trying to understand the CommentsController.php

I would be very thankful for any hints with this. I am still a bit new to the whole yii framework and writing craft controllers etc.

Cheers and thank you

Additional context

Craft v4.3.8
Verbb comments v2.0.5

I would not recommend using the Controller action to do this. You don't really want to create a new instance of a controller. Instead, create the Comment element object and assign values to it, like you would any other element. The actionSaveComment() is an excellent reference for how to do this.

In your actionAddToQueue() function:

use verbb\comments\elements\Comment;

$comment = new Comment();
$comment->comment = "My comment";
$comment->ownerId = 107;
$comment->parent = null;
$comment->email = test@mail.com;
$comment->name = 'myNickname';

Craft::$app->getElements()->saveElement($comment);
commented

Thanks! Exactly what I needed. Very nice! 🥳

commented

Maybe one last thing, I don't understand. I know that this is all craft related but I would be still really glad if I could get some help.

Within my controller, where I use saveElement(), I previously opened up a database connection to another DB than the crafts db and I mutate an entry.
Now when I do that first and then use saveElement() I get an error saying that fields I want to save are not available in that table.
I think that now my old connection is still open.

I tried to set my PDO connection to null to close it, but that does not help.

I don't know enough about how craft is updating entries within its own database and how that can collide with my own database.
If you have a hint, I would very much appreciate that.

commented

Never mind.
The comments plugin checks whether the key ownerId exists in elements or not.
Maybe this can be helpful for somebody else in the future :).
Cheers and sorry for bothering again.

commented

Ah well one thing:
If you don't set a status when saving the comment, you get an error when clicking on the comment in the cp comments tab:

        $comment = new Comment();
        $comment->comment = $data["comment"];
        $comment->ownerId = $data["owner_id"];
        $comment->parent = null;
        $comment->email = $data["email"];
        $comment->name = $data["nickname"];
        $comment->status = "pending"; // <------------------ add a status

        Craft::$app->getElements()->saveElement($comment);