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.
I have a simple form which has the fields 'Title', 'Url', 'Tags', which is used to create a new bookmark.
My tables are set up as follows:
bookmarks: id (primary), uid, title, url, private (boolean), time
tags: id (primary), tag
bookmarks_tags: id (primary), bookmark_id, tag_id
There's also a users table.
My bookmark model (Bookmark.php) is pretty simple so far, looks like:
class Bookmark extends AppModel {
public $name = "Bookmark";
public $displayField = 'name';
public $hasAndBelongsToMany = array(
'Tag' => array(
'className' => 'Tag',
'joinTable' => 'bookmarks_tags',
'foreignKey' => 'bookmark_id',
'associationForeignKey' => 'tag_id',
'unique' => 'keepExisting',
)
);
And then some validation.
The tag model looks like:
class Tag extends AppModel {
public $name='Tag';
public $displayField = 'name';
public $hasAndBelongsToMany = array(
'Bookmark' => array(
'className' => 'Bookmark',
'joinTable' => 'bookmarks_tags',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'bookmark_id',
'unique' => 'keepExisting',
)
);
The form on the frontend so far looks like this:
<div id="addBookmark" class="card">
<div id="addBookmarkTopSpan" class="topSpan">add new bookmark</div>
<?php echo $this->Form->create(null, array('url' => array('controller' => 'bookmarks', 'action' => 'add'))); ?>
<fieldset>
<?php echo $this->Form->input('Bookmark.title');
echo $this->Form->input('Bookmark.url');
echo $this->Form->input('Bookmark.private', array('type' => 'checkbox'));
echo $this->Form->input('Bookmark.uid', array('type' => 'hidden', 'value' => $user['User']['id']));
echo $this->Form->input('Bookmark.Tag');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
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?
In your view change
echo $this->Form->input('Bookmark.Tag');
to
echo $this->Form->input('Tag.id');
or
echo $this->Form->input('Tag.0.id');
echo $this->Form->input('Tag.1.id');
echo $this->Form->input('Tag.2.id');
...
Just couple of side notes about your code ...
Since you're following CakePHP conventions, you don't need to specify the parameters for the relationship;
public $hasAndBelongsToMany = array('Tag');
and
public $hasAndBelongsToMany = array('Bookmark');
is enough.
Rather than setting the user ID in the form, which could be modified, set it in your controller:
BookmarksController.php
public function add() {
if ($this->request->is('post')) {
$this->request->data['Bookmark']['uid'] = $this->Auth->user('id');
if ($this->Note->save($this->request->data)) {
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.