Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
feat: Working on the new version
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmcarvalho committed Jun 24, 2020
1 parent 91c3116 commit 423cbb5
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 10 deletions.
24 changes: 24 additions & 0 deletions docs/tables/config-bundle/columns.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ the index to put the column in a specific position. Example of usage::
needed, so, the query will be build automatically according to your configuration. **BelongsTo** and **HasMany**
associations will follow the same logical from CakePHP ou result entity.

Adding a custom database column
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. php:method:: addCustomDatabaseColumn(FunctionExpression $functionExpression, string $asName, ?int $index = null)
With this method, you will able of do some custom sql finds like `CONCAT`, `SUM` and others. You must provide a
`FunctionExpression` instance that is the result of one of many methods that you can find in `FunctionsBuilder` that you
can get in `$configBundle->Columns->func()` method. Example of usage::

/**
* @param \DataTables\Table\ConfigBundle $configBundle
*/
public function config(ConfigBundle $configBundle): void
{
$func = $configBundle->Columns->func()->concat(['id' => 'identifier', ' created in: ', 'created' => 'identifier']);
$configBundle->Columns->addCustomDatabaseColumn($func, 'custom_field');

}

.. note::
When you use this method with joined tables you will need to join it manually with the `$configBundle->Query` object.
With this `\DataTables\Table\QueryBaseState` object, you will can join the new table with a contain method or others
joins methods.

Adding non database column
^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion docs/tables/datatables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DataTables

.. php:class:: DataTables()
Classes inherited from **DataTables** is the classes that has two methods very important that is
Classes inherited from **DataTables** are the classes that has two methods very important that is
called to apply the application business rules to a :doc:`ConfigBundle </tables/config-bundle>` from a table. They
are saved on ``src/DataTables/`` folder and are postfixed with `DataTables`, so, Categories DataTables class will be
named `CategoriesDataTables`. You can easily :doc:`bake </tables/bake>` this class using the CakePHP bake shell.
Expand Down
28 changes: 23 additions & 5 deletions src/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace DataTables\Table;

