示例#1
0
 function flush()
 {
     foreach ($this->toDelete as $e) {
         $table = @static::getClassAnnotation(get_class($e))['ORM_Table'];
         if ($table && strlen($table) > 0) {
             $primaryColumn = static::findPrimaryColumn(get_class($e));
             $primaryProp = static::findPrimaryProperty(get_class($e));
             $primaryProp = new \ReflectionProperty(get_class($e), $primaryProp);
             $primaryProp->setAccessible(true);
             $primaryValue = $primaryProp->getValue($e);
             $primaryProp->setAccessible(false);
             unset($primaryProp);
             $this->connection->delete($table, [$primaryColumn => $primaryValue]);
         }
     }
     $this->toDelete = array();
     /**
      * @var $e Model
      */
     foreach ($this->entities as $e) {
         if ($e->getExists()) {
             $class = get_class($e);
             $table = @static::getClassAnnotation($class)['ORM_Table'];
             if ($table && strlen($table) > 0) {
                 $primaryColumn = static::findPrimaryColumn($class);
                 $primaryProp = static::findPrimaryProperty($class);
                 $primaryProp = new \ReflectionProperty($class, $primaryProp);
                 $primaryProp->setAccessible(true);
                 $primaryValue = $primaryProp->getValue($e);
                 $primaryProp->setAccessible(false);
                 $fields = [];
                 $meta = static::getPropertiesMetadata($class);
                 foreach ($meta as $property => $data) {
                     if ($property == $primaryProp->getName()) {
                         continue;
                     }
                     $p = new \ReflectionProperty($class, $property);
                     $p->setAccessible(true);
                     if (isset($data['ORM_Column'])) {
                         if (!isset($data['ORM_Type'])) {
                             throw new \Exception('ORM Type is not set.');
                         }
                         if ($data['ORM_Type'] == 'numeric') {
                             $fields[$data['ORM_Column']] = $p->getValue($e);
                             if (strlen($fields[$data['ORM_Column']]) <= 0) {
                                 $fields[$data['ORM_Column']] = 'NULL';
                             }
                         } elseif ($data['ORM_Type'] == 'string') {
                             if (is_null($p->getValue($e))) {
                                 $fields[$data['ORM_Column']] = 'NULL';
                             } else {
                                 $val = $this->connection->escape($p->getValue($e));
                                 $fields[$data['ORM_Column']] = '\'' . $val . '\'';
                             }
                         } elseif ($data['ORM_Type'] == 'bool') {
                             if (is_null($p->getValue($e))) {
                                 $fields[$data['ORM_Column']] = 'NULL';
                             } else {
                                 $fields[$data['ORM_Column']] = $p->getValue($e) ? '1' : '0';
                             }
                         } elseif ($data['ORM_Type'] == 'date') {
                             if (is_null($p->getValue($e))) {
                                 $fields[$data['ORM_Column']] = 'NULL';
                             } else {
                                 $val = $p->getValue($e);
                                 if ($val instanceof \DateTime) {
                                     $val = $val->format('Y-m-d');
                                 }
                                 $fields[$data['ORM_Column']] = '\'' . $val . '\'';
                             }
                         } else {
                             throw new \Exception('Wrong ORM Type: "' . $data['ORM_Type'] . '"');
                         }
                     } elseif (isset($data['ORM_ManyToOne'])) {
                         $relData = static::getRelationData($class, $p->getName(), 'ORM_ManyToOne');
                         $targetClass = 'Src\\Models\\' . $relData['targetClass'];
                         $mappedByColumn = $relData['mappedBy'];
                         $targetPrimaryName = static::findPrimaryProperty($targetClass);
                         $targetPrimaryProperty = new \ReflectionProperty($targetClass, $targetPrimaryName);
                         $targetPrimaryProperty->setAccessible(true);
                         $obj = $p->getValue($e);
                         if ($obj instanceof RelationModel) {
                             $obj = $obj->get();
                         }
                         $targetPrimaryValue = $targetPrimaryProperty->getValue($obj);
                         $fields[$mappedByColumn] = $targetPrimaryValue;
                     } elseif (isset($data['ORM_OneToMany'])) {
                         // TODO: if cascade
                     } else {
                         throw new \Exception('ORM annotation is not found.');
                     }
                     unset($p);
                 }
                 $this->connection->update($fields, $table, [$primaryColumn => $primaryValue]);
                 unset($primaryProp);
                 unset($meta);
             }
         } else {
             // TODO: Добавление всех новых одним запросом
             $class = get_class($e);
             $table = @static::getClassAnnotation($class)['ORM_Table'];
             if ($table && strlen($table) > 0) {
                 $fields = [];
                 $values = [];
                 $values[0] = [];
                 $meta = static::getPropertiesMetadata($class);
                 $primaryProp = static::findPrimaryProperty($class);
                 foreach ($meta as $property => $data) {
                     if ($property == $primaryProp) {
                         continue;
                     }
                     $p = new \ReflectionProperty($class, $property);
                     $p->setAccessible(true);
                     if (isset($data['ORM_Column'])) {
                         if (!isset($data['ORM_Type'])) {
                             throw new \Exception('ORM Type is not set.');
                         }
                         if ($data['ORM_Type'] == 'numeric') {
                             $fields[] = $data['ORM_Column'];
                             $val = $p->getValue($e);
                             $values[0][] = strlen($val) > 0 ? $val : 'NULL';
                         } elseif ($data['ORM_Type'] == 'string') {
                             if (is_null($p->getValue($e))) {
                                 $fields[] = $data['ORM_Column'];
                                 $values[0][] = 'NULL';
                             } else {
                                 $val = $this->connection->escape($p->getValue($e));
                                 $fields[] = $data['ORM_Column'];
                                 $values[0][] = '\'' . $val . '\'';
                             }
                         } elseif ($data['ORM_Type'] == 'bool') {
                             $fields[] = $data['ORM_Column'];
                             //var_dump($p->getValue($e));
                             if (is_null($p->getValue($e))) {
                                 $values[0][] = 'NULL';
                             } else {
                                 $values[0][] = $p->getValue($e) ? '1' : '0';
                             }
                         } elseif ($data['ORM_Type'] == 'date') {
                             $fields[] = $data['ORM_Column'];
                             if (is_null($p->getValue($e))) {
                                 $values[0][] = 'NULL';
                             } else {
                                 $val = $p->getValue($e);
                                 if ($val instanceof \DateTime) {
                                     $val = $val->format('Y-m-d');
                                 }
                                 $values[0][] = '\'' . $val . '\'';
                             }
                         } else {
                             throw new \Exception('Wrong ORM Type: "' . $data['ORM_Type'] . '"');
                         }
                     } elseif (isset($data['ORM_ManyToOne'])) {
                         $relData = static::getRelationData($class, $p->getName(), 'ORM_ManyToOne');
                         $targetClass = 'Src\\Models\\' . $relData['targetClass'];
                         $mappedByColumn = $relData['mappedBy'];
                         $targetPrimaryName = static::findPrimaryProperty($targetClass);
                         $targetPrimaryProperty = new \ReflectionProperty($targetClass, $targetPrimaryName);
                         $targetPrimaryProperty->setAccessible(true);
                         $obj = $p->getValue($e);
                         if ($obj instanceof RelationModel) {
                             $obj = $obj->get();
                         }
                         $targetPrimaryValue = $targetPrimaryProperty->getValue($obj);
                         $fields[] = $mappedByColumn;
                         $values[0][] = $targetPrimaryValue;
                     } elseif (isset($data['ORM_OneToMany'])) {
                         // TODO: if cascade
                     } else {
                         throw new \Exception('ORM annotation is not found.');
                     }
                     unset($p);
                 }
                 /*var_dump('--------------');
                   var_dump($table);
                   var_dump($fields);
                   var_dump($values);*/
                 $this->connection->insert($table, $fields, $values);
                 $primaryName = static::findPrimaryProperty($class);
                 $primaryProperty = new \ReflectionProperty($class, $primaryName);
                 $primaryProperty->setAccessible(true);
                 $primaryValue = $this->connection->query('SELECT LAST_INSERT_ID() as :last_id;');
                 $primaryValue = $primaryValue[0]['last_id'];
                 $primaryProperty->setValue($e, $primaryValue);
                 unset($meta);
                 unset($primaryProperty);
             }
         }
     }
     $this->entities = [];
 }
