1
0
mirror of synced 2025-01-18 22:41:43 +03:00

batch update functionality added

This commit is contained in:
zYne 2007-10-13 17:52:16 +00:00
parent 7d7313ab4a
commit 32a4639aa4
2 changed files with 45 additions and 85 deletions

View File

@ -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 = $this->_options['ownerTable']->getConnection();
$conn = Doctrine_Manager::connection(); $tableName = $this->_options['ownerTable']->getTableName();
} $component = $this->_options['ownerTable']->getComponentName();
$fields = array_merge($fields, array($id)); $id = $this->_options['ownerTable']->getIdentifier();
$query = 'SELECT ' . implode(', ', $fields) . ' FROM ' . $tableName . ' WHERE ' $class = $this->getOption('className');
. $id . ' IN (SELECT foreign_id FROM ' $fields = $this->getOption('fields');
. $indexTableName
. ') ORDER BY ' . $id;
$data = $conn->fetchAll($query); try {
foreach ($data as $row) { $conn->beginTransaction();
$identifier = $row[$id];
$query = 'SELECT * FROM ' . $conn->quoteIdentifier($tableName)
unset($row[$id]); . ' WHERE ' . $conn->quoteIdentifier($id)
. ' IN (SELECT ' . $conn->quoteIdentifier($id)
foreach ($row as $field => $data) { . ' FROM ' . $conn->quoteIdentifier($this->_options['pluginTable']->getTableName())
$terms = $this->analyze($data); . ' WHERE keyword IS NULL)';
foreach ($terms as $pos => $term) { $rows = $conn->fetchAll($query);
$conn->insert($indexTableName, array('keyword' => $field,
'position' => $pos, foreach ($rows as $row) {
'field' => $field, foreach ($fields as $field) {
'foreign_id' => $identifier)); $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'); $className = $this->getOption('className');
$this->_options['ownerTable'] = $table;
if (class_exists($className)) { if (class_exists($className)) {
return false; return false;
} }
@ -165,7 +185,7 @@ class Doctrine_Search extends Doctrine_Plugin
$id = $table->getIdentifier(); $id = $table->getIdentifier();
$options = array('className' => $className); $options = array('className' => $className);
$fk = $this->generateForeignKeys($table); $fk = $this->generateForeignKeys($table);
$columns += $fk; $columns += $fk;

View File

@ -1,60 +0,0 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Search_Template
*
* @package Doctrine
* @subpackage Search
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @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));
}
}