diff --git a/composer.json b/composer.json index da52983..0e5a30b 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/samples/index.php b/samples/index.php index 89f3968..71d6810 100644 --- a/samples/index.php +++ b/samples/index.php @@ -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; /** @@ -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); \ No newline at end of file +print_r($user->parent); +print_r($db->table('users')->count()); + +print_r($db->prepared); \ No newline at end of file diff --git a/src/SimpleDb/HasManyRelation.php b/src/SimpleDb/MultipleRelation.php similarity index 94% rename from src/SimpleDb/HasManyRelation.php rename to src/SimpleDb/MultipleRelation.php index cce33de..51ef9a9 100644 --- a/src/SimpleDb/HasManyRelation.php +++ b/src/SimpleDb/MultipleRelation.php @@ -1,14 +1,12 @@ 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); } diff --git a/src/SimpleDb/Table.php b/src/SimpleDb/Table.php index 39af900..6c882ae 100644 --- a/src/SimpleDb/Table.php +++ b/src/SimpleDb/Table.php @@ -111,6 +111,23 @@ 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. * @@ -118,7 +135,7 @@ public function getNonUniqueColumns() { * * @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)); } diff --git a/tests/SimpleDbTest.php b/tests/SimpleDbTest.php index 595f450..ce81fcd 100644 --- a/tests/SimpleDbTest.php +++ b/tests/SimpleDbTest.php @@ -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; } @@ -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); } @@ -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() {