Actions

Difference between revisions of "JTable/move"

From Joomla! Documentation

(New page: ===Syntax=== ''void'' move ($dirn, [$where = '']) where: {| class="wikitable" !Argument !Data type !Description !Default |- |$dirn |integer |Direction to move the record. $dirn < 0 fo...)
 
Line 52: Line 52:
 
|17
 
|17
 
|}
 
|}
We now want to compress the ordering of the table.
+
We now want to move down the entry 'Hello' one step.
 
<source lang="php">
 
<source lang="php">
 
//We need an instance of the table object. In a controller or model we can do it like this...
 
//We need an instance of the table object. In a controller or model we can do it like this...
 
$table = $this->getTable('greeting');
 
$table = $this->getTable('greeting');
//Let's compact the ordering sequence
+
//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->reorder();
+
$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);
 
</source>
 
</source>
The first thing the method does is to load the table ordered by the current ordering:
+
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.
 
{| class="wikitable"
 
{| class="wikitable"
 
!id
 
!id
Line 65: Line 68:
 
!ordering
 
!ordering
 
|-
 
|-
|3
+
|4
|Guten Tag
+
|Buenos Días
|3
+
|10
 +
|}
 +
The next (and last step) is that the method switches the ordering values of both records.
 +
'''Before:'''
 +
{| class="wikitable"
 +
!id
 +
!greeting
 +
!ordering
 
|-
 
|-
 
|1
 
|1
Line 73: Line 83:
 
|5
 
|5
 
|-
 
|-
|2
+
|4
|Bonjour
+
|Buenos Días
|15
+
|10
 
|}
 
|}
The next (and last step) is that the method updates the ordering entries so it's a consecutive sequence:
+
'''After:'''
 
{| class="wikitable"
 
{| class="wikitable"
 
!id
 
!id
Line 83: Line 93:
 
!ordering
 
!ordering
 
|-
 
|-
|3
 
|Guten Tag
 
 
|1
 
|1
 +
|Hello
 +
|10
 +
|-
 +
|4
 +
|Buenos Días
 +
|5
 +
|}
 +
 +
In the end the table looks like this:
 +
 +
{| class="wikitable"
 +
!id
 +
!greeting
 +
!ordering
 
|-
 
|-
 
|1
 
|1
 
|Hello
 
|Hello
|2
+
|10
 
|-
 
|-
 
|2
 
|2
 
|Bonjour
 
|Bonjour
 +
|15
 +
|-
 
|3
 
|3
 +
|Guten Tag
 +
|3
 +
|-
 +
|4
 +
|Buenos Días
 +
|5
 +
|-
 +
|5
 +
|Nǐ hǎo
 +
|17
 
|}
 
|}
  
 
===See also===
 
===See also===
* [[JTable/move]]
+
* [[JTable/reorder]]
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JTable]]</noinclude>
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JTable]]</noinclude>

Revision as of 03:19, 7 November 2008

Contents

Syntax

void move ($dirn, [$where = ])

where:

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

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

See also