Domanda

Faccio la prossima query:

        $this->find('all',array(
            'contain' => array(
                'User' => array('id','facebook_id','name','username','Author' => array('first_name','last_name','code','photo')),
                'Tab.code'
            ),
            'fields' => array('Comment.date','Comment.time','Comment.content'),
            'conditions' => array(
                'Tab.code' => $code
            ),
            'limit' => $limit,
            'offset' => $offset
        ));
.

Commento ...

    public $belongsTo = array(
        'User' => array('foreignKey' => 'user_id'),
        'Tab' => array('foreignKey' => 'tab_id')
    );
.

Utente ...

    public $hasOne = array('Author' => array('foreignKey' => 'id'));
.

e autore ...

    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'id'
        )
    );
.

Questo restituisce qualcosa del genere (in JSON) ...

[
    {
        "Comment": {
            "date": "2001-12-15",
            "time": "17:32:12",
            "content": "..."
        },
        "User": {
            "id": "29",
            "facebook_id": "1234",
            "name": "User 29",
            "username": "user29"
        },
        "Tab": {
            "code": "llibre-de-prova-29",
            "id": "229"
        }
    }
    ...
]
.

... Mentre mi aspetto che collegato all'utente appaia un autore con i campi che ho specificato.Sai perché l'autore non appare?Ho fatto altro contiene con due livelli di ricorsività sul codice e tutti i lavori come previsto.

Grazie per il tuo aiuto e attenzione!

È stato utile?

Soluzione

Assicurati che il tuo modello abbia il comportamento contenibile allegato.Dovrebbe includere la seguente riga:

$actsAs = array('Containable');
.

Presta attenzione per assicurarti di non aver omesso un "S" in "Atti" che è un errore comune che è difficile da individuare.Puoi anche allegare il comportamento contenibile al volo come questo:

$this->YourModel->Behaviors->attach('Containable');
$this->YourModel->find('all', array('contain' => array(...)));
.

Assicurati anche di digitare "Contenisci" come tasto Array, non "contiene".

Altri suggerimenti

È necessario specificare i campi nel tasto 'Fields'.Trattalo come una chiamata find() aggiuntiva.

$this->find('all', array(
            'contain' => array(
                'User' => array(
                    'fields' => array('id','facebook_id','name','username'),
                    'Author' => array(
                        'fields' => array('first_name','last_name','code','photo')
                    ),
                    'Tab.code'
                ),
            ),
            'fields' => array('Comment.date','Comment.time','Comment.content'),
            'conditions' => array(
                'Tab.code' => $code
            ),
            'limit' => $limit,
            'offset' => $offset
        ));
.

 $this->find('all',array(
        'contain' => array(
            'User' => array('id','facebook_id','name','username','Author' =>array( 'AuthorModelName'=>array('first_name','last_name','code','photo'))),
            'Tab.code'
        ),
.

Tutte le chiavi straniere devono essere nei campi Array

'fields' => array('Comment.date','Comment.time','Comment.content', 'Comment.user_id', ....)
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top