0

I want to check if the ID passed by the user using a HTTP request exists in another table in cakephp. Right now, I tried using :

public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->existsIn('id_venue', 'venues'));
    return $rules;
}

But It doesn't seems to work. This rule is in a reservation table, in this table I have a column called id_venue, I have another table called Venues which has a primary key called id_venue too.

public function initialize(array $config)
{
    parent::initialize($config);
    $this->table('reservations');
    $this->primaryKey('id_reservation');
    $this->hasOne('Venues');
}

I've associated my Reservations table with my Venues table

Thanks for helping.

Jonathan Bouchard
  • 423
  • 1
  • 4
  • 15

2 Answers2

0

Did't completly understand the type of call you need but if you just have to check if particular row already exist into the DB you can use exists

$table = TableRegistry::get('Venues');
$conditions = ['id' => 15];
$exists = $table->exists($conditions); //returns true or false

This kind of question was already answered: Check if record exists in CakePHP3

Community
  • 1
  • 1
David A.
  • 91
  • 4
0

The first parameter to existsIn is an array, and I believe the second is case sensitive. Try

$rules->add($rules->existsIn(['id_venue'], 'Venues'));
Greg Schmidt
  • 5,010
  • 2
  • 14
  • 35