This commit is contained in:
parent
39bfb030c9
commit
3cea839f45
3 changed files with 44 additions and 36 deletions
|
@ -35,7 +35,8 @@ class Doctrine_Plugin
|
||||||
/**
|
/**
|
||||||
* @var array $_options an array of plugin specific options
|
* @var array $_options an array of plugin specific options
|
||||||
*/
|
*/
|
||||||
protected $_options = array('generateFiles' => false);
|
protected $_options = array('generateFiles' => false,
|
||||||
|
'identifier' => false);
|
||||||
/**
|
/**
|
||||||
* __get
|
* __get
|
||||||
* an alias for getOption
|
* an alias for getOption
|
||||||
|
@ -81,10 +82,6 @@ class Doctrine_Plugin
|
||||||
*/
|
*/
|
||||||
public function setOption($name, $value)
|
public function setOption($name, $value)
|
||||||
{
|
{
|
||||||
if ( ! isset($this->_options[$name])) {
|
|
||||||
throw new Doctrine_Plugin_Exception('Unknown option ' . $name);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->_options[$name] = $value;
|
$this->_options[$name] = $value;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
|
@ -34,14 +34,18 @@ class Doctrine_Search extends Doctrine_Plugin
|
||||||
{
|
{
|
||||||
const INDEX_FILES = 0;
|
const INDEX_FILES = 0;
|
||||||
|
|
||||||
const INDEX_TABLE = 1;
|
const INDEX_TABLES = 1;
|
||||||
|
|
||||||
protected $_options = array('generateFiles' => false,
|
protected $_options = array('generateFiles' => false,
|
||||||
|
'type' => self::INDEX_TABLES,
|
||||||
'className' => '%CLASS%Index',
|
'className' => '%CLASS%Index',
|
||||||
'generatePath' => false,
|
'generatePath' => false,
|
||||||
|
'resource' => null,
|
||||||
'batchUpdates' => false,
|
'batchUpdates' => false,
|
||||||
'pluginTable' => false,
|
'pluginTable' => false,
|
||||||
'fields' => array());
|
'fields' => array(),
|
||||||
|
'identifier' => null,
|
||||||
|
'connection' => null);
|
||||||
|
|
||||||
|
|
||||||
public function __construct(array $options)
|
public function __construct(array $options)
|
||||||
|
@ -51,6 +55,9 @@ class Doctrine_Search extends Doctrine_Plugin
|
||||||
if ( ! isset($this->_options['analyzer'])) {
|
if ( ! isset($this->_options['analyzer'])) {
|
||||||
$this->_options['analyzer'] = new Doctrine_Search_Analyzer_Standard();
|
$this->_options['analyzer'] = new Doctrine_Search_Analyzer_Standard();
|
||||||
}
|
}
|
||||||
|
if ( ! isset($this->_options['connection'])) {
|
||||||
|
$this->_options['connection'] = Doctrine_Manager::connection();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -104,33 +111,34 @@ class Doctrine_Search extends Doctrine_Plugin
|
||||||
|
|
||||||
public function readTableData($limit = null, $offset = null)
|
public function readTableData($limit = null, $offset = null)
|
||||||
{
|
{
|
||||||
|
$conn = $this->_options['resource']->getConnection();
|
||||||
|
$tableName = $this->_options['resource']->getTableName();
|
||||||
|
$id = $this->_options['identifier'];
|
||||||
|
|
||||||
|
$query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName)
|
||||||
|
. ' WHERE ' . $conn->quoteIdentifier($id)
|
||||||
|
. ' IN (SELECT ' . $conn->quoteIdentifier($id)
|
||||||
|
. ' FROM ' . $conn->quoteIdentifier($this->_options['pluginTable']->getTableName())
|
||||||
|
. ' WHERE keyword IS NULL)';
|
||||||
|
|
||||||
|
$query = $conn->modifyLimitQuery($query, $limit, $offset);
|
||||||
|
|
||||||
|
return $conn->fetchAll($query);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function processPending($limit = null, $offset = null)
|
public function processPending($limit = null, $offset = null)
|
||||||
{
|
{
|
||||||
$conn = $this->_options['ownerTable']->getConnection();
|
$id = $this->_options['identifier'];
|
||||||
$tableName = $this->_options['ownerTable']->getTableName();
|
$class = $this->_options['className'];
|
||||||
$component = $this->_options['ownerTable']->getComponentName();
|
$fields = $this->_options['fields'];
|
||||||
$id = $this->_options['ownerTable']->getIdentifier();
|
$conn = $this->_options['connection'];
|
||||||
$class = $this->getOption('className');
|
|
||||||
$fields = $this->getOption('fields');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
$conn->beginTransaction();
|
$conn->beginTransaction();
|
||||||
|
|
||||||
$query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName)
|
$rows = $this->readTableData($limit, $offset);
|
||||||
. ' WHERE ' . $conn->quoteIdentifier($id)
|
|
||||||
. ' IN (SELECT ' . $conn->quoteIdentifier($id)
|
|
||||||
. ' FROM ' . $conn->quoteIdentifier($this->_options['pluginTable']->getTableName())
|
|
||||||
. ' WHERE keyword IS NULL)';
|
|
||||||
|
|
||||||
$query = $conn->modifyLimitQuery($query, $limit, $offset);
|
|
||||||
|
|
||||||
$rows = $conn->fetchAll($query);
|
|
||||||
|
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
$ids[] = $row[$id];
|
$ids[] = $row[$id];
|
||||||
}
|
}
|
||||||
|
@ -179,13 +187,9 @@ class Doctrine_Search extends Doctrine_Plugin
|
||||||
|
|
||||||
$conn->insert($indexTableName, array('foreign_id' => $id));
|
$conn->insert($indexTableName, array('foreign_id' => $id));
|
||||||
}
|
}
|
||||||
public function buildDefinition(Doctrine_Table $table)
|
public function buildDefinition()
|
||||||
{
|
{
|
||||||
$name = $table->getComponentName();
|
|
||||||
|
|
||||||
$className = $this->getOption('className');
|
$className = $this->getOption('className');
|
||||||
|
|
||||||
$this->_options['ownerTable'] = $table;
|
|
||||||
|
|
||||||
if (class_exists($className)) {
|
if (class_exists($className)) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -203,18 +207,18 @@ class Doctrine_Search extends Doctrine_Plugin
|
||||||
'primary' => true,
|
'primary' => true,
|
||||||
));
|
));
|
||||||
|
|
||||||
$id = $table->getIdentifier();
|
$id = $this->_options['identifier'];
|
||||||
|
|
||||||
$options = array('className' => $className);
|
$options = array('className' => $className);
|
||||||
|
|
||||||
$fk = $this->generateForeignKeys($table);
|
$fk = $this->generateForeignKeys($this->_options['resource']);
|
||||||
$columns += $fk;
|
$columns += $fk;
|
||||||
|
|
||||||
$relations = $this->generateRelation($table, $fk);
|
$relations = $this->generateRelation($this->_options['resource'], $fk);
|
||||||
|
|
||||||
$this->generateClass($options, $columns, $relations);
|
$this->generateClass($options, $columns, $relations);
|
||||||
|
|
||||||
$this->_options['pluginTable'] = $table->getConnection()->getTable($this->_options['className']);
|
$this->_options['pluginTable'] = $this->_options['connection']->getTable($this->_options['className']);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,10 +52,17 @@ class Doctrine_Template_Searchable extends Doctrine_Template
|
||||||
$this->_plugin->setOption('className', str_replace('%CLASS%', $name, $className));
|
$this->_plugin->setOption('className', str_replace('%CLASS%', $name, $className));
|
||||||
$className = $this->_plugin->getOption('className');
|
$className = $this->_plugin->getOption('className');
|
||||||
}
|
}
|
||||||
$this->_plugin->buildDefinition($this->_table);
|
$this->_plugin->setOption('resource', $this->_table);
|
||||||
|
$this->_plugin->setOption('identifier', $this->_table->getIdentifier());
|
||||||
|
$this->_plugin->buildDefinition();
|
||||||
|
|
||||||
$this->hasMany($className, array('local' => $id, 'foreign' => $id));
|
$this->hasMany($className, array('local' => $id, 'foreign' => $id));
|
||||||
|
|
||||||
$this->addListener(new Doctrine_Search_Listener($this->_plugin));
|
$this->addListener(new Doctrine_Search_Listener($this->_plugin));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function processPending($limit = null, $offset = null)
|
||||||
|
{
|
||||||
|
$this->_plugin->processPending($limit, $offset);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue