私は Symfony プロジェクトを持っており、Zend Lucene 検索フレームワークを使用してサイトの検索を統合しました。美しく機能しますが、1 つのテーブルの検索に限定されます。
ユーザーがサイト全体 (8 つの選択テーブル) を検索し、結果をまとめて返すことができるようにする必要があります。各テーブルには、インデックスが作成された同じフィールドがあります。これは、テーブルを指定してクエリを呼び出すコードです。
結果を得るために 8 つのテーブルすべてを調べる方法はありますか?
public function getForLuceneQuery($query)
{
$hits = self::getLuceneIndex()->find($query);
$pks = array();
foreach ($hits as $hit)
{
$pks[] = $hit->pk;
}
if (empty($pks))
{
return array();
}
$alltables = Doctrine_Core::getTable('Car');
$q = $alltables->createQuery('j')
->whereIn('j.token', $pks)
->orderBy('j.endtime ASC')
->andwhere('j.endtime > ?', date('Y-m-d H:i:s', time()))
->andWhere('j.activated = ?', '1')
->limit(21);
return $q->execute();
}
8 つのテーブルの背景を少し説明すると、それらはすべて基本的に似ています。それらにはすべてタイトル、メーカー、モデルなどがあるため、それらすべてに対して単一のクエリを実行し、すべての結果を昇順で返す必要があります (テーブルに関係なく)。Doctrine_core::getTable コマンドは、複数のテーブルや配列を好まないようです (正しく実行していない限り)。ありがとう!
更新 (作業中):
これが更新されたコードです。これは私がSearchTable.class.php
ファイルに持っているものです:
public function getForLuceneQuery($query)
{
// sort search result by end time
$hits = self::getLuceneIndex()->find(
$query, 'endtime', SORT_NUMERIC, SORT_ASC
);
$result = array(
'index' => $hits,
'database' => array(),
);
// group search result by class
foreach ($hits as $hit)
{
if (!isset($result['database'][$hit->class]))
{
$result['database'][$hit->class] = array();
}
$result['database'][$hit->class][] = $hit->pk;
}
// replace primary keys with real results
foreach ($result['database'] as $class => $pks)
{
$result['database'][$class] = Doctrine_Query::create()
// important to INDEXBY the same field as $hit->pk
->from($class . ' j INDEXBY j.token')
->whereIn('j.token', $pks)
->orderBy('j.endtime ASC')
->andwhere('j.endtime > ?', date('Y-m-d H:i:s', time()))
->andWhere('j.activated = ?', '1')
->limit(21)
->execute();
}
return $result;
}
actions.class.php
検索モジュールのファイルにあるものは次のとおりです。
public function executeIndex(sfWebRequest $request)
{
$this->forwardUnless($query = $request->getParameter('query'), 'home', 'index');
$this->results = Doctrine_Core::getTable('Search')
->getForLuceneQuery($query);
}
そして最後に、これは私のテンプレート ファイルですindexSuccess.php
。理解しやすいように簡略化しています。私のindexSuccess.php
はもっと複雑ですが、値を呼び出すことができるようになったので、さらにカスタマイズできます。
<div class="product_list"
<ul>
<?php foreach ($results['index'] as $hit): ?>
<li class="item">
<?php if (isset($results['database'][$hit->class][$hit->pk])) ?>
<span class="title">
<?php echo $results['database'][$hit->class][$hit->pk]->getTitle() ?>
</span>
</li>
<?php endforeach ?>
</ul>
</div>
これは美しく機能します。検索結果の各フィールドを呼び出すことでカスタマイズでき、完璧に機能します。各テーブルに同じタイトルのアイテムを追加すると、検索結果にすべてが表示されました。どうもありがとう!