Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP 2.X: HABTM with Bookmarks / Tags, saving from same form
    primarykey
    data
    text
    <p>I'm trying to build a simple bookmarking site as a project to learn some CakePHP, but am having significant trouble figuring out how to do HABTM (or whatever I might need) for Bookmarks/Tags.</p> <p>I have a simple form which has the fields 'Title', 'Url', 'Tags', which is used to create a new bookmark.</p> <p>My tables are set up as follows:</p> <pre><code>bookmarks: id (primary), uid, title, url, private (boolean), time tags: id (primary), tag bookmarks_tags: id (primary), bookmark_id, tag_id </code></pre> <p>There's also a users table.</p> <p>My bookmark model (Bookmark.php) is pretty simple so far, looks like:</p> <pre><code>class Bookmark extends AppModel { public $name = "Bookmark"; public $displayField = 'name'; public $hasAndBelongsToMany = array( 'Tag' =&gt; array( 'className' =&gt; 'Tag', 'joinTable' =&gt; 'bookmarks_tags', 'foreignKey' =&gt; 'bookmark_id', 'associationForeignKey' =&gt; 'tag_id', 'unique' =&gt; 'keepExisting', ) ); </code></pre> <p>And then some validation.</p> <p>The tag model looks like:</p> <pre><code>class Tag extends AppModel { public $name='Tag'; public $displayField = 'name'; public $hasAndBelongsToMany = array( 'Bookmark' =&gt; array( 'className' =&gt; 'Bookmark', 'joinTable' =&gt; 'bookmarks_tags', 'foreignKey' =&gt; 'tag_id', 'associationForeignKey' =&gt; 'bookmark_id', 'unique' =&gt; 'keepExisting', ) ); </code></pre> <p>The form on the frontend so far looks like this:</p> <pre><code>&lt;div id="addBookmark" class="card"&gt; &lt;div id="addBookmarkTopSpan" class="topSpan"&gt;add new bookmark&lt;/div&gt; &lt;?php echo $this-&gt;Form-&gt;create(null, array('url' =&gt; array('controller' =&gt; 'bookmarks', 'action' =&gt; 'add'))); ?&gt; &lt;fieldset&gt; &lt;?php echo $this-&gt;Form-&gt;input('Bookmark.title'); echo $this-&gt;Form-&gt;input('Bookmark.url'); echo $this-&gt;Form-&gt;input('Bookmark.private', array('type' =&gt; 'checkbox')); echo $this-&gt;Form-&gt;input('Bookmark.uid', array('type' =&gt; 'hidden', 'value' =&gt; $user['User']['id'])); echo $this-&gt;Form-&gt;input('Bookmark.Tag'); ?&gt; &lt;/fieldset&gt; &lt;?php echo $this-&gt;Form-&gt;end(__('Submit')); ?&gt; &lt;/div&gt; </code></pre> <p>As I said, I'm pretty new to Cake so I'm not sure what's going on here..currently if I enter data into that form and hit submit the bookmark gets created just fine but nothing at all happens with the tag. How do I save the data into the associated join table and the Tags table?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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