Edson Horacio Junior
Edson Horacio Junior

Reputation: 3143

ZF2 Select Statement: Change columns order with joined tables

I have a select statement like this:

$select1 = $sql->select('table1');
$select1->columns('col1', 'col2', 'col3');

$select1->join('table2', 'table1.id = table2.id', array('col4', 'col5'));

$select1->join('table3', 'table2.id2 = table3.id2', array('col6', 'col7'));

My select statement shows columns in this order: col1, col2, col3, col4, col5, col6, col7.

I want to show columns in any other order, for example: col7, col6, col1, col2, col3, col4, col5.

How can I do that without changing my FROM statement and the order of the joins? I need something like specify the columns in just one $select1->columns() so I can set any columns order I want.

Upvotes: 1

Views: 366

Answers (1)

Kunal Dethe
Kunal Dethe

Reputation: 1274

As I am not able to currently test this today, so just try this out for now -

$select1 = $sql->select('table1');
$select1->columns(array('col1', 'col2', 'col3'));
$select1->join('table2', 'table1.id = table2.id', array('col4', 'col5'));
$select1->join('table3', 'table2.id2 = table3.id2', array('col6', 'col7'));

Creating wrapper upon the above select statement.

$select_main = $sql->select();
$select_main->from(array('temp_table' => $select1));
$select_main->columns(array('col7', 'col6', 'col1', 'col2', 'col3', 'col4', 'col5'));

Now execute the $select_main and check the result.

This is just an idea that by creating a wrapper select to the result can help in changing the positions of the columns.

Will test the above code soon and let you know if any changes are required.

Good Luck!!!.

Upvotes: 2

Related Questions