Trace: • erreurs-1.1 • credits • 1.2.x
Differences ¶
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:tutorials:main:using-dao [2007/09/16 06:51] – created laurent | en:tutorials:main:using-dao [2008/11/19 21:42] (current) – laurent | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== How to use daos ====== | + | |
- | + | ||
Jelix proposes a relational object-mapping system named jDao, based on DAO pattern. | Jelix proposes a relational object-mapping system named jDao, based on DAO pattern. | ||
Line 8: | Line 7: | ||
Concretely with jDao, a DAO XML file enables you to define a record and a factory, which will act on one or more tables at the same time. You thus define the mapping in it: which field of the table will go in which property of the record, as well as the type of data, the keys, on which tables the mapping is carried out, according to which joints etc. | Concretely with jDao, a DAO XML file enables you to define a record and a factory, which will act on one or more tables at the same time. You thus define the mapping in it: which field of the table will go in which property of the record, as well as the type of data, the keys, on which tables the mapping is carried out, according to which joints etc. | ||
- | + | Starting from this file, jDao dynamically generates two classes, stored in a php file in the cache of jelix, | |
- | Starting from this file, jDao dynamically generates two classes | + | |
In the DAO XML file, you can also define your own access methods to data, and jDao will generate the corresponding methods and requests in the DAO factory. | In the DAO XML file, you can also define your own access methods to data, and jDao will generate the corresponding methods and requests in the DAO factory. | ||
Line 17: | Line 15: | ||
- | You have command to be able to create a DAO file, based on an existing table. It has this following syntax: | + | You have a command to be able to create a DAO file, based on an existing table. It has this following syntax: |
| | ||
- | A news table has been created before, and we will create a DAO named " | + | A "news" |
<code bash> | <code bash> | ||
Line 29: | Line 27: | ||
- | You obtain an actu.org/ | + | You obtain an news.org/ |
- | Its content is what follows : | ||
<code xml> | <code xml> | ||
<?xml version=" | <?xml version=" | ||
<dao xmlns=" | <dao xmlns=" | ||
< | < | ||
- | < | + | < |
</ | </ | ||
< | < | ||
- | < | + | < |
- | < | + | < |
- | < | + | < |
< | < | ||
</ | </ | ||
</ | </ | ||
- | |||
</ | </ | ||
This is a very simple content, and of course there are other attributes and tags to enrich it and customize it. For the moment, we will leave it there. | This is a very simple content, and of course there are other attributes and tags to enrich it and customize it. For the moment, we will leave it there. | ||
- | |||
===== List the news ===== | ===== List the news ===== | ||
- | We will now use this DAO to retrieve the list of news. We thus will request from jDao the factory of this DAO, and call its preset method findAll. | + | We will now use this DAO to retrieve the list of news. We thus will request from jDao the factory of this DAO, and call its preset method |
<code php> | <code php> | ||
Line 60: | Line 55: | ||
</ | </ | ||
- | As parameter of jDao:: | + | As parameter of jDao:: |
- | Actually, this is not really a list, but a jDbResultSet, | + | Actually, this is not really a list, but a //jDbResultSet//, which is an iterator on the results of the corresponding request. |
Let's integrate it in our controller : | Let's integrate it in our controller : | ||
Line 81: | Line 76: | ||
</ | </ | ||
- | As stated ealier, the body property of jResponseHtml is a jTpl object (template engine). We give the list to it in a " | + | As stated ealier, the body property of jResponseHtml is a jTpl object (template engine). We give the list to it in a " |
< | < | ||
Line 95: | Line 90: | ||
</ | </ | ||
- | You discover here the {foreach} template tag, which | + | You discover here the {foreach} template tag, which is exactly like the php foreach |
- | Vous découvrez ici le tag de template {foreach}, which is exactly like the php foreach. To display some values, you only have to put the name of the template var after a $ and between {}. | + | |
- | The list returned by findAll is a list of record objects, as defined in the xml file. The have then the id_news, subject, text and news_date properties. | + | To display some values, you only have to put the name of the template var after a $ and between {}. |
+ | |||
+ | The list returned by findAll is a list of record objects, as defined in the xml file. The have then the news_id, subject, text and news_date properties. | ||
Display the page one more time : | Display the page one more time : | ||
- | | + | |
You should see the list of news. | You should see the list of news. | ||
- | |||
- | ---- | ||
- | * Next : [[en: | ||
- | * Previous : [[en: | ||
- | * [[en: |