Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
PHP - Sviluppo ORM
Forum - PHP - Sviluppo ORM

Avatar
TheDarkJuster (Member)
Guru^2


Messaggi: 1620
Iscritto: 27/09/2013

Segnala al moderatore
Postato alle 20:01
Venerdì, 24/07/2015
Sto sviluppando una funzione relativa al mio ORM che permette di immagazzinare un oggetto  in un database RDBMS. La funzione in questione è la seguente:

Codice sorgente - presumibilmente Php

  1. public function Store($object) {
  2.         //check if the given data is an object
  3.         if (!is_object($object))
  4.             throw new DatabaseException("Only objects can be stored through the Store function", -10);
  5.        
  6.         //check if the connection can be used
  7.         if ($this->connectionHandler != NULL)
  8.         {
  9.             //the serialized data
  10.             $serialized = array();
  11.            
  12.             //reflect the class
  13.             $reflectedObject = new ReflectionObject($object);
  14.            
  15.             //get object properties
  16.             $objectProperties = $reflectedObject->getProperties(ReflectionProperty::IS_STATIC | ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
  17.            
  18.             //prepare to cycle each property
  19.             reset($objectProperties);
  20.            
  21.             //get the number of properties
  22.             $propertiesNumber = count($objectProperties);
  23.            
  24.             //cycle each property
  25.             for ($counter = 0; $counter < $propertiesNumber; $counter++) {
  26.                 //get the currently processed property
  27.                 $currentProperty = current($objectProperties);
  28.  
  29.                 //get the name and the value of the current property
  30.                 $propertyName = $currentProperty->getName();
  31.                 $propertyValue = $currentProperty->getValue();
  32.                 $propertySerializedValue = NULL;
  33.                
  34.                 //check for the $propertyValue type (cannot be a resource or an object)
  35.                 $propertyType = gettype($propertyValue);
  36.                 if (($propertyType == "resource") || ($propertyType == "object")) {
  37.                     throw new DatabaseException("An object or a resource inside an object cannot be serialized", -12);
  38.                 } else if ($propertyType == "array") {
  39.                     //resources, objects and arrays cannot be nested inside an array
  40.                 } else if ($propertyType != "NULL") {
  41.                     $propertySerializedValue = $propertyValue;
  42.                 }
  43.                
  44.                 //serialization completed, store the result
  45.                 if ($propertyType != "NULL")
  46.                     $serialized[$propertyName] = $propertySerializedValue;
  47.                
  48.                 //get the next property to process
  49.                 next($objectProperties);
  50.             }
  51.         } else {
  52.             throw new DatabaseException("The Store operation cannot be executed because the database connection is closed", -11);
  53.         }
  54.     }



Ora.... ho delle domande: come faccio a immagazzinare un oggetto che è in una proprietà dell'oggetto passato alla funzione? E' una mancanza grave non implementare l'immagazzinamento di un oggetto che è un valore di una proprietà dell'oggetto passato alla funzione?

L'approccio che sto utilizzando è corretto? Può essere più veloce?

La scelta di usare json_encode() per serializzare un array è la migliore, o ha svantaggi che non ho considerato?

Grazie per le eventuali risposte.

Ultima modifica effettuata da TheDarkJuster il 25/07/2015 alle 0:39
PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 0:27
Domenica, 26/07/2015
La serializzazione di oggetti è sempre un risultato "imperfetto"; dal momento che non è pratico salvare lo stato in memoria di qualsiasi oggetto, dipende dai tuoi obiettivi quanto è "grave".

Se stai scrivendo un ORM generico, normalmente mi aspetterei la possibilità di serializzare oggetti che contengono altri oggetti.


Il mio blog: https://piero.dev
PM Quote
Avatar
TheDarkJuster (Member)
Guru^2


Messaggi: 1620
Iscritto: 27/09/2013

Segnala al moderatore
Postato alle 12:11
Domenica, 26/07/2015
Ok.... E come potrei fare per "legare" due oggetti?

PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 16:17
Domenica, 26/07/2015
Chiama store ricorsivamente, salvando un riferimento al tipo di oggetto che viene serializzato.


Il mio blog: https://piero.dev
PM Quote
Avatar
TheDarkJuster (Member)
Guru^2


Messaggi: 1620
Iscritto: 27/09/2013

Segnala al moderatore
Postato alle 17:25
Domenica, 26/07/2015
Ok, grazie!

PM Quote