Skip to content

Commit

Permalink
Version 2.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marty Wallace committed Jan 16, 2017
1 parent b8201f3 commit 4697696
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "martywallace/simpledb",
"version": "1.0.0",
"version": "2.0.0",
"description": "A thin wrapper around PDO for common MySQL operations.",
"homepage": "https://github.com/MartyWallace/simpledb",
"type": "library",
Expand Down
17 changes: 10 additions & 7 deletions samples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use SimpleDb\Database;
use SimpleDb\Field;
use SimpleDb\Model;
use SimpleDb\HasOneRelation;
use SimpleDb\SingleRelation;
use SimpleDb\Row;
use SimpleDb\Relation;
use SimpleDb\HasManyRelation;
use SimpleDb\MultipleRelation;
use SimpleDb\Query;

/**
Expand Down Expand Up @@ -41,14 +41,17 @@ protected function fields() {

protected function relations() {
return [
'parent' => new HasOneRelation(User::class, 'parentId'),
'children' => new HasManyRelation(User::class, 'parentId')
'parent' => new SingleRelation(User::class, 'parentId'),
'children' => new MultipleRelation(User::class, 'parentId')
];
}
}

$db = new Database('root@localhost/test');
$db = new Database('root@localhost/test', true);

$user = $db->table('users')->one(['id' => 2])->populate(User::class);
$user = $db->table('users')->find(1)->populate(User::class);

var_dump($user->children);
print_r($user->parent);
print_r($db->table('users')->count());

print_r($db->prepared);
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?php namespace SimpleDb;

use Exception;

/**
* A relationship of many external records.
*
* @package SimpleDb
* @author Marty Wallace
*/
class HasManyRelation extends Relation {
class MultipleRelation extends Relation {

/** @var string */
private $_foreign;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?php namespace SimpleDb;

use Exception;

/**
* A relationship of one external record.
*
* @package SimpleDb
* @author Marty Wallace
*/
class HasOneRelation extends Relation {
class SingleRelation extends Relation {

/** @var string */
private $_local;
Expand Down Expand Up @@ -36,11 +34,9 @@ public function __construct($model, $local, $foreign = 'id') {
* @param Model $model The model who the related data is attached to.
*
* @return Model
*
* @throws Exception If the table name of the foreign model could not be determined.
*/
public function fetch(Model $model) {
$row = $this->getForeignTable()->one([$this->_foreign => $model->getFieldValue($this->_local)]);
$row = $this->getForeignTable()->oneWhere([$this->_foreign => $model->getFieldValue($this->_local)]);
return empty($row) ? null : $row->populate($this->model);
}

Expand Down
19 changes: 18 additions & 1 deletion src/SimpleDb/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,31 @@ public function getNonUniqueColumns() {
return array_values(array_filter($this->getColumns(), function(Column $column) { return !$column->isUnique(); }));
}

/**
* Find a record using its primary key.
*
* @param string|string[]|int|int[] $primary The primary key. An array can be provided for multiple keys.
*
* @return Row
*/
public function find($primary) {
if (!is_array($primary)) {
$primary = [$primary];
}

return Database::get()->one(Query::select($this->_name)->where(array_map(function(Column $column) {
return $column->name;
}, $this->getPrimaryColumns()))->limit(1), $primary);
}

/**
* Return one row from this table using search criteria.
*
* @param array $criteria An array of fields mapped to values to search for.
*
* @return Row
*/
public function one(array $criteria) {
public function oneWhere(array $criteria) {
return Database::get()->one(Query::select($this->_name)->where(array_keys($criteria))->limit(1), array_values($criteria));
}

Expand Down
6 changes: 3 additions & 3 deletions tests/SimpleDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testInsert(Database $db) {

$db->table('users')->insert(['name' => 'John', 'email' => $email]);

$this->assertEquals($db->table('users')->one(['email' => $email])->name, 'John');
$this->assertEquals($db->table('users')->oneWhere(['email' => $email])->name, 'John');

return $email;
}
Expand All @@ -49,7 +49,7 @@ public function testInsert(Database $db) {
* @depends testInsert
*/
public function testOne(Database $db, $email) {
$record = $db->table('users')->one(['email' => $email]);
$record = $db->table('users')->oneWhere(['email' => $email]);

$this->assertTrue($record instanceof Row);
}
Expand All @@ -70,7 +70,7 @@ public function testAll(Database $db) {
public function testDelete(Database $db, $email) {
$db->table('users')->delete(['email' => $email]);

$this->assertNull($db->table('users')->one(['email' => $email]));
$this->assertNull($db->table('users')->oneWhere(['email' => $email]));
}

public function testBuildSelectQuery() {
Expand Down

0 comments on commit 4697696

Please sign in to comment.