示例#2
0
 /**
  * @throws \Exception
  * @return Model[]|int
  */
 public function query()
 {
     switch ($this->queryType) {
         case QueryBuilder::SELECT:
             $this->class = 'Src\\Models\\' . $this->class;
             $meta = static::getClassAnnotation($this->class);
             if (!isset($meta['ORM_Entity'])) {
                 throw new \Exception('Class "' . $this->class . '" is not mapped with ORM.');
             }
             $table = $meta['ORM_Table'];
             $rawResult = $this->conn->select(['*'], [$table], $this->whereClause, $this->post);
             $models = [];
             foreach ($rawResult as $raw) {
                 $models[] = $this->hydrateData($raw, $this->class, $this->conn, $this->app);
             }
             return $models;
             break;
         case QueryBuilder::UPDATE:
             /**
              * @var $model Model
              */
             $affected = 0;
             foreach ($this->modelsToUpdate as $model) {
                 if (!$model->getExists()) {
                     continue;
                 }
                 $class = get_class($model);
                 $table = @static::getClassAnnotation($class)['ORM_Table'];
                 if ($table && strlen($table) > 0) {
                     $primaryColumn = static::findPrimaryColumn($class);
                     $primaryProp = static::findPrimaryProperty($class);
                     $primaryProp = new \ReflectionProperty($class, $primaryProp);
                     $primaryProp->setAccessible(true);
                     $primaryValue = $primaryProp->getValue($model);
                     $primaryProp->setAccessible(false);
                     $fields = [];
                     $meta = static::getPropertiesMetadata($class);
                     foreach ($meta as $property => $data) {
                         if ($property == $primaryProp->getName()) {
                             continue;
                         }
                         $p = new \ReflectionProperty($class, $property);
                         $p->setAccessible(true);
                         if (isset($data['ORM_Column'])) {
                             if (!isset($data['ORM_Type'])) {
                                 throw new \Exception('ORM Type is not set.');
                             }
                             if ($data['ORM_Type'] == 'numeric') {
                                 $fields[$data['ORM_Column']] = $p->getValue($model);
                             } elseif ($data['ORM_Type'] == 'string') {
                                 $fields[$data['ORM_Column']] = '\'' . $p->getValue($model) . '\'';
                             } else {
                                 throw new \Exception('Wrong ORM Type: "' . $data['ORM_Type'] . '"');
                             }
                         } elseif (isset($data['ORM_ManyToOne'])) {
                             $relData = static::getRelationData($class, $p->getName(), 'ORM_ManyToOne');
                             $targetClass = $relData['targetClass'];
                             $mappedByColumn = $relData['mappedBy'];
                             $targetPrimaryName = static::findPrimaryProperty($targetClass);
                             $targetPrimaryProperty = new \ReflectionProperty($targetClass, $targetPrimaryName);
                             $targetPrimaryProperty->setAccessible(true);
                             $targetPrimaryValue = $targetPrimaryProperty->getValue($p->getValue($model));
                             $fields[$mappedByColumn] = $targetPrimaryValue;
                         } elseif (isset($data['ORM_OneToMany'])) {
                             // TODO: if cascade
                         } else {
                             throw new \Exception('ORM annotation is not found.');
                         }
                         unset($p);
                     }
                     $affected += $this->conn->update($fields, $table, [$primaryColumn => $primaryValue]);
                     unset($primaryProp);
                     unset($meta);
                 }
             }
             return $affected;
             break;
         case QueryBuilder::INSERT:
             /**
              * @var $model Model
              */
             $affected = 0;
             foreach ($this->modelsToInsert as $model) {
                 //if ($model->getExists()) continue;
                 // TODO: Добавление всех новых одним запросом
                 $class = get_class($model);
                 $table = @static::getClassAnnotation($class)['ORM_Table'];
                 if ($table && strlen($table) > 0) {
                     $fields = [];
                     $values = [];
                     $values[0] = [];
                     $meta = static::getPropertiesMetadata($class);
                     $primaryProp = static::findPrimaryProperty($class);
                     foreach ($meta as $property => $data) {
                         if ($property == $primaryProp) {
                             continue;
                         }
                         $p = new \ReflectionProperty($class, $property);
                         $p->setAccessible(true);
                         if (isset($data['ORM_Column'])) {
                             if (!isset($data['ORM_Type'])) {
                                 throw new \Exception('ORM Type is not set.');
                             }
                             if ($data['ORM_Type'] == 'numeric') {
                                 $fields[] = $data['ORM_Column'];
                                 $values[0][] = $p->getValue($model);
                             } elseif ($data['ORM_Type'] == 'string') {
                                 $fields[] = $data['ORM_Column'];
                                 $values[0][] = '\'' . $p->getValue($model) . '\'';
                             } else {
                                 throw new \Exception('Wrong ORM Type: "' . $data['ORM_Type'] . '"');
                             }
                         } elseif (isset($data['ORM_ManyToOne'])) {
                             $relData = static::getRelationData($class, $p->getName(), 'ORM_ManyToOne');
                             $targetClass = 'Src\\Models\\' . $relData['targetClass'];
                             $mappedByColumn = $relData['mappedBy'];
                             $targetPrimaryName = static::findPrimaryProperty($targetClass);
                             $targetPrimaryProperty = new \ReflectionProperty($targetClass, $targetPrimaryName);
                             $targetPrimaryProperty->setAccessible(true);
                             $targetPrimaryValue = $targetPrimaryProperty->getValue($p->getValue($model));
                             $fields[] = $mappedByColumn;
                             $values[0][] = $targetPrimaryValue;
                         } elseif (isset($data['ORM_OneToMany'])) {
                             // TODO: if cascade
                         } else {
                             throw new \Exception('ORM annotation is not found.');
                         }
                         unset($p);
                     }
                     $affected += $this->conn->insert($table, $fields, $values);
                     unset($meta);
                 }
             }
             return $affected;
             break;
         case QueryBuilder::DELETE:
             $affected = 0;
             foreach ($this->modelsToDelete as $model) {
                 if (!$model->getExists()) {
                     continue;
                 }
                 $c = get_class($model);
                 $table = @static::getClassAnnotation($c)['ORM_Table'];
                 if ($table && strlen($table) > 0) {
                     $primaryColumn = static::findPrimaryColumn($c);
                     $primaryProp = static::findPrimaryProperty($c);
                     $primaryProp = new \ReflectionProperty($c, $primaryProp);
                     $primaryProp->setAccessible(true);
                     $primaryValue = $primaryProp->getValue($model);
                     $primaryProp->setAccessible(false);
                     unset($primaryProp);
                     $affected += $this->conn->delete($table, [$primaryColumn => $primaryValue]);
                 }
             }
             return $affected;
             break;
         default:
             throw new \Exception('No query type specified.');
     }
 }
<?php

include_once "classes/DbConnection.class.php";
$db = new DbConnection();
if (isset($_GET['c_id'])) {
    $comment_id = $_GET['c_id'];
    $delete_comment_sql = "DELETE FROM blog_comment\n\tWHERE comment_id={$comment_id}";
    //delete the specific comment
    if ($db->delete($delete_comment_sql)) {
        header("location: admin.php");
    }
}
示例#4
0
<?php

include_once "classes/DbConnection.class.php";
$db = new DbConnection();
if (isset($_GET['p_id'])) {
    $post_id = $_GET['p_id'];
    $delete_post_sql = "DELETE FROM blog_post WHERE post_id={$post_id}";
    /*
    	$any_comment_sql="SELECT * FROM blog_comment 
    				WHERE fk_post_id=$post_id";
    	if(is_array($db->getRows($any_comment_sql))){
    $delete_comment_sql="DELETE FROM blog_comment 
    						WHERE fk_post_id=$post_id";
    //delete the comment to a specific post
    $db->delete($delete_comment_sql);
    }
    */
    //delete the specific post
    if ($db->delete($delete_post_sql)) {
        header("location:admin.php");
    }
}