belongsTo's relationship

0

I would like to know if it is possible to do this kind of relationship in Cakephp's Model, if it is possible, show me how, because I can not.

Way I need it:

    public $belongsTo = array(
         Status => array(
            'className' => 'Status',
            'foreignKey' => 'status_id_origem', 'status_id_destino',
            'dependent' => false,
         ),
    );

Normal way:

    public $belongsTo = array(
         Status => array(
            'className' => 'Status',
            'foreignKey' => 'status_id_origem',
            'dependent' => false,
         ),
    );

Specifying what I need best:

I run the following query:

    SELECT Historico.*, Status.descricao
       FROM historicos as Historico
             LEFT JOIN statuses as Status
                     ON (Status.id = Historico.status_id_origem)

Soon this query returns the following:

     id integer|status_id_origem integer|status_id_destino integer|descricao
     ----------|------------------------|-------------------------|---------
          44   |        1               | 2                       | Aguardando embarque
          43   |        1               | 2                       | Aguardando embarque

But what I really need it to get back to me is this:

     id integer|status_id_origem integer|status_id_destino integer|descricaoIdOrigem varchar|descricaoIdDestino varchar
     ----------|------------------------|-------------------------|-------------------------|------------------------
          44   |        1               | 2                       | Aguardando embarque     |    Embarcado
          43   |        1               | 2                       | Aguardando embarque     |    Embarcado
    
asked by anonymous 15.12.2016 / 20:30

1 answer

1

Resolved as follows:

Really stayed the same way:

    public $belongsTo = array(
         Status => array(
            'className' => 'Status',
            'foreignKey' => 'status_id_origem',
            'dependent' => false,
         ),
    );

Way I was doing:

    SELECT Historico.*, Status.descricao
       FROM historicos as Historico
             LEFT JOIN statuses as Status
                     ON (Status.id = Historico.status_id_origem)

Soon this query returns the following:

     id integer|status_id_origem integer|status_id_destino integer|descricao
     ----------|------------------------|-------------------------|---------
          44   |        1               | 2                       | Aguardando embarque
          43   |        1               | 2                       | Aguardando embarque

Way I found what I needed:

    SELECT Historico.*, Status.descricao, Statuse.descricao
       FROM historicos as Historico
             LEFT JOIN statuses as Status
                     ON (Status.id = Historico.status_id_origem)
             LEFT JOIN statuses as Statuse
                     ON (Statuse.id = Historico.status_id_destino):

Soon this query returns the following:

     id integer|status_id_origem integer|status_id_destino integer|  descricao  varchar  |  descricao  varchar
     ----------|------------------------|-------------------------|----------------------|---------------------
          44   |        1               |          2              | Aguardando embarque  |    Embarcado
          43   |        1               |          2              | Aguardando embarque  |    Embarcado
    
16.12.2016 / 12:03