DB.PHP - PHP ORM

Query order

In previous chapters we talked about query initialization and simle usage. Now let us list step by step query object features.

Example class:

public product
{
    public $name;
    public $type;
    public $color;
}

Query can be ordered many ways but two common behaviors needs to be remembered as single field order simpliest way is to call \db\query::order

$query->order ('color','desc');
//OR
$query->order ('color');
//OR
$query->order ('color','asc');
//asc is required when calling directly \db\query::order

And multi field order technics:

$query->order->add ('color','desc');
$query->order->add ('name', 'asc');
$query->order->add ('type');

This will generate something like: order by color desc, name asc, type asc but with full field names

Single field order can by done with \db\order functions also

$query->order->field ('color');
$query->order->method ('desc');

The very simple and not recomended usage also:

$query->order->field = 'color';
$query->order->method = 'desc';

Some little usefull trick

$query->order->method->swap();

It will swap order method 'asc' to 'desc', or if set to 'desc' than will swap to 'asc'.

Well this is also possible:

$query->order->add (new \db\order('color','desc'));
$query->order->add (new \db\order('name'));

Have a look at \db\order class and \db\method class for further details.

\db\order has

\db\order::items - an array which stores \db\order objects if multi order takes place
\db\order::field - here order stores field
\db\order::method - here order stores method
\db\order::add(field, method)
\db\order::field(field)
\db\order::method(method)

\db\method has

\db\method::mode - here method stores \db\method::asc or \db\method::desc
\db\method::mode($mode) - set mode
\db\method:swap() - swap modes
\db\method::asc() - set mode asc
\db\method::desc() - set mode desc