JTable/move
From Joomla! Documentation
< JTable
| This recently added article requires a review |
Contents |
Syntax
void move ($dirn, [$where = ])
Parameters
| Argument | Data type | Description | Default |
|---|---|---|---|
| $dirn | integer | Direction to move the record. $dirn < 0 for up, $dirn > 0 for down. | |
| $where | string | Additional where query to limit ordering to a particular subset of records |
Returns
nothing
Preconditions
Your database table must have a column named 'ordering'.
What does the method do?
The goal of the method is to move a record of the table one step up, or one step down by switching the ordering values with the respective neighbour.
Example
Let's say we have the following database-table:
| id | greeting | ordering |
|---|---|---|
| 1 | Hello | 5 |
| 2 | Bonjour | 15 |
| 3 | Guten Tag | 3 |
| 4 | Buenos Días | 10 |
| 5 | Nǐ hǎo | 17 |
We now want to move down the entry 'Hello' one step.
//We need an instance of the table object. In a controller or model we can do it like this... $table = $this->getTable('greeting'); //We need to load the 'Hello' entry into the table object. The id (which is the primary key of the table) of this entry is 1. $table->load(1); //We now want to move the 'Hello' entry one step down. That means, the $dirn Parameter must be positive. //We choose the value 1, but it could also be any other positive number $table->move(1);
The first thing the method does is to load the record which has the next higher ordering value. (If our $dirn paremeter would have been negative, it would have loaded the record with the next lower ordering value. In our case, this is the record with the id #4.
| id | greeting | ordering |
|---|---|---|
| 4 | Buenos Días | 10 |
The next (and last step) is that the method switches the ordering values of both records.
Before:
| id | greeting | ordering |
|---|---|---|
| 1 | Hello | 5 |
| 4 | Buenos Días | 10 |
After:
| id | greeting | ordering |
|---|---|---|
| 1 | Hello | 10 |
| 4 | Buenos Días | 5 |
In the end the table looks like this:
| id | greeting | ordering |
|---|---|---|
| 1 | Hello | 10 |
| 2 | Bonjour | 15 |
| 3 | Guten Tag | 3 |
| 4 | Buenos Días | 5 |
| 5 | Nǐ hǎo | 17 |
