Skip to content

Commit

Permalink
1.0.0 and README improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Mar 12, 2021
1 parent 760bd4a commit 4a01016
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Parable PHP ORM

## 1.0.0

Version 1.0.0 is here!

_Changes_
- Upgraded `parable-php/di` and `parable-php/query` to 1.0.0 as well.
- `Database` now accepts constructor values for quicker setup. See `Database::__construct()` or check the README.
- `README` updated to show how to actually connect to a database :facepalm:

## 0.11.1

- Upgrade `parable-php/query` to 0.5.0.
Expand Down
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,56 @@ $repository->deleteDeferred(); // Actually perform it.

For both deferred saves and deletes, the currently stored entities to be saved or deleted can be cleared by calling either `clearDeferredSaves()` or `clearDeferredDeletes()`.

#### How to connect to a database

You didn't think I'd ever forget this, do you? 2 database types are currently supported, MySQL and Sqlite3.

To connect to a MySQL server:

```php
$database = new Database(
Database::TYPE_MYSQL,
'parable',
'localhost',
'username',
'password'
);
```

Or:

```php
$database = new Database();
$database->setDatabaseName('parable');
$database->setHost('localhost');
$database->setUsername('username');
$database->setPassword('password');

$database->connect();

$results = $database->query('SELECT * FROM users');
```

And to connect to a Sqlite3 file:

```php
$database = new Database(
Database::TYPE_SQLITE,
'storage/parable.db'
);
```

Or:

```php
$database = new Database();
$database->setDatabaseName('storage/parable.db');

$database->connect();

$results = $database->query('SELECT * FROM users');
```

## Contributing

Any suggestions, bug reports or general feedback is welcome. Use github issues and pull requests, or find me over at [devvoh.com](https://devvoh.com).
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"php": ">=8.0",
"ext-pdo": "*",
"ext-json": "*",
"parable-php/di": "^0.3.0",
"parable-php/query": "^0.5.0"
"parable-php/di": "^1.0",
"parable-php/query": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
Expand Down
22 changes: 12 additions & 10 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,29 @@ class Database
public const DATE_SQL = 'Y-m-d';
public const TIME_SQL = 'H:i:s';
public const DATETIME_SQL = self::DATE_SQL . ' ' . self::TIME_SQL;

public const TYPE_MYSQL = 0;
public const TYPE_SQLITE = 1;

protected ?int $type = null;
protected ?string $host = null;
protected int $port = 3306;
protected ?string $username = null;
protected ?string $password = null;
protected ?string $databaseName = null;
protected ?string $charSet = null;
protected int $errorMode = PDO::ERRMODE_SILENT;
protected ?PDO $connection = null;
protected int $queryCount = 0;
protected ?string $lastQuery;

/**
* The connection class MUST extend PDO if not PDO itself
* The connection class MUST extend PDO or a class that extends PDO
*/
protected string $connectionClass = PDO::class;

public function __construct(
protected ?int $type = null,
protected ?string $databaseName = null,
protected ?string $host = null,
protected ?string $username = null,
protected ?string $password = null,
protected int $port = 3306,
protected ?string $charSet = null,
protected int $errorMode = PDO::ERRMODE_SILENT
) {}

public function setConnectionClass(string $connectionClass): void
{
if (!is_subclass_of($connectionClass, PDO::class)) {
Expand Down

0 comments on commit 4a01016

Please sign in to comment.