开发者

Can I get a collection of versions for versionable Doctrine record?

So I'm trying to find a way to get a c开发者_StackOverflow中文版ollection of all versions of a versionable Doctrine Record. Is this possible? I could definitely build something that would do it but I wasn't sure if the functionality was already built in. Thanks in advance, John


I'm pretty sure that Doctrine's (at least 1.2 version) versionable behaviour doesn't define any additional methods for your model except revert() that allows you to revert previous version of resource.

So you'll have to do everything by your own.


you can query on the Version table: Doctrine::getTable('EntityVersions')->findAllById($entity_id);

gyaan: for any Version table, the PK is jointly placed on the id & version. Unlike default doctrine style, there is no surrogate key!


I implemented an access shortcut in the corresponding Table-Class e.g. model/doctrine/ItemTable.class.php:

public static function getHistoryInstance() {
    return self::getInstance()->getTemplate('Versionable')->getAuditLog()->getTable();
}

Then accessing the history like this from whereever:

    $history_collection = ItemTable::getHistoryInstance()->createQuery()
            ->select('id')
            ->execute();

(I'm using Symfony 1.4 with Doctrine 1.2)


Just in case anyone else stumbles upon this: Here is a quick solution how to get the versioned records. (This should of course better be implemented inside or as extension of the 'AuditLog' class, but for a quick solution the following can just go anywhere.)

/**
 * get collection of versioned records as array
 * 
 * @param string $table table name without the 'Version' -suffix (e.g. 'User' instead of 'UserVersion')
 * @param mixed $id simple id or array with key / value pairs for compound keys
 * @return array 
 */
public function getHistory($table, $ids) {

    $className = $table . 'Version';

    $identifiers = (array) Doctrine_Core::getTable($table)->getIdentifier();
    if (count($identifiers) === 1) {
        $ids = array_combine($identifiers, (array) $ids);
    }

    $q = Doctrine_Core::getTable($className)
            ->createQuery();

    $conditions = array();
    $values = array();
    foreach ($identifiers as $id) {
        if (!isset($ids[$id])) {
            throw new Doctrine_Record_Exception('Primary key \'' . $id . '\' must be given to access ' . $className . '.');
        }
        $conditions[] = $className . '.' . $id . ' = ?';
        $values[] = $ids[$id];
    }

    $q->where(implode(' AND ', $conditions));

    $data = $q->execute($values, Doctrine_Core::HYDRATE_ARRAY);

    return $data;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