Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/Phactory/Sql/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function __construct($name, $defaults, $associations = array(), Phactory
}
}

public function setTablePrimaryKey($primary_key) {
$this->_table->setPrimaryKey($primary_key);
}

public function setDefaults($defaults) {
$this->_defaults = $defaults;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Phactory/Sql/Phactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ public function getConnection() {
* @param array $defaults key => value pairs of column => value, or a phactory_blueprint
* @param array $associations array of phactory_associations
*/
public function define($blueprint_name, $defaults = array(), $associations = array()) {
public function define($blueprint_name, $defaults = array(), $associations = array(), $primary_key = null) {
if($defaults instanceof Blueprint) {
$blueprint = $defaults;
} else {
$blueprint = new Blueprint($blueprint_name, $defaults, $associations, $this);
}
$blueprint->setTablePrimaryKey($primary_key);
$this->_blueprints[$blueprint_name] = $blueprint;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/Phactory/Sql/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Table {
protected $_singular;
protected $_name;
protected $_db_util;
protected $_primary_key;
protected $_phactory;

public function __construct($singular_name, $pluralize = true, Phactory $phactory) {
Expand All @@ -19,6 +20,10 @@ public function __construct($singular_name, $pluralize = true, Phactory $phactor
}
}

public function setPrimaryKey($primary_key){
return $this->_primary_key = $primary_key;
}

public function getName() {
return $this->_name;
}
Expand All @@ -28,6 +33,9 @@ public function getSingularName() {
}

public function getPrimaryKey() {
if($this->_primary_key){
return $this->_primary_key;
}
return $this->_db_util->getPrimaryKey($this->_name);
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Phactory/Sql/TableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Phactory\Sql;

class TableTest extends \PHPUnit_Framework_TestCase
{
protected $pdo;
protected $phactory;

protected function setUp()
{
$this->pdo = new \PDO("sqlite:test.db");
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

$this->pdo->exec("CREATE TABLE `users` ( id INTEGER PRIMARY KEY, name TEXT )");
$this->pdo->exec("CREATE TABLE `settings` ( id INTEGER, name TEXT )");

$this->phactory = new Phactory($this->pdo);
}

protected function tearDown()
{
$this->pdo->exec("DROP TABLE `settings`");
$this->pdo->exec("DROP TABLE `users`");
$this->phactory->reset();
}

public function testGetPrimaryKeyWhenPrimaryKeyExists()
{
$table = new Table('user', true, $this->phactory);
$this->assertEquals('id', $table->getPrimaryKey());
}

public function testGetPrimaryKeyWhenPrimaryKeyRegistered()
{
$table = new Table('setting', true, $this->phactory);
$table->setPrimaryKey('id');
$this->assertEquals('id', $table->getPrimaryKey());
}


}
?>