Skip to content

Commit

Permalink
Rework "fromRequest" from Table
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 17, 2024
1 parent ee90ceb commit cd567b2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ use Rougin\Datatables\Table;
$request = new Request($_GET);
// -----------------------------------------

// Parse columns based on the Request ---
$table = Table::fromRequest($request);
// --------------------------------------
// Parse columns based on the Request ---------
$table = Table::fromRequest($request, 'users');
// --------------------------------------------
```

By default, getting columns from the payload of the Javascript part of `DataTables` does not provide its name (e.g., `forename`, `surname`, etc.). As the column name is required for getting its data from a source, there is a need to map its column to the database table:
Expand All @@ -70,8 +70,6 @@ By default, getting columns from the payload of the Javascript part of `DataTabl

// ...

$table->setName('users');

// The first column will be named as "forename" ---
$table->mapColumn(0, 'forename');
// ------------------------------------------------
Expand Down Expand Up @@ -126,7 +124,7 @@ $query = new Query($request, $source);
$result = $query->getResult($table);
```

The `getResult` from the `Query` class will return as the `Result` class in which returns the response as an array or as JSON format:
The `getResult` from the `Query` class returns a `Result` class in which returns the response as an array or as JSON format:

``` php
// index.php
Expand Down Expand Up @@ -165,7 +163,9 @@ $ php index.php
"London",
"2009-10-09",
"1200000.0"
]
],

// ...
]
}
```
Expand Down
11 changes: 8 additions & 3 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class Table
protected $table;

/**
* @param string $name
* @param \Rougin\Datatables\Request $request
* @param string $name
*
* @return self
*/
public static function fromRequest($name, Request $request)
public static function fromRequest(Request $request, $name = null)
{
$table = new Table;

Expand All @@ -41,7 +41,12 @@ public static function fromRequest($name, Request $request)
$table->addColumn($column);
}

return $table->setName($name);
if ($name)
{
$table->setName($name);
}

return $table;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/Source/PdoSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function test_column_names()

$request = Params::columnNames();

$table = Table::fromRequest('users', $request);
$table = Table::fromRequest($request, 'users');

$query = new Query($request, $this->source);

Expand Down Expand Up @@ -76,7 +76,7 @@ public function test_few_columns()

$request = Params::fewColumns();

$table = Table::fromRequest('users', $request);
$table = Table::fromRequest($request, 'users');

$query = new Query($request, $this->source);

Expand Down Expand Up @@ -104,7 +104,7 @@ public function test_global_and_column_search()

$request = Params::globalAndColumnSearch();

$table = $this->setTable('users', $request);
$table = $this->setTable($request, 'users');

$query = new Query($request, $this->source);

Expand Down Expand Up @@ -132,7 +132,7 @@ public function test_global_search()

$request = Params::globalSearch();

$table = $this->setTable('users', $request);
$table = $this->setTable($request, 'users');

$query = new Query($request, $this->source);

Expand Down Expand Up @@ -160,7 +160,7 @@ public function test_initial_load()

$request = Params::initialData();

$table = $this->setTable('users', $request);
$table = $this->setTable($request, 'users');

$query = new Query($request, $this->source);

Expand Down Expand Up @@ -188,7 +188,7 @@ public function test_search_column()

$request = Params::searchColumn();

$table = $this->setTable('users', $request);
$table = $this->setTable($request, 'users');

$query = new Query($request, $this->source);

Expand Down

0 comments on commit cd567b2

Please sign in to comment.