From 32a4639aa4c0b5c709f30eeaf97f9a7a4740139d Mon Sep 17 00:00:00 2001 From: zYne Date: Sat, 13 Oct 2007 17:52:16 +0000 Subject: [PATCH] batch update functionality added --- lib/Doctrine/Search.php | 70 ++++++++++++++++++++------------ lib/Doctrine/Search/Template.php | 60 --------------------------- 2 files changed, 45 insertions(+), 85 deletions(-) delete mode 100644 lib/Doctrine/Search/Template.php diff --git a/lib/Doctrine/Search.php b/lib/Doctrine/Search.php index b6790eb6e..37ba6a4d7 100644 --- a/lib/Doctrine/Search.php +++ b/lib/Doctrine/Search.php @@ -96,35 +96,53 @@ class Doctrine_Search extends Doctrine_Plugin } } } - - public function processPendingTable($tableName, $indexTableName, array $fields, $id, $conn = null) + + public function processPending($limit = null, $offset = null) { - if ( ! ($conn instanceof Doctrine_Connection)) { - $conn = Doctrine_Manager::connection(); - } - $fields = array_merge($fields, array($id)); - $query = 'SELECT ' . implode(', ', $fields) . ' FROM ' . $tableName . ' WHERE ' - . $id . ' IN (SELECT foreign_id FROM ' - . $indexTableName - . ') ORDER BY ' . $id; + $conn = $this->_options['ownerTable']->getConnection(); + $tableName = $this->_options['ownerTable']->getTableName(); + $component = $this->_options['ownerTable']->getComponentName(); + $id = $this->_options['ownerTable']->getIdentifier(); + $class = $this->getOption('className'); + $fields = $this->getOption('fields'); - $data = $conn->fetchAll($query); + try { - foreach ($data as $row) { - $identifier = $row[$id]; - - unset($row[$id]); - - foreach ($row as $field => $data) { - $terms = $this->analyze($data); - - foreach ($terms as $pos => $term) { - $conn->insert($indexTableName, array('keyword' => $field, - 'position' => $pos, - 'field' => $field, - 'foreign_id' => $identifier)); + $conn->beginTransaction(); + + $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)'; + + $rows = $conn->fetchAll($query); + + foreach ($rows as $row) { + foreach ($fields as $field) { + $data = $row[$field]; + + $terms = $this->analyze($data); + + foreach ($terms as $pos => $term) { + $index = new $class(); + + $index->keyword = $term; + $index->position = $pos; + $index->field = $field; + + foreach ((array) $this->_options['ownerTable']->getIdentifier() as $id) { + $index->$id = $row[$id]; + } + + $index->save(); + } } } + + $conn->commit(); + } catch (Doctrine_Exception $e) { + $conn->rollback(); } } /** @@ -146,6 +164,8 @@ class Doctrine_Search extends Doctrine_Plugin $className = $this->getOption('className'); + $this->_options['ownerTable'] = $table; + if (class_exists($className)) { return false; } @@ -165,7 +185,7 @@ class Doctrine_Search extends Doctrine_Plugin $id = $table->getIdentifier(); $options = array('className' => $className); - + $fk = $this->generateForeignKeys($table); $columns += $fk; diff --git a/lib/Doctrine/Search/Template.php b/lib/Doctrine/Search/Template.php deleted file mode 100644 index 366ee3371..000000000 --- a/lib/Doctrine/Search/Template.php +++ /dev/null @@ -1,60 +0,0 @@ -. - */ - -/** - * Doctrine_Search_Template - * - * @package Doctrine - * @subpackage Search - * @author Konsta Vesterinen - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @version $Revision$ - * @link www.phpdoctrine.com - * @since 1.0 - */ -class Doctrine_Search_Template extends Doctrine_Template -{ - protected $_search; - - public function __construct(array $options) - { - $this->_search = new Doctrine_Search($options); - } - public function setUp() - { - $this->_search->buildDefinition($this->_table); - - $id = $this->_table->getIdentifier(); - $name = $this->_table->getComponentName() . 'Index'; - - $this->_search->setOption('className', $name); - - foreach ((array) $id as $column) { - $foreign[] = strtolower($this->_table->getComponentName() . '_' . $column); - } - - $foreign = (count($foreign) > 1) ? $foreign : current($foreign); - - $this->hasMany($name, array('local' => $id, 'foreign' => $foreign)); - - $this->addListener(new Doctrine_Search_Listener($this->_search)); - } -} \ No newline at end of file