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

Saturday, January 1, 2022

[FIXED] Join table without id as column name - Cakephp 3

 January 01, 2022     cakephp-3.0     No comments   

Issue

I have a big problem, I don't know how to join a table without passing '_id' as the referenced column name and also including multiple tables who do not have this constraint. For example, I want to join A.code = B.code_defaut

I tried both methods :

       $cycle = $this->Cycles->find('all')
                             ->where(['Cycles.id'=>$id])
                             ->contain(['Quais',
                             'Defauts',
                             'Traces'])
                             ->join([
                                 'ReferentielTraces'=>[
                                     'table'=>'referentiel_traces',
                                     'type'=>'LEFT',
                                     'conditions'=>'ReferentielTraces.code = Defauts.code_defaut'
                                 ]
                             ]);
       $cycle = $this->Cycles->find('all')
                             ->where(['Cycles.id'=>$id])
                             ->contain(['Quais',
                             'Defauts',
                             'Traces',
                             'Defauts.ReferentielTraces']);

But in the first case I have this problem (yes sorry it's french ^^ ):

Error: SQLSTATE[42P01]: Undefined table: 7 ERREUR: entrée manquante de la clause FROM pour la table « defauts » LINE 1: ...ces ReferentielTraces ON ReferentielTraces.code = Defauts.co... ^

which means " missing entry of the FROM clause for the table "Defauts".

And in the second case I have this:

Error: SQLSTATE[42883]: Undefined function: 7 ERREUR: l'opérateur n'existe pas : integer = character varying LINE 1: ..._traces ReferentielTraces ON ReferentielTraces.id = (Defauts... ^

which means "we can't compare integer and character varying " as they are trying to compare ReferentielTraces.id and Defauts.code_defaut

I also tried to make a group of join like join([firstTable, secondTable ...]). But I have no datas for Defauts, Traces, Quais and ReferentielTraces.

Here is the relationship between tables:

Cycles:

        //Quais.id = Cycles.quai_id
        $this->belongsTo('Quais', [
            'foreignKey' => 'quai_id'
        ]);

        //Defauts.cycle_id = Cycles.id
        $this->hasMany('Defauts', [
            'foreignKey' => 'cycle_id'
        ]);

        //Traces.cycle_id = Cycles.id
        $this->hasMany('Traces', [
            'propertyName'=>'Traces',
            'foreignKey' => 'cycle_id',
        ]);

Defauts:

//Defauts.code_defaut = ReferentielTraces.code
$this->belongsTo('ReferentielTraces',[
            'className'=>'ReferentielTraces',
            'foreignKey' => 'code_defaut'   
        ]);

Could you help me please?


Solution

You can use bindingKey , which will be something similar to the following :

//Defauts.code_defaut = ReferentielTraces.code
$this->belongsTo('ReferentielTraces',[
            'className'=>'ReferentielTraces',
            'foreignKey' => 'code_defaut',
            'bindingKey' => 'code'   
        ]);


Answered By - Shifat
  • 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