Relationship belongsTo

Asked

Viewed 67 times

0

I wonder if it’s possible to do this kind of relationship in the cakephp model, if it’s possible, show me how, because I’m not getting it.

Way I need to:

    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 better what I need:

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 me 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 her 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
  • The two posted ways are identical, what’s the problem? (has an error too)

  • Opa, sorry @Virgilionovic, my fault, I ran out here and ended up putting twice the same code, is that in the way I need I put two references in Foreignkey and in the normal way, one should put only one, could help me in this question?

  • (I’ve already edited the question)

  • Welcome to Stackoverflow in English. I edited your question to remove the greetings, as we usually keep them as clean as possible to focus on your scheduling question. If you are interested in visiting a part of the site that is not aimed to ask questions can know the [chat]. If you have questions about the operation, rules and procedures of the site visit the [meta] :)

  • Ok @diegofm, thank you so much! xD

1 answer

1


Resolved as follows:

Really remained 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 me 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 me 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.