Hi All,

I need help in using saveAll() method for editing, I have a form that
needs to save data to 2 different models, Subscriber and Individual
subscriber. But everytime i edit data, a new data is being created
instead. The following is my codes:

subscriber.php (model)
class Subscriber extends AppModel {

        var $name = 'Subscriber';
    var $order = 'Subscriber.create_date DESC';
        //The Associations below have been created with all possible keys,
those that are not needed can be removed
        var $hasOne = array(
                        'CompanySubscriber' => array('className' => 
'CompanySubscriber',
                                                                'foreignKey' => 
'subscriber_id',
                                                                'conditions' => 
'',
                                                                'fields' => '',
                                                                'order' => '',
                                                                'dependent' => 
''),
                        'IndividualSubscriber' => array('className' =>
'IndividualSubscriber',
                                                                'foreignKey' => 
'subscriber_id',
                                                                'conditions' => 
'',
                                                                'fields' => '',
                                                                'order' => '',
                                                                'dependent' => 
''),
        );

        var $hasMany = array(
                        'SubscriberContact' => array('className' => 
'SubscriberContact',
                                                                'foreignKey' => 
'subscriber_id',
                                                                'conditions' => 
'',
                                                                'fields' => '',
                                                                'order' => '',
                                                                'limit' => '',
                                                                'offset' => '',
                                                                'dependent' => 
'',
                                                                'exclusive' => 
'',
                                                                'finderQuery' 
=> '',
                                                                'counterQuery' 
=> ''),
                        'Transaction' => array('className' => 'Transaction',
                                                                'foreignKey' => 
'subscriber_id',
                                                                'conditions' => 
'',
                                                                'fields' => '',
                                                                'order' => '',
                                                                'limit' => '',
                                                                'offset' => '',
                                                                'dependent' => 
'',
                                                                'exclusive' => 
'',
                                                                'finderQuery' 
=> '',
                                                                'counterQuery' 
=> ''),
        );

    var $belongsTo = array(
            'Country' => array('className' => 'Country',
                                                                'foreignKey' => 
'country_name',
                                                                'conditions' => 
'',
                                                                'fields' => '',
                                                                'order' => '',
                                                                'limit' => '',
                                                                'offset' => '',
                                                                'dependent' => 
'',
                                                                'exclusive' => 
'',
                                                                'finderQuery' 
=> '',
                                                                'counterQuery' 
=> ''),
            'State' => array('className' => 'State',
                                                                'foreignKey' => 
'state_name',
                                                                'conditions' => 
'',
                                                                'fields' => '',
                                                                'order' => '',
                                                                'limit' => '',
                                                                'offset' => '',
                                                                'dependent' => 
'',
                                                                'exclusive' => 
'',
                                                                'finderQuery' 
=> '',
                                                                'counterQuery' 
=> ''),
        );

subscriber_controller.php
class SubscribersController extends AppController {

        var $name = 'Subscribers';
        var $helpers = array('Html', 'Form');
    var $uses = array('Subscriber', 'CompanySubscriber',
'IndividualSubscriber');
    //uses('neat_string');

        function index() {
                $this->Subscriber->recursive = 0;
                $this->set('subscribers', $this->paginate());
        }

        function view($id = null) {
                if (!$id) {
                        $this->Session->setFlash('Invalid Subscriber.');
                        $this->redirect(array('action'=>'index'), null, true);
                }
                $this->set('subscriber', $this->Subscriber->read(null, $id));
        }

        function add() {
                if (!empty($this->data)) {
                        $this->helpers[] = 'Time';
            $this->data['Subscriber']['activation_code'] = $this-
>generate_code();
            $this->data['Subscriber']['create_staffid'] = $this-
>getStaffID();
            $this->data['Subscriber']['create_date'] = $this-
>generate_date();
            $this->data['Subscriber']['update_staffid'] = $this-
>getStaffID();
            $this->data['Subscriber']['update_date'] = $this-
>generate_date();
                        $this->Subscriber->create();
                        if ($this->Subscriber->saveAll($this->data, array
('validate'=>'first'))) {
                                $this->Session->setFlash('The Subscriber has 
been saved');
                                $this->redirect(array('action'=>'index'), null, 
true);
                        } else {
                                $this->Session->setFlash('The Subscriber could 
not be saved.
Please, try again.');
                        }
                }
        $countries = $this->Subscriber->Country->find('list');
        $states = $this->Subscriber->State->find('list');
        $this->set(compact('countries', 'states'));
        }

        function edit($id = null) {
                if (!$id && empty($this->data)) {
                        $this->Session->setFlash('Invalid Subscriber');
                        $this->redirect(array('action'=>'index'), null, true);
                }
                if (!empty($this->data)) {
                        //$this->cleanUpFields();
                        if 
($this->Subscriber->saveAll($this->data['Subscriber'])) {
                                $this->Session->setFlash('The Subscriber has 
been saved');
                                $this->redirect(array('action'=>'index'), null, 
true);
                        } else {
                                $this->Session->setFlash('The Subscriber could 
not be saved.
Please, try again.');
                        }
                }
                if (empty($this->data)) {
                        $this->data = $this->Subscriber->read(null, $id);
                }
        $countries = $this->Subscriber->Country->find('list');
        $states = $this->Subscriber->State->find('list');
        $this->set(compact('countries', 'states'));
        }

        function delete($id = null) {
                if (!$id) {
                        $this->Session->setFlash('Invalid id for Subscriber');
                        $this->redirect(array('action'=>'index'), null, true);
                }
                if ($this->Subscriber->CompanySubscriber->del($id) or $this-
>Subscriber->IndividualSubscriber->del($id)) {
            $this->Subscriber->del($id);
                        $this->Session->setFlash('Subscriber deleted');
                        $this->redirect(array('action'=>'index'), null, true);
                }
        }

    // This function generates the activation code
    function generate_code()
    {
        return substr(md5(mt_rand(0, 99999999999)), mt_rand(0, 19),
mt_rand(8,12));
    }

    /* This function returns the current datetime in SQL format. To be
used for
     * getting the datetime.
     */
    function generate_date()
    {
        date_default_timezone_set('Asia/Singapore');
        return date("Y-m-d H:i:s");
    }

    function getStaffID()
    {
        return "gangzheng";
        /* to change. The ID should be gotten from session*/
    }
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to