OneToManyLinkedLazy.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-2007 by Konstantin V. Arkhipov                     *
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 OneToManyLinkedLazy extends OneToManyLinkedWorker
00016     {
00020         public function makeFetchQuery()
00021         {
00022             $query =
00023                 $this->makeSelectQuery()->
00024                 dropFields()->
00025                 get($this->container->getChildIdField());
00026             
00027             return $this->targetize($query);
00028         }
00029         
00034         public function sync($insert, $update = array(), $delete)
00035         {
00036             Assert::isTrue($update === array());
00037             
00038             $db = DBPool::getByDao($this->container->getDao());
00039             
00040             $uc = $this->container;
00041             $dao = $uc->getDao();
00042 
00043             if ($insert)
00044                 $db->queryNull($this->makeMassUpdateQuery($insert));
00045 
00046             if ($delete) {
00047                 // unlink or drop
00048                 $uc->isUnlinkable()
00049                     ?
00050                         $db->queryNull($this->makeMassUpdateQuery($delete))
00051                     :
00052                         $db->queryNull(
00053                             OSQL::delete()->from($dao->getTable())->
00054                             where(
00055                                 Expression::in(
00056                                     $uc->getChildIdField(),
00057                                     $delete
00058                                 )
00059                             )
00060                         );
00061                 
00062                 $dao->uncacheByIds($delete);
00063             }
00064 
00065             return $this;
00066         }
00067         
00071         private function makeMassUpdateQuery($ids)
00072         {
00073             $uc = $this->container;
00074             
00075             return
00076                 OSQL::update($uc->getDao()->getTable())->
00077                 set($uc->getParentIdField(), null)->
00078                 where(
00079                     Expression::in(
00080                         $uc->getChildIdField(),
00081                         $ids
00082                     )
00083                 );
00084         }
00085     }
00086 ?>