This commit is contained in:
parent
0813509b5c
commit
b32163fab3
@ -32,7 +32,29 @@
|
|||||||
*/
|
*/
|
||||||
class Doctrine_Search
|
class Doctrine_Search
|
||||||
{
|
{
|
||||||
public function buildDefinition(Doctrine_Record $record)
|
protected $_options = array('generateFiles' => true);
|
||||||
|
|
||||||
|
public function __construct(array $options)
|
||||||
|
{
|
||||||
|
$this->_options = array_merge($this->_options, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getOption($option)
|
||||||
|
{
|
||||||
|
if (isset($this->_options[$option])) {
|
||||||
|
return $this->_option[$option];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setOption($option, $value)
|
||||||
|
{
|
||||||
|
$this->_options[$option] = $value;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function buildDefinition(Doctrine_Table $table)
|
||||||
{
|
{
|
||||||
|
|
||||||
$columns = array('keyword' => array('type' => 'string',
|
$columns = array('keyword' => array('type' => 'string',
|
||||||
@ -44,15 +66,15 @@ class Doctrine_Search
|
|||||||
'position' => array('type' => 'integer',
|
'position' => array('type' => 'integer',
|
||||||
'length' => 8));
|
'length' => 8));
|
||||||
|
|
||||||
$id = $record->getTable()->getIdentifier();
|
$id = $table->getIdentifier();
|
||||||
$name = $record->getTable()->getComponentName();
|
$name = $table->getComponentName();
|
||||||
|
|
||||||
$options = array('className' => $name . 'Index');
|
$options = array('className' => $name . 'Index');
|
||||||
|
|
||||||
|
|
||||||
$fk = array();
|
$fk = array();
|
||||||
foreach ((array) $id as $column) {
|
foreach ((array) $id as $column) {
|
||||||
$def = $record->getTable()->getDefinitionOf($column);
|
$def = $table->getDefinitionOf($column);
|
||||||
|
|
||||||
unset($def['autoincrement']);
|
unset($def['autoincrement']);
|
||||||
unset($def['sequence']);
|
unset($def['sequence']);
|
||||||
@ -77,18 +99,12 @@ class Doctrine_Search
|
|||||||
|
|
||||||
$def = $builder->buildDefinition($options, $columns, $relations);
|
$def = $builder->buildDefinition($options, $columns, $relations);
|
||||||
|
|
||||||
|
if ( ! $this->_options['generateFiles']) {
|
||||||
|
eval($def);
|
||||||
|
}
|
||||||
|
/**
|
||||||
print "<pre>";
|
print "<pre>";
|
||||||
print_r($def);
|
print_r(htmlentities($def));
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
fields:
|
|
||||||
[keyword] [field] [foreign_id] [position]
|
|
||||||
|
|
||||||
|
|
||||||
fields:
|
|
||||||
[keyword] [field] [match]
|
|
||||||
|
|
||||||
example data:
|
|
||||||
'orm' 'content' '1:36|2:23'
|
|
||||||
*/
|
|
||||||
|
@ -34,6 +34,12 @@ class Doctrine_Search_Analyzer_Standard implements Doctrine_Search_Analyzer_Inte
|
|||||||
{
|
{
|
||||||
public function analyze($text)
|
public function analyze($text)
|
||||||
{
|
{
|
||||||
|
$text = explode(' ', $text);
|
||||||
|
|
||||||
|
foreach ($text as $i => $term) {
|
||||||
|
$text[$i] = strtolower(trim($term));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,9 +32,15 @@
|
|||||||
*/
|
*/
|
||||||
class Doctrine_Search_Listener extends Doctrine_Record_Listener
|
class Doctrine_Search_Listener extends Doctrine_Record_Listener
|
||||||
{
|
{
|
||||||
|
protected $_search;
|
||||||
|
|
||||||
|
public function __construct(Doctrine_Search $search)
|
||||||
|
{
|
||||||
|
$this->_search = $search;
|
||||||
|
}
|
||||||
|
|
||||||
public function preUpdate(Doctrine_Event $event)
|
public function preUpdate(Doctrine_Event $event)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function postUpdate(Doctrine_Event $event)
|
public function postUpdate(Doctrine_Event $event)
|
||||||
@ -49,6 +55,14 @@ class Doctrine_Search_Listener extends Doctrine_Record_Listener
|
|||||||
|
|
||||||
public function postInsert(Doctrine_Event $event)
|
public function postInsert(Doctrine_Event $event)
|
||||||
{
|
{
|
||||||
|
$fields = $this->_search->getOption('fields');
|
||||||
|
|
||||||
|
foreach ($fields as $field) {
|
||||||
|
$terms = $this->_search->analyze($field);
|
||||||
|
|
||||||
|
foreach ($terms as $term) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,10 +32,20 @@
|
|||||||
*/
|
*/
|
||||||
class Doctrine_Search_Template extends Doctrine_Template
|
class Doctrine_Search_Template extends Doctrine_Template
|
||||||
{
|
{
|
||||||
|
protected $_search;
|
||||||
|
|
||||||
|
public function __construct(array $options)
|
||||||
|
{
|
||||||
|
$this->_search = new Doctrine_Search($options);
|
||||||
|
}
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$id = $record->getTable()->getIdentifier();
|
$this->_search->buildDefinition($this->_table);
|
||||||
$name = $record->getTable()->getComponentName() . 'Index';
|
|
||||||
|
$id = $this->_table->getIdentifier();
|
||||||
|
$name = $this->_table->getComponentName() . 'Index';
|
||||||
|
|
||||||
|
$this->_search->setOption('className', $name);
|
||||||
|
|
||||||
foreach ((array) $id as $column) {
|
foreach ((array) $id as $column) {
|
||||||
$foreign[] = strtolower($name . '_' . $column);
|
$foreign[] = strtolower($name . '_' . $column);
|
||||||
@ -45,6 +55,6 @@ class Doctrine_Search_Template extends Doctrine_Template
|
|||||||
|
|
||||||
$this->hasMany($name, array('local' => $id, 'foreign' => $foreign));
|
$this->hasMany($name, array('local' => $id, 'foreign' => $foreign));
|
||||||
|
|
||||||
$this->addListener(new Doctrine_Search_Listener());
|
$this->addListener(new Doctrine_Search_Listener($this->_search));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user