Empfehlungen: Erweiterte Suche

$validate und $belongsto

Datenbankobjekte strukturieren und validieren

$validate und $belongsto

Beitragvon fleix » Mi 6. Aug 2008, 14:25

moin

ich bin absoluter cake anfänger und habe das blog-beispiel von blog.dievolution.net nachgebaut.
jetzt möchte ich auch die datan der comments prüfen bevor ich sie speicher nur leider wird das $validate des comment-models ignoriert.
ich denke mal dass das an der beziehung des models liegt aber ich finde dazu leider nichts in der doku...
vielleicht könnt ihr mir ja die zusammenhänge etwas erleuchten

gruß

das ist mein post model
Code: Alles auswählen
class Post extends AppModel {
       
        var $name = 'Post';
        var $hasMany = array('Comment');
       
        var $validate = array (
            'titel' => array (
                'required'=>VALID_NOT_EMPTY,
                'length'=>array ('rule'=>array('maxLength',100))),
            'inhalt'=> VALID_NOT_EMPTY
            );
    }


und dies das comment model
Code: Alles auswählen
class Comment extends AppModel{
            var $name = 'Comment';
            var $belongsTo = array('Post');
            var $validate = array (
            'autor' => array (
                'required'=>VALID_NOT_EMPTY,
                'length'=>array ('rule'=>array('maxLength',20))),
            'inhalt'=> VALID_NOT_EMPTY
            );
    }
fleix
 
Beiträge: 2
Registriert: Mi 6. Aug 2008, 14:12

Re: $validate und $belongsto

Beitragvon Alex » Do 7. Aug 2008, 13:37

Moin fleix,

ich kann jetzt so auf Anhieb keinen Fehler erkennen. Kannst du evtl noch deine Views posten?

Greetz,
Alex
Alex
 
Beiträge: 120
Registriert: Di 13. Mai 2008, 13:39
Wohnort: Bremen
CakePHP-Version: 1.x.x
OS: OSX / Win 7 / Debian

Re: $validate und $belongsto

Beitragvon euromark » Do 7. Aug 2008, 21:21

hab mal was gelesen, dass
in das options array (validate=>first)
helfen soll bei mehreren models (auch in die tiefe gehend)
weil dann wirklich erst alle models validiert sein müssen
before dann save(all) etc ausgeführt wird
euromark
 
Beiträge: 618
Registriert: Fr 27. Jun 2008, 22:17
Wohnort: München
CakePHP-Version: 2.1
OS: Windows

Re: $validate und $belongsto

Beitragvon fleix » Di 12. Aug 2008, 00:52

moin moin

danke für die antworten. ich war im urlaub daher jetzt erst eine reaktion meinerseits.
validate=>first hilft leider nicht

hier mal meine beiden controller und der view in dem das formular der comments ist
Code: Alles auswählen
class CommentsController extends AppController
{
    var $name = 'Comments';
    #var $scaffold;
   
    function add($id = NULL) {
        if (!empty($this->data)) {
            $this->data['Comment']['post_id'] = $id;
            $this->data['Comment']['id'] = '';
            if ($this->Comment->save($this->data)) {
                $this->Session->setFlash('Kommentar hinzugefügt');
                $this->redirect($this->referer());
            }
        }
    }
}


Code: Alles auswählen
class PostsController extends AppController {
        var $name = 'Posts';
        var $helpers = array('Form','Datum','Ausgabe');
        function index() {
            $eintraege = $this->Post->findAll(NULL,NULL,'id DESC');
            $this->set('posts',$eintraege);
        }
        function view($id = NULL) {
            $this->Post->id = $id;
            $this->set('post',$this->Post->read());
            }
            function add() {
            if (!empty($this->data)) {
                    if ($this->Post->save($this->data)) {
                    $this->Session->setFlash('Der Beitrag wurde erfolgreich gespeichert');
                    $this->redirect('/posts');
                    } else $this->Session->setFlash('Fehler');
            }
            }
            function delete($id = NULL) {
                if(!empty($id)){
                    if ($this->Post->del($id)) {
                        $this->Session->setFlash('Der Beitrag wurde erfolgreich gelöscht');
                        $this->redirect('/posts');
                    } else {
                        $this->Session->setFlash('Fehler');
                        $this->redirect('/posts');
                    }
                }
            }
            function edit($id = NULL) {
            if (!empty($this->data)) {
                    if ($this->Post->save($this->data)) {
                    $this->Session->setFlash('Der Beitrag wurde erfolgreich geändert');
                    $this->redirect('/posts');
                    }
            }
            else {
                    $this->Post->id = $id;
                    $this->data = $this->Post->read();
                    $this->set('id',$id);
            }
            }
    }


view.ctp
Code: Alles auswählen
<h1><?php echo $ausgabe->out_put($post['Post']['titel']);?></h1>
<p><?php echo $ausgabe->out_put($post['Post']['inhalt']);?></p>
<p>Dieser Beitrag wurde erstellt am: <?php echo $datum->date_de($post['Post']['created'],1);?></p>
<p><?php echo $html->link('zurück zur Übersicht', '/posts/');?></p>
<h2>Kommentare:</h2>
<?php foreach($post['Comment'] as $comment):?>
<p>
    <?php echo $ausgabe->out_put($comment['autor']);?> schrieb am <?php echo $datum->date_de($comment['created'],1);?>:
</p>
<p><?php echo $ausgabe->out_put($comment['inhalt']);?></p>
<hr>
<?php endforeach;?>
<h2>Kommentar verfassen</h2>
<?php echo $form->create('Comment', array('action'=>'add/'.$post['Post']['id']));?>
<?php echo $form->input('autor');?>
<?php echo $form->input('inhalt');?>
<?php echo $form->submit('Kommentar senden');?>
<?php echo $form->end();?>


gruß
fleix
 
Beiträge: 2
Registriert: Mi 6. Aug 2008, 14:12


Zurück zu Models

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast