Pasted php code

Anonymous paste - 21 June 2008 22:11:15 CEST - Viewed 27 times

class Search_Document extends Zend_Search_Lucene_Document{
 
   const KEYWORD = 'keyword';
   const UNINDEXED = 'unindex';
   const BINARY = 'binary';
   const TEXT = 'text';
   const UNSTORED = 'unstored';
 
   const MAX_STORE_LENGTH = 50;
 
   const SEARCH_IDENTIFIER = 'suid';
 
   protected $_model;
   protected $_record;
   protected $_ignoreCols = array();
 
   public function __construct( Doctrine_Record $record ){
 
      $this->_record = $record;
 
      $this->_preHook();
      $this->_propertiesToFields();
      $this->_postHook();
 
   }
 
   protected function _preHook() {}
 
 
   protected function _propertiesToFields() {
 
      $this->ignoreFields($this->_record->getTable()->getRelations());
      $this->_colsToFields($this->_record->getTable()->getColumns());
      $this->_relsToFields($this->_record->getTable()->getRelations());
 
   }
 
   public function ignoreFields( $fields ) {
      if(is_array($fields)) {
         foreach($fields as $field){
            $this->ignoreField($field['local']);
         }
      }
   }
 
   public function ignoreField( $name ) {
      $this->_ignoreCols[] = $name;
   }
 
   protected function _colToField( $name, $column ) {
      if(!in_array($name, $this->_ignoreCols)) {
         switch($column['type']) {
            case 'string':
               if( $column['length'] < self::MAX_STORE_LENGTH ) {
                  $this->addField($name, $this->_record->$name, self::TEXT);
               } else {
                  $this->addField($name, $this->_record->$name, self::UNSTORED);
               }
               break;
            case 'enum':
               $this->addField($name, $this->_record->$name, self::KEYWORD);
               break;
            case 'timestamp':
               $this->addField($name, $this->_record->$name, self::UNSTORED);
               break;
         }
      }
   }
 
   protected function _colsToFields( $cols ) {
      if(is_array($cols)) {
         foreach($cols as $name=>$column) {
            $this->_colToField($name, $column);
         }
         $this->addField(self::SEARCH_IDENTIFIER, $this->_record->getTable()->getTableName().'.'.$this->_record->oid());
      }
   }
 
   protected function _relsToFields( $relations ) {
      if(is_array($relations)) {
         foreach($relations as $relation) {
            $rel = $relation->toArray();
            switch($relation->getType()) {
               case Doctrine_Relation::ONE_AGGREGATE:
                  $this->addField($rel['alias'], $this->_record->$rel['alias']);
                  break;
               case Doctrine_Relation::MANY_AGGREGATE:
                  //TODO Build this somehow
                  break;
            }
         }
      }
   }
 
   public function addField($name, $value, $type = self::TEXT) {
 
      switch($type) {
 
         case self::KEYWORD:
            $field = Zend_Search_Lucene_Field::Keyword( $name, $value );
            break;
 
         case self::UNINDEXED:
            $field = Zend_Search_Lucene_Field::UnIndexed( $name, $value );
            break;
 
         case self::BINARY:
            $field = Zend_Search_Lucene_Field::Binary( $name, $value );
            break;
 
         case self::UNSTORED:
            $field = Zend_Search_Lucene_Field::UnStored( $name, $value );
            break;
 
         case self::TEXT:
         default:
            $field = Zend_Search_Lucene_Field::Text( $name, $value );
            break;
 
      }
 
      parent::addField( $field );
   }
 
   protected function _postHook() {}
 
}

Paste tree

- 7 months ago
    - 7 months ago
        - 7 months ago
            - 7 months ago

Submit a correction

Hashbin is currently in BETA version, please report any bugs/design/spam/abuse/problems/etc to hartym (at) dakrazy (dot) net.
Thanks.