Empfehlungen: Erweiterte Suche

Beziehungen noch weiter auflösen

Datenbankobjekte strukturieren und validieren

Beziehungen noch weiter auflösen

Beitragvon justjesus » Fr 23. Jan 2009, 01:35

Hallo,

ich habe folgendes Problem.

Ich backe mit CakePHP gerade eine Bilder-Gallery. In dieser Gallery können Bilder auch von User kommentiert werden.
Anbei meine Models:

Pic Model
Code: Alles auswählen

class Pic extends AppModel {

    var $name = 'Pic';
    var $validate = array(
        'id' => array('numeric'),
        'image_name' => array('notempty'),
        'image_path' => array('notempty'),
        'thumb_path' => array('notempty'),
        'width' => array('numeric'),
        'height' => array('numeric'),
        'size' => array('numeric'),
        'keywords' => array('notempty'),
        'created' => array('date'),
        'modified' => array('date'),
        'user_id' => array('numeric'),
        'active' => array('numeric'),
        'image_downloads' => array('numeric'),
        'image_views' => array('numeric')
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
            'User' => array('className' => 'User',
                                'foreignKey' => 'user_id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => ''
            )
    );

    var $hasMany = array(
            'Comment' => array('className' => 'Comment',
                                'foreignKey' => 'pic_id',
                                'dependent' => false,
                                'conditions' => '',
                                'fields' => '',
                                'order' => '',
                                'limit' => '',
                                'offset' => '',
                                'exclusive' => '',
                                'finderQuery' => '',
                                'counterQuery' => ''
            )
    );

    var $hasAndBelongsToMany = array(
            'Category' => array('className' => 'Category',
                        'joinTable' => 'pics_categories',
                        'foreignKey' => 'pic_id',
                        'associationForeignKey' => 'category_id',
                        'unique' => true,
                        'conditions' => '',
                        'fields' => '',
                        'order' => '',
                        'limit' => '',
                        'offset' => '',
                        'finderQuery' => '',
                        'deleteQuery' => '',
                        'insertQuery' => ''
            )
    );

}
 


User Model
Code: Alles auswählen

<?php
class User extends AppModel {

    var $name = 'User';
    var $validate = array(
        'user_name' => array('rule' => array( array('custom', '/^[a-z0-9]{5,20}$/i'), 'message' => 'Der Benutzername muss zwischen 5 und 20 Zeichen lang sein und darf nur aus Buchstaben und Zahlen bestehen')),  
        'suname' => array('notempty'),
        'name' => array('notempty'),
        'password' => array('rule' => array('isPassword'), 'message' => 'Passwörter nicht identisch oder zu kurz'),
        'e_mail' => array('rule' =>  'email', 'message' => 'Bitte geben sie eine gültige E-Mailadresse an'),
        'created' => array('date'),
        'modified' => array('date')
    );

    function isPassword($data,$field){

        $valid = false;
        if($data['password'] == $this->data['User']['password2']){
            $valid=true;
        }
        if(strlen($data['password']) < 5){
            $valid = false;
        }

        return ($valid);
    }

     function uniqueUser($data,$field){

        return ($valid);
    }


}
?>
 


Comment Model
Code: Alles auswählen

<?php
class Comment extends AppModel {

    var $name = 'Comment';
    var $validate = array(
        'id' => array('numeric'),
        'user_id' => array('numeric'),
        'pic_id' => array('numeric'),
        'comment' => array('notempty')
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
            'User' => array('className' => 'User',
                                'foreignKey' => 'user_id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => ''
            ),
            'Pic' => array('className' => 'Pic',
                                'foreignKey' => 'pic_id',
                                'conditions' => '',
                                'fields' => '',
                                'order' => ''
            )
    );

}
?>


Mein Problem ist jetzt das ich bei der Abfrage eines Pics nicht den Usernamen eines Kommentarschreibers zurückbekomme.
Das müsste doch auch noch irgendwie aufgelößt werden. Es ist ja definiert.

Momentan bekomme ich aber nur die folgende Ausgabe --> Da fehlen die Userdaten des Komentarschreibers
Code: Alles auswählen

Array
(
    [Pic] => Array
        (
            [id] => 41
            [image_name] => rusty.jpg
            [image_path] => img\uploads/rusty0.jpg
            [thumb_path] => img\uploads/thumb_rusty0.jpg
            [width] => 512
            [height] => 512
            [colors] => 0
            [size] => 77220
            [image_type] => jpg
            [description] => Beschreibung
            [keywords] =>  Metall
            [created] => 2009-01-11 01:21:33
            [modified] => 2009-01-11 01:21:33
            [user_id] => 22
            [active] =>
            [image_downloads] => 2
            [image_views] => 9
        )

    [User] => Array
        (
            [id] => 22
            [gender] =>
            [user_name] => TestUser
            [suname] => Test
            [name] => User
            [password] => 123456
            [e_mail] => test@test.de
            [created] => 2009-01-06 20:07:49
            [modified] => 2009-01-06 20:07:49
            [activation_key] => PEMCM-BUNXJ-NSDPW-VCRJQ-ACCGW-1231268869
            [active] => 0
        )

    [Comment] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [user_id] => 1
                    [pic_id] => 41
                    [comment] => Alles cool man !!!!! Das ist doch mal ein Kommentar !!!
                    [modified] => 2009-01-06 20:09:54
                    [created] =>  2009-01-06 20:09:54
                )

        )
 



Was mache ich falsch???????????


Danke im voraus.......
justjesus
 
Beiträge: 2
Registriert: Di 30. Dez 2008, 01:02

Re: Beziehungen noch weiter auflösen

Beitragvon Tobitobe » Fr 23. Jan 2009, 07:24

Hi,

probier es doch bei Deinem find() mal mit der $option
Code: Alles auswählen

'recursive' => 2
 


'recursive' beeinflusst die Verschachtelungstiefe von SQL-Result-Arrays.

Gruß
Tobi
Tobitobe
 
Beiträge: 45
Registriert: Mi 11. Jun 2008, 08:41
Wohnort: Neuss
CakePHP-Version: 1.2.0.7962 Final
OS: Mac OS X

Re: Beziehungen noch weiter auflösen

Beitragvon justjesus » Fr 23. Jan 2009, 18:12

Danke,
das war's!

Ich hab es im Controller gesetzt.

Code: Alles auswählen
    $this->Pic->recursive = 2;
justjesus
 
Beiträge: 2
Registriert: Di 30. Dez 2008, 01:02


Zurück zu Models

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast

cron