1

我一直在关注 Jobeet 教程,以便将 Zend Lucene 搜索集成到我的 Symfony 项目中,但我似乎无法让它工作。我已将以下代码添加到我的 config/ProjectConfiguration.class.php

class ProjectConfiguration extends sfProjectConfiguration
{

  static protected $zendLoaded = false;

  static public function registerZend()
  {
    if (self::$zendLoaded)
    {
      return;
    }

    set_include_path(sfConfig::get('sf_lib_dir').'/vendor'.PATH_SEPARATOR.get_include_path());
    require_once sfConfig::get('sf_lib_dir').'/vendor/Zend/Loader/Autoloader.php';
    Zend_Loader_Autoloader::getInstance();
    self::$zendLoaded = true;
  }

我也将此代码添加到我的 CarTable.class.php

    static public function getLuceneIndex()
    {
      ProjectConfiguration::registerZend();

      if (file_exists($index = self::getLuceneIndexFile()))
      {
        return Zend_Search_Lucene::open($index);
      }

      return Zend_Search_Lucene::create($index);
    }

    static public function getLuceneIndexFile()
    {
      return sfConfig::get('sf_data_dir').'/car.'.sfConfig::get('sf_environment').'.index';
    }

最后,我将以下代码添加到我的 Car.class.php

    public function updateLuceneIndex()
    {
      $index = CarTable::getLuceneIndex();


      foreach ($index->find('pk:'.$this->getIditem()) as $hit)
      {
        $index->delete($hit->id);
      }


      if (!$this->getActivated())
      {
        return;
      }

      $doc = new Zend_Search_Lucene_Document();


      $doc->addField(Zend_Search_Lucene_Field::Keyword('pk', $this->getIditem()));


      $doc->addField(Zend_Search_Lucene_Field::UnStored('title', $this->getTitle(), 'utf-8'));
      $doc->addField(Zend_Search_Lucene_Field::UnStored('features', $this->getFeatures(), 'utf-8'));
      $doc->addField(Zend_Search_Lucene_Field::UnStored('location_city', $this->getLocation_city(), 'utf-8'));
      $doc->addField(Zend_Search_Lucene_Field::UnStored('location_state', $this->getLocation_state(), 'utf-8'));


      $index->addDocument($doc);
      $index->commit();
    }


    public function delete(Doctrine_Connection $conn = null)
    {
      $index = CarTable::getLuceneIndex();

      foreach ($index->find('pk:'.$this->getIditem()) as $hit)
      {
        $index->delete($hit->id);
      }

      return parent::delete($conn);
    }

当然,我在保存函数中添加了这几行:

      $conn = $conn ? $conn : $this->getTable()->getConnection();
      $conn->beginTransaction();
      try
      {
        $ret = parent::save($conn);

        $this->updateLuceneIndex();

        $conn->commit();

        return $ret;
      }
      catch (Exception $e)
      {
        $conn->rollBack();
        throw $e;
      }

我已将 Zend 框架的“库”文件夹的内容提取到我的 lib/vendor/Zend 文件夹中。

但是每次我运行命令

php symfony doctrine:data-load

它一直运行并加载我所有的装置,但它不会在我的数据文件夹中创建包含索引文件的文件夹。我不确定我做错了什么。

这是我的参考资料(Jobeet 教程) http://www.symfony-project.org/jobeet/1_4/Doctrine/en/17

顺便说一句,我使用的是 Symfony 1.4 (Doctrine)。

4

0 回答 0