JTable/move
| Line 3: | Line 3: | ||
''void'' move ($dirn, [$where = '']) | ''void'' move ($dirn, [$where = '']) | ||
| − | + | === Parameters === | |
{| class="wikitable" | {| class="wikitable" | ||
!Argument | !Argument | ||
| Line 20: | Line 20: | ||
| | | | ||
|} | |} | ||
| + | |||
| + | === Returns === | ||
| + | nothing | ||
=== Preconditions === | === Preconditions === | ||
Latest revision as of 12:51, 11 November 2008
| This is a article which: needs review. You can help the Joomla! Documentation Wiki by contributing to it. More pages that need help similar to this one are here. If you feel the need is satistified, please remove this notice. While actively editing, consider adding {{inuse}} to reduce edit conflicts. |
Contents |
[edit] Syntax
void move ($dirn, [$where = ])
[edit] 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 |
[edit] Returns
nothing
[edit] Preconditions
Your database table must have a column named 'ordering'.
[edit] 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.
[edit] 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 |