Example #1
0
 /**
  * Delete entity.
  *
  * @param $id
  * @return int deleted rows
  */
 public function remove($id)
 {
     return $this->connection->delete($this->tableName(), $this->identifier() . ' = :id', ['id' => $id]);
 }
Example #2
0
 /**
  * Migrate sql files into a database.
  *
  * @param $path, directory with sql files
  * @param array $ignore, filename to ignore
  * @return array of sql queries
  */
 public static function migrate(AbstractConnection $conn, $path, array $ignore = [])
 {
     $result = [];
     if (file_exists($path) && is_dir($path)) {
         $files = scandir($path);
         if ($files !== false) {
             $queries = [];
             foreach ($files as $file) {
                 $fileinfo = pathinfo($path . DIRECTORY_SEPARATOR . $file);
                 if ($fileinfo['extension'] === 'sql' && !in_array($fileinfo['filename'], $ignore)) {
                     $sql = file_get_contents($path . DIRECTORY_SEPARATOR . $file);
                     $queries = array_merge($queries, explode(';', $sql));
                 }
             }
             if (!empty($queries)) {
                 foreach ($queries as $q) {
                     $query = trim($q);
                     if (!empty($query)) {
                         $conn->query($query . ';');
                         $result[] = $query;
                     }
                 }
             }
         }
     }
     return $result;
 }