Exemple #1
0
 public function __construct($config_file = NULL)
 {
     if ($config_file == NULL) {
         $config_file = APP_DIR . '/.env';
     }
     $this->getenv_func = 'getenv';
     if (function_exists('apache_getenv')) {
         $this->getenv_func = 'apache_getenv';
     }
     if (!file_exists($config_file)) {
         Logger::warning('Unable to find the configuration file (only the global environment variable will be available)!');
         return;
     }
     $config = file_get_contents($config_file);
     $lines = explode(PHP_EOL, $config);
     foreach ($lines as $env) {
         $env = trim($env);
         if (!empty($env)) {
             if (function_exists('apache_setenv')) {
                 $raw = explode('=', $env);
                 if (count($raw) == 2) {
                     apache_setenv($raw[0], $raw[1]);
                 } else {
                     Logger::warning('Invalid environment definition: ' . $env);
                 }
                 continue;
             }
             putenv($env);
         }
     }
     unset($lines);
     unset($config);
 }
Exemple #2
0
 /**
  * Fetch the object in the database with the specific fields.
  * If NULL is set, the object will be retrieve with all the
  * fields without thoses corresponding to the primary keys.
  */
 public function fetch(...$fields)
 {
     if ($this->getId() != NULL) {
         Logger::warning('Trying to fetch an object already fetched!');
         return $this;
     }
     $result = self::all();
     if (!empty($fields)) {
         foreach ($fields as $f) {
             $result->filter($f, '=', $this->getRawValue($f));
         }
     } else {
         $pks = $this->parameters()['primary_key'];
         if (is_string($pks)) {
             $pks = array($pks => $pks);
         }
         foreach ($this->_structure as $f) {
             if ($f instanceof PrimaryField) {
                 continue;
             }
             if (isset($pks[$f])) {
                 continue;
             }
             $result->filter($f, '=', $this->getRawValue($f));
         }
     }
     $count = count($result);
     if ($count > 1) {
         throw new \Exception('Too many results!');
     }
     if ($count == 0) {
         Logger::notice('Object not fetched in the database!');
         return NULL;
     }
     $obj = $result->current();
     if (!$obj instanceof Models) {
         throw new \Exception('Internal Error: The QuerySet doesn\'t return an object of instance Models! ' . print_r($obj, true));
     }
     foreach ($this->_structure as $f_name => $field) {
         $f = $obj->getField($f_name);
         // Reset the field
         unset($this->_values['old'][$f_name]);
         // Set the value from the database
         if ($f->isReady()) {
             $this->_values['old'][$f_name] = $obj->getRawValue($f_name);
         }
     }
     return $this;
 }