I'm playing cakephp 3 by doing the cookbook bookmarker tutorial. I'm implementing the action in controller bookmarks / tags, but when trying to access the error appear the title of the question: Impossible to generate condition with empty list of values for field (Tags.title)
Follow the controller code
<?php namespace App\Controller;
use App\Controller\AppController;
class BookmarksController extends AppController
{
public function index()
{
$this->paginate = [
'contain' => ['Users']
];
$bookmarks = $this->paginate($this->Bookmarks);
$this->set(compact('bookmarks'));
$this->set('_serialize', ['bookmarks']);
}
public function view($id = null)
{
$bookmark = $this->Bookmarks->get($id, [
'contain' => ['Users', 'Tags']
]);
$this->set('bookmark', $bookmark);
$this->set('_serialize', ['bookmark']);
}
public function add()
{
$bookmark = $this->Bookmarks->newEntity();
if ($this->request->is('post')) {
$bookmark = $this->Bookmarks->patchEntity($bookmark, $this->request->data);
if ($this->Bookmarks->save($bookmark)) {
$this->Flash->success(__('The bookmark has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The bookmark could not be saved. Please, try again.'));
}
}
$users = $this->Bookmarks->Users->find('list', ['limit' => 200]);
$tags = $this->Bookmarks->Tags->find('list', ['limit' => 200]);
$this->set(compact('bookmark', 'users', 'tags'));
$this->set('_serialize', ['bookmark']);
}
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);
if ($this->Bookmarks->save($bookmark)) {
$this->Flash->success(__('The bookmark has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The bookmark could not be saved. Please, try again.'));
}
}
$users = $this->Bookmarks->Users->find('list', ['limit' => 200]);
$tags = $this->Bookmarks->Tags->find('list', ['limit' => 200]);
$this->set(compact('bookmark', 'users', 'tags'));
$this->set('_serialize', ['bookmark']);
}
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$bookmark = $this->Bookmarks->get($id);
if ($this->Bookmarks->delete($bookmark)) {
$this->Flash->success(__('The bookmark has been deleted.'));
} else {
$this->Flash->error(__('The bookmark could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
public function tags(){
$tags= $this->request->params['pass'];
$bookmarks= $this->Bookmarks->find('tagged',[
'tags'=>$tags
]);
$this->set(compact('bookmarks','tags'));
}
}
Follow the route code routes.php file
Router:: scope(
'/bookmarks',
['controller'=>'Bookmarks'],
function ($routes){
$routes->connect('/tagged/*',['action'=>'tags']);
}
);
Follow the Code of the model table BookmarksTable.php
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class BookmarksTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('bookmarks');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', ['foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsToMany('Tags', [
'foreignKey' => 'bookmark_id',
'targetForeignKey' => 'tag_id',
'joinTable' => 'bookmarks_tags'
]);
}
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->allowEmpty('title');
$validator
->allowEmpty('description');
$validator
->allowEmpty('url');
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['user_id'], 'Users'));
return $rules;
}
public function findTagged(Query $query ,array $options){
$fields=[
'Bookmarks.id',
'Bookmarks.title',
'Bookmarks.url',
];
return $this->find()
->distinct($fields)
->matching('Tags',function ($q) use ($options){
return $q->where(['Tags.title IN'=> $options['tags']]);
});
}
}
Now the code of the View tags.ctp
<h1> Bookmarks tagged with
<?= $this->Text->toList($tags) ?>
</h1>
<section>
<?php foreach ($bookmarks as $bookmark): ?>
<article>
<h4>
<?= $this->Html->link($bookmark->title, $bookmark->url) ?>
</h4>
<small>
<?= h($bookmark->url) ?>
</small>
<?= $this->Text->autoParagraph($bookmark->description) ?>
</article> <?php endforeach; ?>
</section>
I wanted to know where I'm going wrong, I know it's basic, but I could not identify the error yet.