Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not a CakePHP expert, but I still think it would make sense to have your own controller. From what I remember from doing one of those CakePHP blog tutorials is, that you need to link the comments and the post in the comment model. This is some of the code I have from it:</p> <pre><code>class Comment extends AppModel { var $name = ‘Comment’; var $belongsTo = array(‘Person’); } </code></pre> <p>And then you need a controller (comments_controller.php):</p> <pre><code>class CommentsController extends AppController { var $name = ‘Comments’; var $scaffold; } </code></pre> <p>Some SQL:</p> <pre><code>CREATE TABLE comments ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, author VARCHAR(50), comment TEXT, person_id INT, created DATETIME DEFAULT NULL, modified DATETIME DEFAULT NULL ); </code></pre> <p>The <code>$scaffold</code> creates a CRUD application for you, so when you go to /comments in your browser you can <strong>c</strong>reate, <strong>r</strong>ead, <strong>u</strong>pdate and <strong>d</strong>elete comments. So, as you see, there is not much involved here. All you need is your database tables and a little logic to provide person_id.</p> <p>To save a comment (in your Person/view):</p> <pre><code>&lt;h2&gt;Add comment&lt;/h2&gt; &lt;?php echo $form-&gt;create(‘Comment’, array(‘action’=&gt;‘add/’.$person[‘Person’][‘id’]); echo $form-&gt;input(‘author’); echo $form-&gt;input(‘content’); echo $form-&gt;submit(‘Add comment’); echo $form-&gt;end(); ?&gt; </code></pre> <p>And in your CommentsController:</p> <pre><code>function add($id = NULL) { if (!empty($this-&gt;data)) { $this-&gt;data['Comment']['person_id'] = $id; $this-&gt;data['Comment']['id'] = ''; if ($this-&gt;Comment-&gt;save($this-&gt;data)) { $this-&gt;Session-&gt;setFlash('Commented added'); $this-&gt;redirect($this-&gt;referer()); } } } </code></pre> <p>So you basically overwrite the standard add action, which Cake adds by itself. Hope that makes sense now. Also, you might need a route so it picks up /comments/add/ID. I don't know about this part. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload