1
I’m playing with cakephp 3 by doing the Cookbook bookmarker tutorial. I am implementing the action in the bookmarks/tags controller, but when trying to access the error of the question title: 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'));
}
}
Follows the path code Routes.php file
Router:: scope(
'/bookmarks',
['controller'=>'Bookmarks'],
function ($routes){
$routes->connect('/tagged/*',['action'=>'tags']);
}
);
Follow the Code of the Bookmarkstable.php model table
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 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 haven’t been able to identify the error yet.
I hope the code, is easy to visualize, anything tells me to adjust!
– jmfrolim
The idea is to leave as much explained as possible. Because if someone has the same problem in the future is simple to help!
– jmfrolim