Ok, I'm going to try to make this quick.  This _works_ but I think
there is a better way to do it.

Tables:

CREATE TABLE `ratings` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `rating_types_id` tinyint(3) default NULL,
  `editorial_reviews_id` int(11) default NULL,
  `service_types_id` int(11) default NULL,
  `rating` tinyint(2) unsigned NOT NULL default '0',
  `created` datetime default NULL,
  `modified` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=403 DEFAULT CHARSET=latin1

CREATE TABLE `rating_types` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `published` tinyint(1) default NULL,
  `created` datetime default NULL,
  `updated` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=latin1

CREATE TABLE `service_types` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `published` tinyint(1) default NULL,
  `created` datetime default NULL,
  `updated` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1

CREATE TABLE `editorial_reviews` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) default NULL,
  `review` text,
  `published` tinyint(1) default NULL,
  `created` datetime default NULL,
  `updated` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1

CREATE TABLE `editorial_reviews_service_types` (
  `editorial_reviews_id` int(11) default NULL,
  `service_types_id` tinyint(2) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
=====================================================================================

editorial_reviews habtm service_types

Anyways, so the main focus point is the "ratings" table.  Essentially,
I want to store the ratings based on a specific editorial and service
type, hence the reason the columns are both present in that table.

The problem is, I have to do a little trickery here, where I have to
write some beforeSave logic to actually insert the rows, and I just
think Cake has an easier way to handle something like this.

Take for instance from the editorial_review.php model:
        function beforeSave()
        {
            if(count($this->data['ServiceType']['ServiceType']) > 0 && count(@
$this->data['Rating']) > 0)) {

                        // Mapping ServiceTypes
                    $service_types = array();
                        if(isset($this->data['ServiceType']['ServiceType'][0])) 
{
                            foreach(@$this->data['ServiceType']['ServiceType'] 
as $key=>
$val) {
                                $service_types[$val] = $val;
                            }
                        }



                        foreach($this->data['Rating'] as $step) {
                                foreach($step as $key => $val) {
                                    if(!empty($val['rating'])) {
                                        // Check if service_type is checked
                                        
if(@$service_typ...@$val['service_types_id']] == @
$val['service_types_id']) {
                                            $data_array['Rating'] = array(
                                                                      'id'      
             => @
$val['id'],
                                                                                
                  'editorial_reviews_id' =>
$val['editorial_reviews_id'],
                                                                                
                  'service_types_id'     =>
$val['service_types_id'],
                                                                                
                  'rating_types_id'      =>
$val['rating_types_id'],
                                                                                
                  'rating'               => $val['rating']);
                                                //pr($data_array);
                                                
$this->Rating->save($data_array);
                                                } else {
                                        // Play clean-up
                                        $this->Rating-
>deleteAll(array('Rating.editorial_reviews_id' =>
$val['editorial_reviews_id'], 'Rating.service_types_id' =>
$val['service_types_id']));
                                    }
                                    }
                                }
                        }
                        unset($this->data['Rating']);
                  }
                }
                return $this->data;
}

(sorry for the mess, I feel this is more of a hack than anything...

Please help me figure out a better way to handle this, to be more
reusable, I guess is what I'm saying?

Thanks,
Jake

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 [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

To unsubscribe from this group, send email to 
cake-php+unsubscribegooglegroups.com or reply to this email with the words 
"REMOVE ME" as the subject.

Reply via email to