# DaoUtils.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2007 by Nickolay G. Korolyov                            *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00015     final class DaoUtils extends StaticFactory
    00016     {
    00017         private static $nullValue   = 0;
    00018
    00019         /* void */ public static function swap(
    00020             DAOConnected $first,
    00021             DAOConnected $second,
    00022             $property = 'position'
    00023         )
    00024         {
    00025             Assert::isTrue(
    00026                 get_class($first) === get_class($second)
    00027             );
    00028
    00029             $setMethod = 'set'.ucfirst($property);
    00030             $getMethod = 'get'.ucfirst($property);
    00031
    00032             Assert::isTrue(
    00033                 method_exists($first, $setMethod)
    00034                 && method_exists($first, $getMethod)
    00035             );
    00036
    00037             $dao = $first->dao();
    00038             $db = DBPool::me()->getByDao($dao);
    00039
    00040             $oldPosition = $first->$getMethod();
    00041             $newPosition = $second->$getMethod();
    00042
    00043             $db->begin();
    00044
    00045             $e = null;
    00046
    00047             try {
    00048                 $dao->save(
    00049                     $first->$setMethod(self::$nullValue)
    00050                 );
    00051
    00052                 $dao->save(
    00053                     $second->$setMethod($oldPosition)
    00054                 );
    00055
    00056                 $dao->save(
    00057                     $first->$setMethod($newPosition)
    00058                 );
    00059
    00060                 $db->commit();
    00061             } catch (DatabaseException $e) {
    00062                 $db->rollback();
    00063             }
    00064
    00065             $dao->
    00066                 uncacheByIds(
    00067                     array(
    00068                         $first->getId(), $second->getId()
    00069                     )
    00070                 );
    00071
    00072             if ($e)
    00073                 throw $e;
    00074         }
    00075
    00076         /* void */ public static function setNullValue($nullValue)
    00077         {
    00078             self::$nullValue = $nullValue;
    00079         }
    00080
    00081         public static function increment(
    00082             DAOConnected &$object,
    00083             array $fields /* fieldName => value */,
    00084             $refreshCurrent = true,
    00085             /*UpdateQuery*/ $query = null
    00086         )
    00087         {
    00088             $objectDao = $object->dao();
    00089
    00090             if ($query)
    00091                 $updateQuery = $query;
    00092             else
    00093                 $updateQuery =
    00094                     OSQL::update()->setTable($objectDao->getTable())->
    00095                     where(Expression::eqId('id', $object));
    00096
    00097             $mapping = $objectDao->getProtoClass()->getMapping();
    00098
    00099             foreach ($mapping as $field => $column)
    00100                 if (isset($fields[$field]))
    00101                     $updateQuery->set(
    00102                         $column,
    00103                         Expression::add($column, $fields[$field])
    00104                     );
    00105
    00106             $updateCount =
    00107                 DBPool::getByDao($objectDao)->queryCount($updateQuery);
    00108
    00109             if ($query)
    00110                 $objectDao->uncacheLists();
    00111             else
    00112                 $objectDao->uncacheById($object->getId());
    00113
    00114             if ($refreshCurrent && !$query)
    00115                 $object = $objectDao->getById($object->getId());
    00116
    00117             return $updateCount;
    00118         }
    00119     }
    00120 ?>