use Cake\Database\Expression\FunctionExpression;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Cake\Utility\Text;
Expand Down Expand Up @@ -109,24 +110,34 @@ final class Column {
*/
private $_associationPath = '';

/**
* @var \Cake\Database\Expression\FunctionExpression|null
*/
private $_functionExpression = null;

/**
* Column constructor.
*
* @param string $name
* @param bool $database
* @param array $columnSchema
* @param string $associationPath
*/
public function __construct(string $name, bool $database = true, array $columnSchema = [], string $associationPath = '') {
$title = Inflector::humanize($name);
if ($database === true) {
$title = Inflector::humanize(explode('.', $name)[1]);
* @param \Cake\Database\Expression\FunctionExpression|null $functionExpression
*/
public function __construct(string $name, bool $database = true, array $columnSchema = [], string $associationPath = '', FunctionExpression $functionExpression = null) {
$title = explode('.', $name);
if (count($title) === 2) {
$title = $title[1];
} else {
$title = $title[0];
}
$title = Inflector::humanize($title);
$this->_config = Hash::insert($this->_config, 'name', $name);
$this->_config = Hash::insert($this->_config, 'title', $title);
$this->_database = $database;
$this->_columnSchema = $columnSchema;
$this->_associationPath = $associationPath;
$this->_functionExpression = $functionExpression;
if ($database === true && !empty($columnSchema['type']) && !empty(static::DATA_TABLES_TYPE_MAP[$columnSchema['type']])) {
$this->setType(static::DATA_TABLES_TYPE_MAP[$columnSchema['type']]);
}
Expand All @@ -139,6 +150,13 @@ public function getAssociationPath(): string {
return $this->_associationPath;
}

/**
* @return \Cake\Database\Expression\FunctionExpression
*/
public function getFunctionExpression(): ?FunctionExpression {
return $this->_functionExpression;
}

/**
* @param bool $onlyDirty
* @param bool $isDefaultColumn
Expand Down
34 changes: 34 additions & 0 deletions src/Table/Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace DataTables\Table;

use Cake\Database\Expression\FunctionExpression;
use Cake\Database\FunctionsBuilder;
use Cake\Error\FatalErrorException;
use Cake\Utility\Inflector;
use DataTables\Tools\Functions;
Expand Down Expand Up @@ -40,6 +42,11 @@ final class Columns {
*/
public $Default;

/**
* @var \Cake\Database\FunctionsBuilder|null
*/
private $_functionsBuilder = null;

/**
* Columns constructor.
*
Expand Down Expand Up @@ -70,6 +77,20 @@ public function addDatabaseColumn(string $dataBaseField, ?int $index = null): Co
return $this->saveColumn($column, $index);
}

/**
* Add a custom database column to DataTables table.
* This method don't will autoload others tables, so, if you want use with a joined table, you must add it before.
*
* @param \Cake\Database\Expression\FunctionExpression $functionExpression
* @param string $asName
* @param int|null $index Position to insert new column.
* @return \DataTables\Table\Column
*/
public function addCustomDatabaseColumn(FunctionExpression $functionExpression, string $asName, ?int $index = null): Column {
$column = new Column($asName, true, [], '', $functionExpression);
return $this->saveColumn($column, $index);
}

/**
* Add a non database column to DataTables table.
*
Expand All @@ -86,6 +107,19 @@ public function addNonDatabaseColumn(string $label, ?int $index = null): Column
return $this->saveColumn($column, $index);
}

/**
* Get the query FunctionBuilder instance.
*
* @return \Cake\Database\FunctionsBuilder
*/
public function func(): FunctionsBuilder {
if ($this->_functionsBuilder === null) {
$this->_functionsBuilder = new FunctionsBuilder();
}

return $this->_functionsBuilder;
}

/**
* Save the column on array.
*
Expand Down
3 changes: 1 addition & 2 deletions src/Table/Option/MainOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function setColumns(Columns $columns): self {
if ($column->isDatabase() === false) {
$column->setSearchable(false);
$column->setOrderable(false);
} else {
} elseif (empty($column->getFunctionExpression())) {
$association = Functions::getInstance()->getAssociationUsingPath($columns->getConfigBundle()->getDataTables()->getOrmTable(), $column->getAssociationPath());
if ($association instanceof HasMany) {
$column->setSearchable(false);
Expand Down Expand Up @@ -329,7 +329,6 @@ public function getConfigAsArray(?bool $printAllOptions = null): array {
$localResourcesConfig->setLoadPluginSelect(true);
}
}

$this->processUrlQuery();
$url = Hash::get($this->_config, 'ajax.url');
$url = "$url/" . md5(Router::url());
Expand Down
6 changes: 5 additions & 1 deletion src/Table/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ private function getFormattedColumn(string $columnName, Column $column, EntityIn
$value = h($value);
}
} else {
$value = __d('data_tables', 'NOT CONFIGURED YET');
if (!empty($column->getFunctionExpression()) && !empty($entity->{$column->getName()})) {
$value = $entity->{$column->getName()};
} else {
$value = __d('data_tables', 'NOT CONFIGURED YET');
}
}
$pattern = '%s';
if (Configure::read('DataTables.columnAutoGeneratedWarning', true)) {
Expand Down
4 changes: 3 additions & 1 deletion src/Table/TableDataUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ public function getFind(): Query {
$table = $this->_configBundle->getDataTables()->getOrmTable();
$query = $this->_configBundle->Query->mergeWithQuery($table->find());
foreach ($columns->getColumns() as $columnName => $column) {
if ($column->isDatabase() && !Functions::getInstance()->getAssociationUsingPath($table, $column->getAssociationPath()) instanceof HasMany) {
if ($column->isDatabase() && empty($column->getFunctionExpression()) && !Functions::getInstance()->getAssociationUsingPath($table, $column->getAssociationPath()) instanceof HasMany) {
$select[] = $columnName;
} elseif (!empty($column->getFunctionExpression())) {
$select[$columnName] = $column->getFunctionExpression();
}
$associationPaths = substr_replace($column->getAssociationPath(), '', 0, strlen($table->getAlias()) + 1);
if (!empty($associationPaths)) {
Expand Down

0 comments on commit 423cbb5

Please sign in to comment.