Scalr\Model\AbstractEntity::db PHP Méthode

db() public méthode

Gets database instance associated with the entity
public db ( ) : ADODB_mysqli
Résultat ADODB_mysqli Database connection instance
    public function db()
    {
        if ($this->db === null) {
            $class = get_class($this);
            if (!isset(self::$cache[$class]['entity'])) {
                $this->_parseClassDocComment();
            }
            $service = self::$cache[$class]['entity']->table->service;
            //Validates service
            if (!in_array($service, array('adodb', 'cadb'))) {
                throw new ModelException(sprintf("Service %s is not allowed", $service));
            }
            $this->db = \Scalr::getContainer()->{$service};
        }
        return $this->db;
    }

Usage Example

Exemple #1
0
 /**
  * Creates partitions
  */
 public function create()
 {
     $db = $this->entity->db();
     $dt = new \DateTime('tomorrow');
     $end = new \DateTime('+1 month');
     $interval = new \DateInterval("P1D");
     $patritionSet = '';
     while ($dt <= $end) {
         $patritionSet .= "PARTITION p" . $dt->format('Ymd') . " VALUES LESS THAN (UNIX_TIMESTAMP('" . $dt->format('Y-m-d') . " 00:00:00')),";
         $dt->add($interval);
     }
     $this->_disableChecks();
     try {
         $this->entity->db()->Execute("\n                ALTER TABLE " . $this->entity->table() . "\n                PARTITION BY RANGE(UNIX_TIMESTAMP(" . $this->field->getColumnName() . ")) (" . rtrim($patritionSet, ',') . ")\n            ");
     } catch (\Exception $e) {
         $this->_enableChecks();
         throw $e;
     }
     $this->_enableChecks();
 }