Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP5] Zapisywanie kolekcji do bazy
Forum PHP.pl > Forum > PHP > Object-oriented programming
dantekir
Mam następujące klasy:
  1. <?php
  2. /*
  3.  * To change this template, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /**
  8.  * Description of classCollectionIterator
  9.  *
  10.  * @author daniel
  11.  */
  12. class CollectionIterator implements Iterator {
  13.    private $_collection;
  14.    private $_currIndex=0;
  15.    private $_keys;
  16.  
  17.    function __construct(Collection $objCol){
  18.        $this->_collection=$objCol;
  19.        $this->_keys=$this->_collection->keys();
  20.    }
  21.  
  22.    function rewind(){
  23.        $this->_currIndex=0;
  24.    }
  25.    function valid(){
  26.        return $this->_currIndex < $this->_collection->length();
  27.    }
  28.    function key(){
  29.        return $this->_keys[$this->_currIndex];
  30.    }
  31.    function current(){
  32.        return $this->_collection->getItem($this->_keys[$this->_currIndex]);
  33.    }
  34.    function next(){
  35.        $this->_currIndex++;
  36.    }
  37.    
  38.    
  39. }
  40. ?>



  1. <?php
  2. require_once 'class.KeyInUseException.php';
  3. require_once 'class.KeyInvalidException.php';
  4. require_once 'class.CollectionIterator.php';
  5. /*
  6.  * To change this template, choose Tools | Templates
  7.  * and open the template in the editor.
  8.  */
  9.  
  10. /**
  11.  * class Collection
  12.  * uniwersalna klasa kolekcji danych
  13.  *
  14.  */
  15. class Collection implements IteratorAggregate
  16. {
  17.    /**
  18.    *
  19.    * @access private
  20.    */
  21.    private $_members = array();
  22.    private $_onload; //funkcja zwrotna
  23.    private $_isLoaded = false;//flaga określająca czy funkcja zwrotna została już wywołana
  24.  
  25.    public function addItem($obj, $key = NULL){
  26.        $this->_checkCallback();//czy zawartość kolekcji została załadowana
  27.        if($key){
  28.            if(isset($this->_members[$key])){
  29.                throw new KeyInUseException("Klucz \"$key\" jest już zajęty!");
  30.            } else {
  31.                $this->_members[$key]=$obj;
  32.            }
  33.        } else {
  34.            $this->_members[]=$obj;
  35.        }
  36.  
  37.    }
  38.  
  39.    public function removeItem($key){
  40.        $this->_checkCallback();//czy zawartość kolekcji została załadowana
  41.        if(isset($this->_members[$key])){
  42.            unset($this->_members[$key]);
  43.        } else {
  44.            throw new KeyInvalidException("Błędny klucz \"$key\"!");
  45.        }
  46.    }
  47.  
  48.    public function getItem($key){
  49.        $this->_checkCallback();//czy zawartość kolekcji została załadowana
  50.        if(isset($this->_members[$key])){
  51.            return $this->_members[$key];
  52.        } else {
  53.            throw new KeyInvalidException("Błędny klucz \"$key\"!");
  54.        }
  55.        
  56.    }
  57.  
  58.    public function keys(){
  59.        $this->_checkCallback();//czy zawartość kolekcji została załadowana
  60.        return array_keys($this->_members);
  61.    }
  62.  
  63.    public function exists($key){
  64.        $this->_checkCallback();//czy zawartość kolekcji została załadowana
  65.        return (isset($this->_members[$key]));
  66.    }
  67.  
  68.    public function length(){
  69.        $this->_checkCallback();//czy zawartość kolekcji została załadowana
  70.        return sizeof($this->_members);
  71.    }
  72.  
  73. /**
  74.  *
  75.  * @param <type> $functionName
  76.  * @param <type> $objOrClass
  77.  * @return <type>
  78.  * Ta metoda pozwala na zdefiniowanie funkcji,
  79.  * którą należy wywołać aby wypełnić kolekcję.
  80.  * Jednym słowem parametrem tej funkcji powinna być kolekcja do wypełnienia
  81.  */
  82.    public function setLoadCallback($functionName, $objOrClass = NULL){
  83.        if ($objOrClass){
  84.            $callback = array($objOrClass, $functionName);
  85.        } else {
  86.            $callback = $functionName;
  87.        }
  88.  
  89.        if (!is_callable($callback,false,$callableName)){
  90.            throw new Exception("Funkcja zwrotna $callableName nieprawidłowa!");
  91.            return false;
  92.        }
  93.        $this->_onload=$callback;
  94.    }
  95. /**
  96.  * sprawdzenie czy funkcja zwrotna została zdefiniowana
  97.  * a jeśli tak czy została już wywołana
  98.  * jeśli nie  zostaje ona wywołana
  99.  */
  100.    private function _checkCallback(){
  101.        if(isset($this->_onload) && !$this->_isLoaded){
  102.            $this->_isLoaded=true;
  103.            call_user_func($this->_onload,$this);
  104.        }
  105.    }
  106.  
  107.    public function getIterator(){
  108.        $this->_checkCallback();
  109.        return new CollectionIterator(clone $this);
  110.  
  111.    }
  112.  
  113.  
  114. }
  115. ?>



  1. <?php
  2. require_once 'class.PropertyObject.php';
  3. require_once 'class.Collection.php';
  4. require_once 'class.Telefon.php';
  5. require_once 'class.Adres.php';
  6. require_once 'class.Email.php';
  7. require_once 'class.Organizator.php';
  8. require_once 'class.Firma.php';
  9. require_once 'class.TypOsoby.php';
  10. /**
  11.  * Description of classJednostka
  12.  *
  13.  * @author daniel
  14.  */
  15. abstract class Jednostka extends PropertyObject
  16. {
  17.    public $telephones = array();
  18.    public $emails = array();
  19.    public $adresses = array();
  20.    //public $typjednostka;
  21.    //public $nazwa;
  22.    //public $idjednostka;
  23.   // public $nip;
  24.  
  25.    public function __construct($arData) {
  26.        if(!$arData){
  27.            parent::__construct(null);
  28.            $this->telephones= new Collection();
  29.            $this->emails= new Collection();
  30.            $this->adresses= new Collection();
  31.        } else {
  32.            $arData2=DataManager::getData($arData['idjednostka'],'jednostka');
  33.            $arData=array_merge($arData,$arData2);
  34.            parent::__construct($arData);
  35.            $this->telephones= new Collection();
  36.            $this->telephones->setLoadCallback('_loadTelephones',$this);
  37.            $this->emails= new Collection();
  38.            $this->emails->setLoadCallback('_loadEmails',$this);
  39.            $this->adresses= new Collection();
  40.            $this->adresses->setLoadCallback('_loadAdresses',$this);
  41.  
  42.        }
  43.            $this->propertyTable['nazwa']='name';
  44.            $this->propertyTable['idjednostka']='idjednostka';
  45.            $this->propertyTable['nip']='nip';
  46.    }
  47.    public function _loadTelephones(Collection $col) {
  48.        $this->telephones=DataManager::getColectionObjectsForEntity($this->idjednostka, $col, 'telefon');
  49.    } // end of member function
  50.    public function _loadEmails(Collection $col) {
  51.        $this->emails=DataManager::getColectionObjectsForEntity($this->idjednostka, $col, 'email');
  52.    } // end of member function
  53.    public function _loadAdresses(Collection $col) {
  54.        $this->adresses=DataManager::getColectionObjectsForEntity($this->idjednostka, $col, 'adres');
  55.    } // end of member function
  56.    public function addAdres(Adres $adresses){
  57.        $this->adresses->addItem($adresses);
  58.    }
  59.    public function addEmail(Email $email){
  60.        $this->emails->addItem($email);
  61.    }
  62.    public function addTelefon(Telefon $telefon){
  63.        $this->telephones->addItem($telefon);
  64.    }
  65.    public function save(){
  66.        $newData = array();
  67.        $newData['name']=$this->nazwa;
  68.        $newData['nip']=$this->nip;
  69.        $this->idjednostka=DataManager::saveNewField('jednostka',$newData);
  70.    }
  71.    public function update(){
  72.        $newData = array();
  73.        $newData['name']=$this->nazwa;
  74.        $newData['nip']=$this->nip;
  75.        DataManager::saveField('jednostka',$newData,$this->idjednostka);
  76.    }
  77.    public function del(){
  78.        //print ('ooooooooo'.$this->idjednostka);
  79.        DataManager::delField($this->idjednostka,'jednostka');
  80.      
  81.    }
  82. }
  83. ?>


  1. <?php
  2. /*
  3.  * To change this template, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. require_once 'class.PropertyObject.php';
  7. /**
  8.  * Description of classAddress
  9.  *
  10.  * @author daniel
  11.  */
  12. class Adres extends PropertyObject
  13. {
  14.  
  15.    //public $idadres;
  16.    public function __construct($id=null) {
  17.        if(!$id){
  18.        }else{
  19.            $arData=DataManager::getData($id,'adres');
  20.            parent::__construct($arData);
  21.  
  22.        }
  23.            $this->propertyTable['miejscowosc']='miejscowosc';
  24.            $this->propertyTable['nrlokalu']='nrlokalu';
  25.            $this->propertyTable['nrdomu']='nrdomu';
  26.            $this->propertyTable['kod']='kod';
  27.            $this->propertyTable['ulica']='ulica';
  28.            $this->propertyTable['kraj']='kraj';
  29.            $this->propertyTable['idadres']='idadres';
  30.            $this->propertyTable['idjednostka']='idjednostka';
  31.    }
  32.    public function  __toString() {
  33.        return (string)($this->ulica." ".$this->nrdomu." ".$this->nrlokalu." ".$this->miejscowosc." ".$this->kod." ".$this->kraj." ");
  34.    }
  35.    public function save($idjednostka){
  36.        $newData = array();
  37.        $newData['miejscowosc']=$this->miejscowosc;
  38.        $newData['nrlokalu']=$this->nrlokalu;
  39.        $newData['nrdomu']=$this->nrdomu;
  40.        $newData['kod']=$this->kod;
  41.        $newData['ulica']=$this->ulica;
  42.        $newData['kraj']=$this->kraj;
  43.        $newData['idjednostka']=$idjednostka;
  44.        $this->idadres=DataManager::saveNewField('adres',$newData);
  45.    }
  46.    public function update(){
  47.        $newData = array();
  48.        $newData['miejscowosc']=$this->miejscowosc;
  49.        $newData['nrlokalu']=$this->nrlokalu;
  50.        $newData['nrdomu']=$this->nrdomu;
  51.        $newData['kod']=$this->kod;
  52.        $newData['ulica']=$this->ulica;
  53.        $newData['kraj']=$this->kraj;
  54.        $newData['idjednostka']=$idjednostka;
  55.        DataManager::saveField('adres',$newData,$this->idadres);
  56.    }
  57. }
  58. ?>



Chciałbym w klasie Jednostka funkcji save() zapisać wszystkie elementy kolekcji np. Addresses
Nie wiem jak to ugryźć...
erix
A słyszałeś o interfejsie Serializable? ;]
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.