PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 30, 2021

[FIXED] Cakephp 3 How to access deep contain associations in where clause?

 December 30, 2021     cakephp, cakephp-3.0, php     No comments   

Issue

I have an issue with Cakephp 3.3 in which I do not know how to access linked data by using contain in a where clause.

Here is the request :

$cables = TableRegistry::get('cable_schedule');

    $cablemark = 'test1';
    $equipement = 'test2';
    $compartment = 'test3';
    $system = 'test4';

    $query = $cables->find('all')->contain(['CableType', 'Contract', 'EquipementSource' => ['Compartment', 'System'], 'EquipementDest' => ['Compartment', 'System']])
        ->where(['EquipementSource.Description like' => '%'.$equipement.'%'])
        ->orWhere(['EquipementDest.Description like' => '%'.$equipement.'%'])
        ->andWhere(['Cable_Mark like' => '%'.$cablemark.'%'])
        ->andWhere(['EquipementSource.Compartment.Description like' => '%'.$compartment.'%'])
        ->orWhere(['EquipementDest.Compartment.Description like' => '%'.$compartment.'%'])
        ->andWhere(['EquipementSource.System.Description like' => '%'.$system.'%'])
        ->orWhere(['EquipementDest.System.Description like' => '%'.$system.'%']);

    $this->set('cables', $query);

The error is :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'EquipementDest.System.Description' in 'where clause'


Solution

You need to use the matching() method (see the CakePHP manual).

For example, if you have table articles and it has many tags, you could filter results like this:

$query = $articles->find();
$query->matching('Tags', function ($q) {
    return $q->where(['Tags.name' => 'CakePHP']);
});


Answered By - makallio85
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing