Differences ¶
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:tutorial:using-dao [2007/01/13 21:56] – doubleface | en:tutorial:using-dao [2007/09/17 22:42] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== How to use daos ====== | + | This page has been moved to [[en:tutorials:main:using-dao]]. |
- | + | ||
- | Jelix proposes a relational object-mapping system named jDao, based on DAO pattern. | + | |
- | + | ||
- | The DAO pattern is based on two types of objects : a " | + | |
- | + | ||
- | + | ||
- | 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 based respectively on jDaoRecordBase and jDaoFactoryBase (which is stored in a php file in the cache of jelix), in which all main methods and SQL requests are **statically** provided. Indeed, in contrary to other mapping systems, SQL requests are thus generated only once, and not dynamically with each page calls That allows better performances. | + | |
- | + | ||
- | 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. | + | |
- | + | ||
- | + | ||
- | ===== creating a first DAO ===== | + | |
- | + | ||
- | + | ||
- | You have 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 " | + | |
- | + | ||
- | <code bash> | + | |
- | php jelix.php createdao news news news | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | You obtain an actu.org/ | + | |
- | + | ||
- | Its content is what follows : | + | |
- | <code xml> | + | |
- | <?xml version=" | + | |
- | <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. | + | |
- | + | ||
- | ===== 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. With this intention, we use jDao::get() : | + | |
- | + | ||
- | <code php> | + | |
- | $fact = jDao:: | + | |
- | $liste = $fact-> | + | |
- | </ | + | |
- | + | ||
- | As parameter of jDao:: | + | |
- | + | ||
- | Actually, this is not really a list, but a jDbResultSet, | + | |
- | + | ||
- | Let's integrate it in our controller : | + | |
- | + | ||
- | <code php> | + | |
- | function index() { | + | |
- | $rep = $this-> | + | |
- | $rep-> | + | |
- | $rep-> | + | |
- | + | ||
- | $fact = jDao:: | + | |
- | $list = $fact-> | + | |
- | + | ||
- | $rep-> | + | |
- | + | ||
- | return $rep; | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | As stated ealier, the body property of jResponseHtml is a jTpl object (template engine). We give the list to it in a " | + | |
- | + | ||
- | < | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | {foreach $list as $news} | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | {/ | + | |
- | </ | + | |
- | </ | + | |
- | + | ||
- | You discover here the {foreach} template tag, which | + | |
- | 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. | + | |
- | + | ||
- | Display the page one more time : | + | |
- | + | ||
- | | + | |
- | + | ||
- | You should see the list of news. | + | |
- | + | ||
- | + | ||
- | + | ||
- | ---- | + | |
- | * Suite : [[tutoriel:formulaire-de-news|Création d'un formulaire]] | + | |
- | * Précedent | + | |
- | * [[tutoriel: | + |