How to fix the error Impossible to generate condition with empty list of values for field (Tags.title)

1

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.

    
asked by anonymous 11.07.2016 / 20:30

1 answer

1

So I guess I was very anxious reading the CookBook and I did not give it a go, the error is very basic. Next in the BookmarksController method

public function tags(){

$tags= $this->request->params['pass'];
$bookmarks= $this->Bookmarks->find('tagged',[
    'tags'=>$tags
]);
$this->set(compact('bookmarks','tags'));
}

In the $tags= $this->request->params['pass'] part you are asking for a parameter, that is, in the url just put a text of a tags already in the database, since the tags' crud was already implemented. As I did not put anything in the url it presented the exception message that could not generate the necessary condition. Now I put a value that was already registered in the bank and ready appeared the beautiful view with the data. Done!

    
12.07.2016 / 14:17