0

I don't see anything wrong with my code. But it does not save data:

<?php
    class ProductsController extends AppController{
        var $name = 'Products';
        //var $helpers = array('Form');
        //var $scaffold;

        function index(){
            $this->Product->recursive = 1;
            $products = $this->Product->find('all');
            $this->set('products',$products);
            //pr($products);
        }

        function add(){
            $categories = $this->Product->Category->find('list',array(
                'field'=>array('Category.categoryName')
            ));
            $this->set('categories',$categories);

            if(!empty($this->data)){
                if($this->Product->save($this->data)){
                    $this->Session->setFlash('Saved');
                }
            }


        }
    }
?>

it flashes "Saved" but nothing is being inserted in my table. What could possibly be wrong when it should be functioning properly. :(

Below is my add.ctp model:

<h2>ADD</h2>

<?php echo $this->Form->create('Product',array('action'=>'add')); ?>
<?php
    echo $form->input('ProductName');
    echo $form->input('categories');
    echo $form->end('DONE');
?>
3
  • Well i saw my mistake with the help of my bestfriend ever "pr()" function. My add.ctp is wrong. It should go like this: <h2>ADD</h2> <?php echo $this->Form->create('Product',array('action'=>'add')); ?> <?php echo $form->input('productName'); echo $form->input('category_id'); echo $form->end('DONE'); ?> Commented May 2, 2012 at 3:56
  • try checking pr($this->data) before save... do you get the posted data..? Commented May 2, 2012 at 3:57
  • yep.. i did.. :) thank you so much to pr() function. Commented May 2, 2012 at 4:48

1 Answer 1

1

You have to use the create() method before to save

function add(){
        $categories = $this->Product->Category->find('list',array(
            'field'=>array('Category.categoryName')
        ));
        $this->set('categories',$categories);

        if(!empty($this->data)){

            $this->Product->create();
            if($this->Product->save($this->data)){
                $this->Session->setFlash('Saved');
            }
        }


    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.