Tuesday, February 12, 2013

Struggling with first cake development.

Okay here is where i am

have created model, controller, views
can select and view table data

cant edit or add new record,
My model validation rules not being applied when submitting data.

when you add a record it says added successful , but no records show up in table and validation rules obviously not being applied before save.

i am thinking its to do with cakes auto-magic thing, but not understanding why its not using my model and linking to database.

I obviously can link to table as it fetches the data for viewing records just fails on changing or adding data.

any help on getting over this first hurdle would be appreciated. its obviously something obvious, but all my debugging attempts shows nothing that i understand ..


my code

CategoriesController.php
<?php
class CategoriesController extends AppController {
    public $helpers = array('Html', 'Form');
    public $components = array('Session');
   
     public function index() {
         $this->set('Categories', $this->Category->find('all'));
    }   
     public function view($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid category'));
        }
        $CatRec = $this->Category->findById($id);
        if (!$CatRec) {
            throw new NotFoundException(__('Invalid category'));
        }
        $this->set('Categories', $CatRec);
    }
     public function add() {
        if ($this->request->is('post')) {
             $this->Category->create();
             if ($this->Category->validates()) {
           
             if ($this->Category->save($this->request->data)) {
                $this->Session->setFlash('Your Category has been saved.');
              //  $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Unable to add your Category.');
            }
        }else {
                $this->Session->setFlash('Unable to validate.');
            }
       
        }
    }   
}

Category.php

<?php
class Category extends AppModel {
    public $validate = array(
        'Name' => array('rule' => 'notEmpty'),
        'Description' => array('rule' => 'notEmpty'),
        'Type' => array('rule' => 'notEmpty')
    );
}

any help appreciated

Thomas 


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: