A couple of issues with the dereuromark cakephp-comments plugin on storing comment data

Hi there. First I should say that I am fairly new to cakephp, so bear with me. I am trying to implement the dereuromark comments plugin in my test environment (cakephp 5.2, cakedc/users plugin and dereuromark cakephp-comments plugin) and I am facing a couple of issues which I need help/advice on to solve.

I have attached the comments plugin to an ‘Articles’ model in the project and the user is able to post a comment to an article item. However, on passing the comment data to the ‘add’ method of the Comments controller, the saving process is terminated at this line of code of the method:

$data[‘content’] = $data[‘comment’] ?? null;

The way I interpret this line of code is that if $data[‘content’] has a value (the comment tekst) it should be replaced by the $data[‘comment’] element in the further processing of the data. However, $data[‘comment’] is always empty in my case and I am not able to see where this element is taken from. My IDE also tells me that this element is undefined. If I modify the code to:

$data[‘content’] = $data[‘content’] ?? null;

the comment tekst is stored as expected. So I’m wandering if this is just a typo in the code, or I am missing something here.

The second issue I have is that the user_id of a commenting logged user is not stored in the table. The user_id should be passed to the $data[‘user_id’] element from the userId() method of the Comments controller. In my case it should be extracted from the user session. However, the userId() method is using ‘Auth.User’ as the session key:

return $this->getRequest()->getSession()->read(‘Auth.User.’ . $userIdField);

but in my case the session key is just ‘Auth’ without the user element. If I modify the code and replace ‘Auth.User.’ with ‘Auth.’ the user_id is passed to the $data[‘user_id’] element as expected and stored in the table. Am I missing something here or do I have to rename the user session key?

I should add that I am not accessing the Comments Component. If I have to do so to solve the issues, I would need some advice on how to use the component.

Any help/advice on this is appreciated.

Does this help? Fix form field handling, session key config, and helper URL bug by dereuromark · Pull Request #4 · dereuromark/cakephp-comments · GitHub
You can try the new release: Release 0.1.4 · dereuromark/cakephp-comments · GitHub

I didnt have any of the issues on prod, because I am manually assigning on add:

  // Handle comment submission                                                                                                                                                                                                                                                                                     
  if ($this->request->getData('Comment')) {                                                                                                                                                                                                                                                                        
      $data = $this->request->getData();                                                                                                                                                                                                                                                                           
      $data['user_id'] = $this->AuthUser->id();  // <-- Explicit user_id assignment                                                                                                                                                                                                                                
      if ($this->Articles->Comments->add($id, 'articles', $data)) {                                                                                                                                                                                                                                                
          ...                                                                                                                                                                                                                                                                                                      
      }                                                                                                                                                                                                                                                                                                            
  }      

Yes, issues solved, except that I had to include the new config key in the plugins config/app.example.php (which I have copied to the app.local.php file) in order for the user_id-fix to work at my end. What about the CommentsTable file, which has an instance of the ‘content field’:

$this->setDisplayField('content');

Is this also affected?

Furthermore, I just realized that the cakedc/users plugin are storing the user_id values as uuid type (char36) and not as integers. So I changed the table in the DB accordingly, after migration of the plugin table, and also changed it in the CommentsTable to be on the safe side:

$this->belongsTo('Users', [
   'type' => 'uuid',
   'className' => 'CakeDC/Users.Users', // Specify the plugin class name
]);

This might perhaps be mentioned in the docs.

Thanks for your help!