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

Asked

Viewed 320 times

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!

  • The idea is to leave as much explained as possible. Because if someone has the same problem in the future is simple to help!

1 answer

1


So guys I think I was very anxious reading the Cookbook and I didn’t pay attention, the mistake 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 part $tags= $this->request->params['pass'] is asking for a parameter, IE, in the url just put a text of a tags that is already in the database, because 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 number that was already registered in the bank and View appeared beautiful with the data. Ready solved!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.