−
Table of content

Creating, modifying and deleting records in a database is very useful. However,
it's not as useful as being able to fetch records and use them. The page on the
use of a dao factory and record showed you the create, the delete and the
update. This is the page on the fetch, or the "select data" part of jDao.

Depending on how and how much results are needed, the method is somewhat different.

To retrieve a single record by specifying it's key (the primary key in the database),
the method to use is called get().

There is no need to further parse the return value of the get() function since
it will fetch the specified record and return it as an object. The object is design
so that each column name is the name of a variable, and the column value, the variable value.

// instantiation of the factory
$myFactory = jDao::get("foo");
// retrieve a record whose content corresponds
// to the record with identifier = 3
$baz = $myFactory->get(3);
// the id of the record is 3, as we specified
echo $baz->id;

In the case where the primary key is several fields, declared like this in your
xml file, primarykey="key1,key2", you have to give all key values at te same time:

When all the records are needed, the findAll() method is used. This method
will return a result set, (jDbResultSet class) containing all the results
from the database in the order they where added to the database. Remember that
this class implements the Iterator interface, so it can be used into a
foreach statement.

To retrieve record(s) through criteria, there are three simple methods available,
findBy(), countBy() and deleteBy(). These methods have one
required parameter, a jDaoConditions object, which contains all the conditions.

Lastly, the deleteBy() method allow to delete records corresponding to indicated
criteria, it returns the number of deleted records:

$nb_deleted = $myFactory->deleteBy($conditions);

You will see that you can get the same result by defining methods in the xml file.
However, you will choose findBy() or a xml-defined method, depending of the context.

Criteria-based retrieving is best used when you don't know the number of criterion
and their type. This includes things such as a complex search form, where the user
can choose which criterion are applied. Criteria-based retrieving is also best used
when the query is used only one time or very rarely. This is because the xml-defined
methods are compiled to PHP, and thus are included each time you call the factory.
It may not be useful to always include some code which is seldomly used. Using
criteria-based retrieving will improve the global performances in that case.

In the other cases, it is recommended to use xml methods, especially when you know
the criteria by advance (without forcibly knowing their value of course), and when
it is an often used research. jDao generates SQL queries during the generation of
the PHP file, so this is a process it has not to do when executing the page.

For example, we often redefine the findAll method in XML, to be able to specify
a retrieving order...

This manual is distributed under the terms of licence Creative Commons by-nc-sa 3.0. Therefore you're allowed to copy, modify and distribute and transmit it publicly under the following conditions: Attribution, Noncommercial, Share Alike.