Skip to content

Commit

Permalink
Apply primary key column from referred schema
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jun 18, 2016
1 parent 61efd23 commit 079f5ee
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
27 changes: 25 additions & 2 deletions src/Schema/DeclareColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,27 @@ public function renderAs($renderAs, array $widgetAttributes = array())
{
$this->renderAs = $renderAs;
$this->widgetAttributes = $widgetAttributes;

return $this;
}

/**
* Use referenece from existing relationship.
*
* Once the column is refered,
* the attribute will be changed, unless user override the attribute after
* this call.
*
* @param string $relationship relationship id
*/
public function refer($schemaClass)
{
$this->attributes['refer'] = $schemaClass;
if (class_exists($schemaClass, true)) {
// get the primary key from the refered schema
if (get_class($this->schema) !== ltrim($schemaClass,'\\') && class_exists($schemaClass, true)) {
$schema = new $schemaClass;
if ($primaryKey = $schema->findPrimaryKeyColumn()) {
$this->applyType($primaryKey);
}
}
return $this;
}
Expand Down Expand Up @@ -317,6 +324,22 @@ public function asRuntimeColumn()
return new RuntimeColumn($this->name, $this->attributes);
}


/**
* Apply column type on a column object for setting foreign key.
*
* @return DeclareColumn
*/
public function applyType(DeclareColumn $column)
{
$column->type = $this->type;
$column->notNull = $this->notNull;
$column->unsigned = $this->unsigned;
return $column;
}



/**
* Export column attributes to an array.
*
Expand Down
20 changes: 2 additions & 18 deletions src/Schema/DeclareSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected function build(array $options = array())
if (false === $this->primaryKey) {
if ($config = ConfigLoader::getInstance()) {
if ($config->hasAutoId() && !isset($this->columns['id'])) {
$this->insertAutoIdColumn();
$this->insertAutoIdPrimaryColumn();
}
}
}
Expand Down Expand Up @@ -196,30 +196,14 @@ public function insertColumn(DeclareColumn $column)
* @param string $name default to 'id'
* @param string $columnType 'int', 'smallint', 'bigint' ...
*/
protected function insertAutoIdColumn($name = 'id', $columnType = 'integer')
protected function insertAutoIdPrimaryColumn($name = 'id', $columnType = 'integer')
{
$column = new AutoIncrementPrimaryKeyColumn($this, $name, $columnType);
$this->primaryKey = $column->name;
$this->insertColumn($column);
return $column;
}

/**
* Apply primary key type on a column object.
*
* @return DeclareColumn
*/
public function applyPrimaryKeyType(DeclareColumn $column)
{
if (!$this->primaryKey || !isset($this->columns[$this->primaryKey])) {
throw new SchemaRelatedException($this, "primary key column doesn't exist on schema.");
}
$pkColumn = $this->columns[$this->primaryKey];
$column->type = $pkColumn->type;
$column->notNull = $pkColumn->notNull;
$column->unsigned = $pkColumn->unsigned;
return $column;
}


/**
Expand Down
14 changes: 8 additions & 6 deletions tests/AuthorBooks/Model/AuthorBookSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ class AuthorBookSchema extends Schema
public function schema()
{
$this->column('author_id')
->required()
->integer()
->unsigned()
->refer('AuthorBooks\\Model\\AuthorSchema');
;
->required()
->integer()
->unsigned()
->refer('AuthorBooks\\Model\\AuthorSchema')
;

$this->column('book_id')
->integer()
->unsigned()
->required();
->required()
->refer('AuthorBooks\\Model\\BookSchema')
;

$this->column('created_on')
->isa('str')
Expand Down

0 comments on commit 079f5ee

Please sign in to comment.