How to display the name field referenced by your ID in CakePHP 3.x?

2

I have three tables (Premises, Routes and Schedules):

Local Table:

   id   |   nombreLocal
--------------------------
    1       Local Uno
    2       Local Dos
    3       Local Tres

Routes Table:

   id   |   id_local_origen   |   id_local_destino
--------------------------------------------------
    1             1                    2
    2             2                    3

Table Schedules:

   id   |   id_rutas
---------------------
    1          1
    2          2

I have the following associations:

In the Locals table:

$this->hasMany('Rutas', [
        'foreignKey' => 'id_local_origen'
    ]);

     $this->hasMany('Rutas', [
        'foreignKey' => 'id_local_destino'
    ]);

In the Routes table:

    $this->belongsTo('Origen', [
        'foreignKey' => 'id_local_origen',
        'className' => 'Locales',
        'joinType' => 'INNER'
    ]);
    $this->belongsTo('Destino', [
        'foreignKey' => 'id_local_destino',
        'className' => 'Locales',
        'joinType' => 'INNER'
    ]);

    $this->hasMany('Programaciones', [
        'foreignKey' => 'id_rutas'
    ]);

In the Schedules table:

    $this->belongsTo('Rutas', [
        'foreignKey' => 'id_rutas',
        'joinType' => 'INNER'
    ]);

What I need for now friends please, is to solve using the CakePHP 3.6 ORM next thing in the view.ctp

In the index.ctp of Schedules, something like this must appear:

   id   |   Origen   |   Destino
------------------------------------
    1      Local Uno     Local Dos
    2      Local Dos     Local Tres

============== In the Programming controller, I have this:

public function view($id = null)
{
    $programacione = $this->Programaciones->get($id, [
        'contain' => ['Rutas']
    ]);

    $this->set('programacione', $programacione);
}

**

  

In other words, I have tables A, B, C   A is already related to B and vice versa.   B is already related to C and vice versa.   What I would like to know please is how can I get fields from table A, being in table C?

**

    
asked by CARLOS B. 07.06.2018 в 22:29
source

1 answer

1

CakePHP allows us to do nests in the contain, and since you have already done most of the work declaring the relationships, you can call them this way:

In yourController Schedules

public function index()
{
    $this->paginate = [
        'contain' => [
            'Rutas' => [
                'Origen',
                'Destino'
            ]
        ]
    ];
    $programaciones = $this->paginate($this->Programaciones);

    $this->set(compact('programaciones'));
}

And in the view as you have it, you can use them in this way to get the result you want

In your index.ctp

<table>
    <thead>
        <th>Id</th>
        <th>Origen</th>
        <th>Destino</th>
    </thead>
    <tbody>
        <?php foreach ($programaciones as $programa): ?>
        <tr>
            <td><?= $programa->id ?></td>
            <td><?= $this->Html->link(
                $programa->ruta->origen->nombreLocal,
                [
                    'controller' => 'Locales',
                    'action' => 'view',
                    $programa->ruta->origen->id,
                ]
            ) ?></td>
            <td><?= $this->Html->link(
                $programa->ruta->destino->nombreLocal,
                [
                    'controller' => 'Locales',
                    'action' => 'view',
                    $programa->ruta->destino->id,
                ]
            ) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

What will print something similar to this:

<table>
  <thead>
    <tr>
      <th>Id</th>
      <th>Origen</th>
      <th>Destino</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td><a href="/locales/view/1">Local uno</a></td>
      <td><a href="/locales/view/2">Local dos</a></td>
    </tr>
    <tr>
      <td>2</td>
      <td><a href="/locales/view/2">Local dos</a></td>
      <td><a href="/locales/view/3">Local tres</a></td>
    </tr>
  </tbody>
</table>
    
answered by 10.06.2018 / 03:08
source