aql::select()

From SkyPHP

Jump to: navigation, search

aql | aql select |


Retrieve data from the database.

Contents

Description

array select ( string $aql, [ array $clause, [ string $rs_callback, [ object $db_conn ] ] ] )
array select ( string $model, [ array $clause, [ string $rs_callback, [ object $db_conn ] ] ] )
array select ( string $aql|$model, [ string $rs_callback, [ object $db_conn ] ] )
array select ( string $aql|$model, [ object $db_conn ] )
array select ( array $keys  )

Parameters

aql - the aql statement to query

model - the model to query

clause - additional clauses to inject into the aql statement (see clause array)

rs_callback - callback function name

db_conn - database connection object

Return Values

Returns an array of records. Each record can be a multi-dimensional array.

Examples

Left Join Tables

Display the title and year of every Pink Floyd track in alphabetical order

$aql = "track {
            number,
            title
            order by title asc
        }
        album {
            name as album_name,
            year
        }
        artist {
            where name = 'Pink Floyd'
        }";
$rs = aql::select( $aql );
foreach ( $rs as $track ) {
    echo $track['title'] . ', ' . $track['year'] .  '<br />';
}

The aql statement above executes the following sql statement:

select track.number, 
       track.title,
       album.name as album_name, 
       album.year
from track
left join album on album.id = track.album_id and album.active = 1
left join artist on artist.id = album.artist_id and artist.active = 1
where track.active = 1
and artist.name = 'Pink Floyd'
order by track.title asc

Model Query

Display the first name of the person who is logged in. [see also aql::value()]

$person = aql::select( 'person', array( 'where' => 'id = ' . PERSON_ID ) );
echo $person['fname'];

Multi-Dimensional Recordset

Display the name of every Pink Floyd album and a list of its tracks

$aql = "
    album {
        name

        track {
            title
            order by number asc
        }

    }
    artist {
        where name = 'Pink Floyd'
    }
";
$albums = aql::select( $aql );
foreach ( $albums as $album ) {

    echo $album['name'];

    echo '<ol>'; 

    $tracks = $album['track'];
    foreach ( $tracks as $track ) {

        echo '<li>' . $track['title'] . '</li>';

    }

    echo '</ol>';
}

Callback Function

Display the name and age of the person who is logged in

$aql = "person {
            fname,
            birthdate
            where id = " . PERSON_ID . "
        }";
$rs = aql::select($aql,'person_callback');

echo $rs['fname'] . ' is ' . $rs['age'] . ' years old.';

function person_callback($rs) {
    $rs['age'] = floor( ( time() - strtotime( $rs['birthdate'] ) ) / ( 365 * 24 * 60 * 60 ) );
    return $rs;
}

See Also

Known Bugs & Issues

  • can't join the same table more than once
  • can't use aggregate function with multiple tables
  • can't use sql functions with more than one parameter. i.e. to_char(date,'YYYY') doesn't work
Personal tools