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

Monday, February 21, 2022

[FIXED] CakePHP 3.0: Get model data from two degree model of separation in pagination

 February 21, 2022     cakephp, cakephp-3.0, model, model-view-controller     No comments   

Issue

Now I'm very tired so I can't think well. Sorry for anticipated if this a duplicate question and if my English is weird.

In CakePHP 3.0 I have the model "Asistencias". Is asosiated to "Ejecutivos". so, in asistencias table, I have de FK ejecutivo_id and works OK when i try to access data from the ejecutivos table because I made the correct declaration:

$this->paginate = [
            //'contain' => ['Sucursales', 'Cedentes', 'Ejecutivos'],
            'contain' => ['Ejecutivos'],
            'limit'=>100,
            'order'=>['Asistencias.entrada'=>'DESC', 'Ejecutivos.nombre'=>'DESC']               
    ];

As you can see, I've commented the line 2. I take out the asosiation of Cedentes and Sucursales from Asistencias.

Now, the FKs sucursal_id and cedente_id belongs to ejecutivos table, instead asistencias table. It's help me to make others tasks in CakePHP that doesn't interest to you.

My question is how I can access to Sucursales and Cedentes in an efficient way? I guess I can't call them from the contain in the paginate properties.

I want use them in my view like this:

//index.ctp
foreach($asistencias as $asistencia){
  echo $asistencia->ejecutivos->name;
  echo $asistencia->ejecutivos->sucursal->name;
  echo $asistencia->ingreso;
  echo $asistencia->salida;
}

I just can't remember the correct way. Can you help me?


Solution

You can nest the contains to get your desired result.

$this->paginate = [
        'contain' => [
              'Ejecutivos' => [
                     'Sucursales', 'Cedentes', 
               ],
        ],
        'limit'=>100,
        'order'=>['Asistencias.entrada'=>'DESC', 'Ejecutivos.nombre'=>'DESC']               
];


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