Skip to content

Commit

Permalink
Save parent declare schema class in declare column property
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jun 18, 2016
1 parent 4fcf784 commit 61efd23
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/Schema/Column/AutoIncrementPrimaryKeyColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace LazyRecord\Schema\Column;

use LazyRecord\Schema\DeclareColumn;
use LazyRecord\Schema\DeclareSchema;

class AutoIncrementPrimaryKeyColumn extends DeclareColumn
{
public function __construct($name = 'id', $type = 'integer')
public function __construct(DeclareSchema $schema, $name = 'id', $type = 'integer')
{
parent::__construct($name);
parent::__construct($schema, $name);
$this->type($type)
->isa('int')
->notNull()
Expand Down
15 changes: 12 additions & 3 deletions src/Schema/DeclareColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ class DeclareColumn extends Column implements ColumnAccessorInterface, IteratorA
*/
protected $attributes = array();


/**
* the parent schema object
*/
protected $schema;

/**
* @var string column name (id)
*/
public function __construct($name = null, $type = null)
public function __construct(DeclareSchema $schema = null, $name = null, $type = null)
{
$this->attributeTypes = $this->attributeTypes + array(
/* primary key */
Expand Down Expand Up @@ -169,9 +175,12 @@ public function renderAs($renderAs, array $widgetAttributes = array())
*
* @param string $relationship relationship id
*/
public function refer($relationship)
public function refer($schemaClass)
{
$this->attributes['refer'] = $relationship;
$this->attributes['refer'] = $schemaClass;
if (class_exists($schemaClass, true)) {
$schema = new $schemaClass;
}
return $this;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Schema/DeclareSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function insertColumn(DeclareColumn $column)
*/
protected function insertAutoIdColumn($name = 'id', $columnType = 'integer')
{
$column = new AutoIncrementPrimaryKeyColumn($name, $columnType);
$column = new AutoIncrementPrimaryKeyColumn($this, $name, $columnType);
$this->primaryKey = $column->name;
$this->insertColumn($column);
return $column;
Expand Down Expand Up @@ -541,8 +541,7 @@ public function column($name, $class = 'LazyRecord\\Schema\\DeclareColumn')
throw new Exception("column $name of ".get_class($this).' is already defined.');
}
$this->columnNames[] = $name;

return $this->columns[ $name ] = new $class($name);
return $this->columns[$name] = new $class($this, $name);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/AuthorBooks/Model/AuthorBookSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public function schema()
->required()
->integer()
->unsigned()
->refer('AuthorBooks\\Model\\AuthorSchema');
;

$this->column('book_id')
Expand Down
8 changes: 5 additions & 3 deletions tests/LazyRecord/Schema/SchemaDeclare/ColumnTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php
use LazyRecord\Schema\DeclareSchema;
use LazyRecord\Schema\DeclareColumn;

class ColumnTest extends PHPUnit_Framework_TestCase
{
function test()
public function test()
{
$column = new LazyRecord\Schema\DeclareColumn('foo');
$column = new DeclareColumn(new DeclareSchema, 'foo');
$column->primary()
->integer()
->autoIncrement()
->notNull();

$this->assertEquals('foo',$column->name);
$this->assertTrue($column->primary);
$this->assertEquals('int',$column->type);
Expand Down

0 comments on commit 61efd23

Please sign in to comment.