Section: Instantiation and use of a dao factory and record
|« DAO file||^ jDao: relational object mapping||Manipulating a result set »|
− Table of content
You have seen how to describe a dao in a xml file (see file). You have to
place this file in a
daos/ directory of a module and name it like this :
name.dao.xml where name is the name of your dao.
Remember that you actually define two objects:
- a dao object which is a factory, and inheriting from
jDaoFactoryBase: it allows you to retrieve, insert, modify, delete one ore several records. It proposes basic methods, but also has the methods that you will have described in the xml file in the
- a dao record object, inheriting form
jDaoRecordBase, representing a database record whose properties are described in the xml file.
Retrieving the factory and an empty DAO record ¶
To be able to retrieve these objects, you have to use the
which proposes several static methods:
get(): allows to get a factory. Always return the same instance (use a singleton)
create(): allows to get a new instance of a factory. Rarely useful.
createRecord(): allows to get an empty dao record object.
createConditions: allows to get a
jDAOConditionsobject which is useful to specify some select conditions to retrieve a set of record objects.
createRecord() all take a dao file
selector as parameter and an optional parameter which is the name of the
jDb profile to use (if this parameter is not specified, it will use the
If the used jDb profile specifies a table prefix, then all used tables in the dao file will be prefixed.
If we have
foo.dao.xml dao file in the bar module :
$myDao = jDao::get("bar~foo"); // or if this line of code is in a file of the bar module : $myDao = jDao::get("foo"); $myRecord = jDao::createRecord("foo");
$myDao contains a factory of foo, and $myRecord an empty record of foo type.
You can also call the method
createRecord() (which does not have parameters),
available on the factory:
$myDao = jDao::get("bar~foo"); $myRecord = $myDao->createRecord();
Create, modify, delete a record ¶
delete() methods of the factory
are made for this. You specify a record to the first two methods. For
delete, you specify the keys of the record.
You should get a new record, fill it and then call the
insert method of
// get the factory $myFactory = jDao::get("foo"); // create a new record $record = jDao::createRecord("foo"); // fill the record $record->foo = "hello"; $record->bar = "..."; // save the record $myFactory->insert($record);
If there are some auto-incremented fields, the corresponding properties will be
updated by the
insert methode with the new value.
The process is the same as record creation: you retrieve a record, you modify
its properties, then you call the
update method of the factory:
// get the factory $myFactory = jDao::get("foo"); // retrieve the record which have 3 as primary key $record = $myFactory->get(3); // fill the record $record->foo = "hello"; $record->bar = "..."; // save the record $myFactory->update($record);
You should call the
delete method of the factory, by giving the primary key of the record to delete.
// get the factory $myFactory = jDao::get("foo"); // delete the record which have 3 as id $myFactory->delete(3);