| Prev | Next |
List objects contains DAO objects that are fetched from result set. However it is important to point, that list object does not contain all objects at once - it only has handle to results set from database and creates new instance every time you fetch next object, so you can freely modify filters, criteriums, limits etc. without worry about performance. Lists are mainly mentioned to work as iterators so if you would want to fetch some objects directly you have to use rewind() method first to initialise results cursor and then fetch single objects using current() for fething and next() for moving to next record. Here is example that shows basic usage of list object including parts explained in following paragraphs like limiting, iterators and counting:
You can limit set of selected rows by setting LIMIT and OFFSET clauses for SQL query. To reduce amount of selected rows use OTS_Base_List::limit(). To skip starting rows use OTS_Base_List::offset(). Note that you can use offset() only with limit() - if you don't use LIMIT clause, OFFSET is not applied! Also remember that rows are counted from 0, so setting OFFSET to 2 will skip 2 first rows starting from third. Another thing is to mention about limiting and skipping at once - if you set LIMIT to a and OFFSET to b, then query will select up to a rows starting from b + 1 - it means that OFFSET does not reduce number of selecting rows, but only moves starting pointer forward. LIMIT 10 OFFSET 2 will select rows from 3 to 12.
As it was written in previous paragraphs the most natural way of using lists objects is foreach loop. Lists classes implements Iterator interface which allows you to use it like an array in such case.
Another implemented interface is Countable. It means that you can also use count() on list object to get amount of rows selected by this list. Note that it uses COUNT() SQL structure so it is also very optimalised and doesn't need to fetch all rows before counting.
Very powerful (and very advanced) option is list filtering. It filters selected rows to choose only those which you want to. It is highly flexible, but also quite difficult at first way. With OTS_SQLFilter class you can create nearly any possible WHERE clause based on fields, values and operators (sub-queries and SQL functions are not supported). Again - fitering also works directly on SQL layer so it is extremly fast - nothing is done by PHP, it's pure optimalisation of your work.
The most basic use is it's compareField() method. It compares field of record with literal value:
By default using compareField() method you execute simple equal comparsion. But you can of course change operator of relation to other - as third parameter you pass operator which you want to use (= by default). You use operator constants for that:
Also by default all appended criteriums are obligatory - using AND to concat. You can of course change that to alternative possible criteriums - using OR to concat. Fourth parameter defines conditions relationship - to use OR you have to pass OTS_SQLFilter::CRITERIUM_OR constant.
As you noticed compareField() allows only literal fields comparsion. This method is only a wrapper for default - more complex one: addFilter(). It accepts field identifiers as operands. It means you can compare fields with fields - using OTS_SQLField class (at all you can do simple fields comparsion using compareField() - right operand can be instance of OTS_SQLField). Passing literal value in PHP effects as comparing to string. For example 'foo' will be used directly in SQL query. If you want to compare field values directly you have to create field identifier objects. For example to compare capacity field value with health field:
Also OTS_SQLField class allows you to create name identifiers of different table then listed table - it means you can for example compare fields of foreign keys:
As you noticed there is also one, probably the most advanced feature which you can use - sub-filters. It allows you to group OR-concated conditions with AND-concated groups. It is done by passing another OTS_SQLFilter class instance as parameter to addFilter() method.
| Prev | Up | Next |
| Guilds | Database | data/ directory resources |