Thursday, July 29, 2010

Re: save data problem

I hope this help you

public function save($data = null, $validate = true, $fieldList =
array()) {
$return = false;
if (isset($data['Usercat']['subcat_id']) && is_array($data['Usercat']
['subcat_id'])) {
foreach ($data['Usercat']['subcat_id'] as $subcat) {
$saveData = array(
'user_id' => $data['Usercat']['user_id'],
'subcat_id' => $subcat,
);
$this->create();
$return = parent::save($saveData); // here can add some tests ...
}
}
return $return;
}

On Jul 29, 8:27 pm, hoss7 <hoss...@gmail.com> wrote:
> i have this array and user can choice many subcat:
>
> Array
> (
>     [Usercat] => Array
>         (
>             [subcat_id] => Array
>                 (
>                     [0] => 3
>                     [1] => 1
>                 )
>
>             [user_id] => 7
>         )
>
> )
>
> you say i can use this:
>
> function save($data = null, $validate = true, $fieldList = array()) {
>   put your code here  <=what code?
>   then
>
>   return parrent:: save($data, $validate, $fieldList) ; <= where?
>   or
>   put parrent:: save(..) into a cycle depending what you want to do.
>
> }
>
> but can you explain more about save function,i think you need more
> information about my problem
>
> i have 2 table: 1.subcats 2.usercats this is sql:
>
> CREATE TABLE IF NOT EXISTS `subcats` (
>   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
>   `cat_id` int(11) NOT NULL,
>   `title` varchar(255) COLLATE utf8_persian_ci NOT NULL,
>   `status` int(2) NOT NULL,
>   PRIMARY KEY (`id`)
> ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci
> AUTO_INCREMENT=4 ;
>
> --
> -- Dumping data for table `subcats`
> --
>
> INSERT INTO `subcats` (`id`, `cat_id`, `title`, `status`) VALUES
> (1, 2, 'sub2-1', 0),
> (2, 2, 'sub2-2', 0),
> (3, 1, 'sub1-1', 0);
>
> -- --------------------------------------------------------
>
> --
> -- Table structure for table `usercats`
> --
>
> CREATE TABLE IF NOT EXISTS `usercats` (
>   `id` int(11) NOT NULL AUTO_INCREMENT,
>   `user_id` int(11) NOT NULL,
>   `subcat_id` int(11) NOT NULL,
>   PRIMARY KEY (`id`)
> ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci
> AUTO_INCREMENT=11 ;
>
> --
> -- Dumping data for table `usercats`
> --
>
> INSERT INTO `usercats` (`id`, `user_id`, `subcat_id`) VALUES
> (3, 1, 1),
> (4, 6, 2),
> (10, 7, 1);
>
> and my model:
> subcat:
> var $hasMany = array(
>                 'Usercat' => array(
>                         'className' => 'Usercat',
>                         'foreignKey' => 'subcat_id',
>                         'dependent' => false,
>                         'conditions' => '',
>                         'fields' => '',
>                         'order' => '',
>                         'limit' => '',
>                         'offset' => '',
>                         'exclusive' => '',
>                         'finderQuery' => '',
>                         'counterQuery' => ''
>                 )
>         );
>
> and usercat:
>
> var $belongsTo = array(
>                         'Subcat' => array(
>                         'className' => 'Subcat',
>                         'foreignKey' => 'subcat_id',
>                         'conditions' => '',
>                         'fields' => '',
>                         'order' => ''
>                 )
>         );
>
> and in my UsercatsController:
>
> function add() {
>                 if (!empty($this->data)) {
>                         $this->Usercat->create();
>                         if ($this->Usercat->save($this->data)) {
>                                 $this->Session->setFlash(__('yes', true));
>                                 $this->redirect(array('action' => 'index'));
>                         } else {
>                                 $this->Session->setFlash(__('no', true));
>                         }
>                 }else {
>                         $this->Session->setFlash(__('no', true));
>                         $this->redirect(array('controller' => 'Subcats','action' =>
> 'usercat'));
>
>                 }
>         }
> and in usercat view:
>
> i have multiple select for user can choice many values
>
> i want save all values in `usercats` with one user id, i can only save
> one value but i want save all value,i think i must create some
> function like you say,but i am new in cakephp,please help me step by
> step

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: