00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class DaoUtils extends StaticFactory
00017 {
00018 private static $nullValue = 0;
00019
00020 public static function swap(
00021 DAOConnected $first,
00022 DAOConnected $second,
00023 $property = 'position'
00024 )
00025 {
00026 Assert::isTrue(
00027 get_class($first) === get_class($second)
00028 );
00029
00030 $setMethod = 'set'.ucfirst($property);
00031 $getMethod = 'get'.ucfirst($property);
00032
00033 Assert::isTrue(
00034 method_exists($first, $setMethod)
00035 && method_exists($first, $getMethod)
00036 );
00037
00038 $dao = $first->dao();
00039 $db = DBPool::me()->getByDao($dao);
00040
00041 $oldPosition = $first->$getMethod();
00042 $newPosition = $second->$getMethod();
00043
00044 $db->begin();
00045
00046 $e = null;
00047
00048 try {
00049 $dao->save(
00050 $first->$setMethod(self::$nullValue)
00051 );
00052
00053 $dao->save(
00054 $second->$setMethod($oldPosition)
00055 );
00056
00057 $dao->save(
00058 $first->$setMethod($newPosition)
00059 );
00060
00061 $db->commit();
00062 } catch (DatabaseException $e) {
00063 $db->rollback();
00064 }
00065
00066 $dao->
00067 uncacheByIds(
00068 array(
00069 $first->getId(), $second->getId()
00070 )
00071 );
00072
00073 if ($e)
00074 throw $e;
00075 }
00076
00077 public static function setNullValue($nullValue)
00078 {
00079 self::$nullValue = $nullValue;
00080 }
00081 }
00082 ?>