Skip to content

Commit

Permalink
Merge pull request #2805 from Laravel-Backpack/fix-2797
Browse files Browse the repository at this point in the history
fixes #2797 - CrudColumn constructor did not guess missing attributes
  • Loading branch information
tabacitu committed May 12, 2020
2 parents e4b7271 + 5c2b1f9 commit b8bb45a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
3 changes: 3 additions & 0 deletions src/app/Library/CrudPanel/CrudColumn.php
Expand Up @@ -32,6 +32,9 @@ public function __construct($name)
$this->setAttributeValue('name', $name);
}

// guess all attributes that weren't explicitly defined
$this->attributes = $this->crud()->makeSureColumnHasNeededAttributes($this->attributes);

return $this->save();
}

Expand Down
37 changes: 37 additions & 0 deletions src/app/Library/CrudPanel/Traits/Columns.php
Expand Up @@ -350,6 +350,43 @@ public function firstColumnWhere($attribute, $value)
});
}

/**
* The only REALLY MANDATORY attribute for a column is the 'name'.
* Everything else, Backpack can probably guess.
*
* This method checks that all necessary attributes are set.
* If not, it tries to guess them.
*
* @param string|array $column The column definition array OR column name as string.
* @return array Proper column definition array.
*/
public function makeSureColumnHasNeededAttributes($column)
{
$column = $this->makeSureColumnHasName($column);
$column = $this->makeSureColumnHasLabel($column);
$column = $this->makeSureColumnHasType($column);
$column = $this->makeSureColumnHasKey($column);
$column = $this->makeSureColumnHasModel($column);

// check if the column exists in the database (as a db column)
$columnExistsInDb = $this->hasDatabaseColumn($this->model->getTable(), $column['name']);

// make sure column has tableColumn, orderable and searchLogic
$column['tableColumn'] = $column['tableColumn'] ?? $columnExistsInDb;
$column['orderable'] = $column['orderable'] ?? $columnExistsInDb;
$column['searchLogic'] = $column['searchLogic'] ?? $columnExistsInDb;

// check if it's a method on the model,
// that means it's a relationship
if (! $columnExistsInDb && method_exists($this->model, $column['name'])) {
$relatedModel = $this->model->{$column['name']}()->getRelated();
$column['entity'] = $column['name'];
$column['model'] = get_class($relatedModel);
}

return $column;
}

/**
* Create and return a CrudColumn object for that column name.
*
Expand Down
37 changes: 0 additions & 37 deletions src/app/Library/CrudPanel/Traits/ColumnsProtectedMethods.php
Expand Up @@ -7,43 +7,6 @@

trait ColumnsProtectedMethods
{
/**
* The only REALLY MANDATORY attribute for a column is the 'name'.
* Everything else, Backpack can probably guess.
*
* This method checks that all necessary attributes are set.
* If not, it tries to guess them.
*
* @param string|array $column The column definition array OR column name as string.
* @return array Proper column definition array.
*/
protected function makeSureColumnHasNeededAttributes($column)
{
$column = $this->makeSureColumnHasName($column);
$column = $this->makeSureColumnHasLabel($column);
$column = $this->makeSureColumnHasType($column);
$column = $this->makeSureColumnHasKey($column);
$column = $this->makeSureColumnHasModel($column);

// check if the column exists in the database (as a db column)
$columnExistsInDb = $this->hasDatabaseColumn($this->model->getTable(), $column['name']);

// make sure column has tableColumn, orderable and searchLogic
$column['tableColumn'] = $column['tableColumn'] ?? $columnExistsInDb;
$column['orderable'] = $column['orderable'] ?? $columnExistsInDb;
$column['searchLogic'] = $column['searchLogic'] ?? $columnExistsInDb;

// check if it's a method on the model,
// that means it's a relationship
if (! $columnExistsInDb && method_exists($this->model, $column['name'])) {
$relatedModel = $this->model->{$column['name']}()->getRelated();
$column['entity'] = $column['name'];
$column['model'] = get_class($relatedModel);
}

return $column;
}

/**
* Add a column to the current operation, using the Setting API.
*
Expand Down

0 comments on commit b8bb45a

Please sign in to comment.