findFacts($options['fact'],$options); default: return parent::find($type, $options); } } // this method will always be specific to each fact model function gather( $start_time = null ) { debug($this->alias.' must implement gather()'); return false; } //-- 'mapped' re-arranges the results in an array-hierarchy according to the group parameter. //-- E.G. grouping by weekday might return array keys mon,tue... instead of 0,1... function findFacts($fact, $options) { $defaults = array( 'conditions' =>'', 'fields' =>array(), 'order' =>'', 'group' =>'', 'mapped'=>false ); $options = array_merge($defaults,$options); $dimensions = $this->getAssociated('belongsTo'); $joins = $this->useTable.' AS '.$this->alias; $this_name = $this->alias; foreach ( $dimensions as $k => $dim ) { $dimension = $this->$dim->useTable.' AS '.$dim; $fk = $this->belongsTo[$dim]['foreignKey']; $joins = "($dimension INNER JOIN $joins ON $dim.id = $this_name.$fk)"; } $fields = array_merge($options['fields'],array($fact.' AS '.$this->alias.'__fact',$options['group'],$options['order'])); $fields = array_unique($fields); $db =& ConnectionManager::getDataSource($this->useDbConfig); $query = $db->renderStatement('select', array( 'conditions' => $db->conditions($options['conditions'], true, true, $this), 'fields' => join(', ', $fields), 'table' => '', 'alias' => '', 'order' => $db->order($options['order']), 'limit' => '', 'joins' => $joins, 'group' => $db->group($options['group']) )); $raw_facts = $this->query($query); if ( $options['mapped'] ) { $group_str = str_replace(' ','',$options['group']); $groups = explode(',',$group_str); $last_group = array_pop($groups); $mapped_facts = array(); foreach ( $raw_facts as $key => $val ) { $domain =& $mapped_facts; foreach ( $groups as $group ) { $gKey = Set::extract($raw_facts,$key.'.'.$group); if ( !isset($domain[ $gKey ]) ) { $domain[ $gKey ] = array(); } $domain =& $domain[ $gKey ]; } $gKey = Set::extract($raw_facts,$key.'.'.$last_group); $domain[$gKey] = $val; } return $mapped_facts; } else { return $raw_facts; } } function saveFact($fact) { $keys = array_keys($fact[$this->alias]); $values = array_values($fact[$this->alias]); $fields = $this->_getFactFields(); $update = ''; foreach ( $fields as $field ) { $update .= ' `'.$field.'` = '.$fact[$this->alias][$field].','; } $query = 'INSERT INTO `'.$this->useTable.'` (`'. implode('`,`', $keys) .'`) VALUES ('. implode(',', $values) .') ON DUPLICATE KEY UPDATE'.substr($update,0,-1); $this->query($query); } function _getFactFields() { $fields = array(); foreach ( $this->_schema as $field => $params ) { if ( !isset($params['key']) ) { $fields[] = $field; } } return $fields; } } ?>