Skip to content

Instantly share code, notes, and snippets.

@markstory
Created March 20, 2015 02:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markstory/eb0c170b7852ee20d770 to your computer and use it in GitHub Desktop.
Save markstory/eb0c170b7852ee20d770 to your computer and use it in GitHub Desktop.
Issue 6100
<?php
namespace App\Controller;
use App\Controller\AppController;
/**
* Bookmarks Controller
*
* @property App\Model\Table\BookmarksTable $Bookmarks
*/
class BookmarksController extends AppController
{
public $components = ['RequestHandler'];
/**
* Index method
*
* @return void
*/
public function index()
{
$this->paginate = [
'fields' => [
'id', 'user_id', 'title', 'created', 'url',
],
'contain' => [
'Tags',
'Users' => ['fields' => ['id', 'email']]
]
];
$this->set('bookmarks', $this->paginate($this->Bookmarks));
$this->loadModel('Categories');
$this->set('categories', $this->Categories->find('threaded'));
}
/**
* Add method
*
* @return void
*/
public function add()
{
$bookmark = $this->Bookmarks->newEntity();
if ($this->request->is('post')) {
$bookmark = $this->Bookmarks->patchEntity($bookmark, $this->request->data, [
'associated' => ['Tags._joinData']
]);
debug($bookmark);
}
$tags = $this->Bookmarks->Tags->find('list');
$this->set(compact('bookmark', 'tags'));
}
/**
* Edit method
*
* @param string $id
* @return void
* @throws \Cake\Network\Exception\NotFoundException
*/
public function edit($id = null)
{
$bookmark = $this->Bookmarks->get($id, [
'contain' => ['Tags']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$bookmark = $this->Bookmarks->patchEntity(
$bookmark,
$this->request->data,
['associated' => ['Tags._joinData']]
);
debug($bookmark);
}
$tags = $this->Bookmarks->Tags->find('list');
$this->set(compact('bookmark', 'tags'));
}
}
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* Bookmark Entity.
*/
class Bookmark extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'user_id' => true,
'title' => true,
'description' => true,
'url' => true,
'user' => true,
'tags' => true,
];
}
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* BookmarksTag Entity.
*/
class BookmarksTag extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'bookmark' => true,
'starred' => true,
'tag' => true,
];
}
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* Tag Entity.
*/
class Tag extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'title' => true,
'bookmarks' => true,
'_joinData' => true,
];
}
<?php
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Bookmarks Model
*/
class BookmarksTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('bookmarks');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
]);
$this->belongsToMany('Tags', [
'foreignKey' => 'bookmark_id',
'targetForeignKey' => 'tag_id',
'joinTable' => 'bookmarks_tags',
'through' => 'BookmarksTags',
]);
}
}
<div class="bookmarks form large-10 medium-9 columns">
<?= $this->Form->create($bookmark, ['novalidate' => true]) ?>
<fieldset>
<legend><?= __('Add Bookmark'); ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Form->input('description');
echo $this->Form->input('url');
echo $this->Form->input('tags.0.id', ['value' => 1]);
echo $this->Form->input('tags.0._joinData.starred', ['value' => '1']);
echo $this->Form->input('tags.1.id', ['value' => 6]);
echo $this->Form->input('tags.1._joinData.starred', ['value' => '1']);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment