Skip to content

Commit

Permalink
Eniity table name generated as eloquent, entity_table will be removed…
Browse files Browse the repository at this point in the history
… in 1.0.0 sunel#24
  • Loading branch information
sunel committed Dec 11, 2018
1 parent ef6128c commit a12335f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public function getBackendTable()
if ($this->dataTable === null) {
$backendTable = trim($this->getAttribute('backend_table'));
if (empty($backendTable)) {
$backendTable = $this->entity()->entityTableName().'_'.strtolower($this->getAttribute('backend_type'));
$backendTable = $this->entity()->code().'_'.strtolower($this->getAttribute('backend_type'));
}
$this->dataTable = $backendTable;
}
Expand Down
6 changes: 1 addition & 5 deletions src/Attribute/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,8 @@ public function tableName()
$attribute = $this->attribute();
if ($attribute->isStatic()) {
$this->table = $attribute->entityType()->entityTableName();
} elseif ($attribute->backendTable()) {
$this->table = $this->attribute()->backendTable();
} else {
$entity = $attribute->entity();
$tableName = sprintf('%s_%s', $entity->entityTableName(), $this->type());
$this->table = $tableName;
$this->table = $this->type();
}
}

Expand Down
30 changes: 6 additions & 24 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,6 @@ public function setEntityKey(string $key)
return $this;
}

/**
* @alias getEntityCustomTable()
*/
public function entityCustomTable()
{
return $this->getEntityCustomTable();
}

/**
* Get the table name for the Entity.
*
* @return string
*/
public function getEntityCustomTable()
{
return $this->entityCustomTable;
}

/**
* Set the table name for the Entity.
*
Expand Down Expand Up @@ -178,13 +160,13 @@ public function entityTableName()
*/
public function getEntityTableName()
{
$tableName = $this->getAttribute('entity_code');

$tablePrefix = $this->getConnection()->getTablePrefix();
if ($tablePrefix != '') {
$tableName = "$tablePrefix.$tableName";
if(!is_null($this->entityCustomTable)) {
return $this->entityCustomTable;
}
return $tableName;

return str_replace(
'\\', '', Str::snake(Str::plural(class_basename($this->entity_class)))
);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Flat/Entity/Complier.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function __construct(Entity $entity, Filesystem $files, Command $console)

public function compile()
{
$this->console->info("\t Creating flat table for `{$this->entity->entity_table}`.");
$this->console->info("\t Creating flat table for `{$this->entity->code()}`.");

$path = $this->getPath($this->entity->entity_table.'_flat');
$path = $this->getPath($this->entity->entityTableName().'_flat');

$this->console->info("\t in {$path}");

Expand All @@ -44,7 +44,7 @@ public function compile()

$this->files->requireOnce($path);

$this->console->info("\t Migrating `{$this->entity->entity_table}` flat schema.");
$this->console->info("\t Migrating `{$this->entity->code()}` flat schema.");

$this->runUp($path);
}
Expand Down Expand Up @@ -84,7 +84,7 @@ protected function resolve($file)

protected function buildSchema()
{
$table = $this->describe($this->entity->entity_table)->map(function ($attribute) {
$table = $this->describe($this->entity->entityTableName())->map(function ($attribute) {
if ($attribute['COLUMN_KEY'] == 'PRI') {
$schema = "{$attribute['COLUMN_NAME']}:{$this->getColumn($attribute['DATA_TYPE'])}:unsigned";
} else {
Expand Down Expand Up @@ -122,15 +122,15 @@ protected function buildSchema()
$tableCache = Collection::make([]);

$this->collectAttributes()->chunk(500, function ($chunk, $page) use ($attributes, $tableCache) {
$chunk->map(function ($attribute) use ($attributes, $tableCache) {
$chunk->map(function ($attribute) use ($attributes, $tableCache) {
$table = $tableCache->get($attribute->backendTable(), function () use ($attribute, $tableCache) {
$key = $attribute->backendTable();
return $tableCache->put($key, $this->describe($key, function ($query) {
return $query->where('COLUMN_NAME', 'value');
})->first())->get($key);
});

$schema = "{$attribute->getAttributeCode()}";
$schema = $attribute->getAttributeCode();

$backendTable = $attribute->getBackendType();

Expand Down Expand Up @@ -236,7 +236,7 @@ protected function compileMigrationStub()
*/
protected function replaceClassName(&$stub)
{
$className = ucwords(camel_case($this->entity->entity_table.'_flat'));
$className = ucwords(camel_case($this->entity->entityTableName().'_flat'));
$stub = str_replace('{{class}}', $className, $stub);
return $this;
}
Expand All @@ -248,7 +248,7 @@ protected function replaceClassName(&$stub)
*/
protected function replaceTableName(&$stub)
{
$table = $this->entity->entity_table.'_flat';
$table = $this->entity->entityTableName().'_flat';
$stub = str_replace('{{table}}', $table, $stub);
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function it_must_have_a_table()
$sku = $this->addSku();

$this->assertEquals(
$this->entity->entityTableName().'_string',
$this->entity->code().'_string',
$sku->backendTable()
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function it_can_detect_entity_table_name()

$entity = $eloquent->baseEntity();

$this->assertEquals($entity->entityCustomTable(), $eloquent->getTable());
$this->assertEquals($entity->entityTableName(), $eloquent->getTable());
}

/** @test */
Expand Down

0 comments on commit a12335f

Please sign in to comment.