Re: DB_DataObject - Table alias in joined query
| From: | arnaud sellenet | Date: | Tue, 22 Aug 2006 21:42:25 +0000 |
| Subject: | Re: DB_DataObject - Table alias in joined query | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-24961@lists.php.net to get a copy of this message | ||
Le 22 août 06 à 21:55, Justin Patrin a écrit :
On 8/22/06, arnaud sellenet <demental@free.fr> wrote:Doing this : <?php $cat = & DB_DataObject::factory('ent_categorie'); $fcat = & DB_DataObject::factory('ent_categorie'); $cat->selectAdd(); $cat->selectAdd('ent_categorie.*'); $cat->joinAdd($fcat,'right','fcat'); $cat->selectAs($fcat,'ent_categorie__%s'); ?> results in that : SELECT ent_categorie.* , ent_categorie.id as ent_categorie__id , ent_categorie.titre as ent_categorie__titre , ent_categorie.strip as ent_categorie__strip , ent_categorie.active as ent_categorie__active , ent_categorie.categorie_id as ent_categorie__categorie_id , ent_categorie.gauche as ent_categorie__gauche , ent_categorie.droite as ent_categorie__droite , ent_categorie.path as ent_categorie__path FROM ent_categorie LEFT JOIN entada.ent_categorie AS fcat ON fcat.id=ent_categorie.categorie_id This is still not what I need. The alias is used for the WHERE clause, not for the select fields. What I would want is : SELECT ent_categorie.* , fcat.id as ent_categorie__id , fcat.titre as ent_categorie__titre , fcat.strip as ent_categorie__strip ,fcat.active as ent_categorie__active , fcat.categorie_id as ent_categorie__categorie_id , fcat.gauche as ent_categorie__gauche , fcat.droite as ent_categorie__droite , fcat.path as ent_categorie__path FROM ent_categorie LEFT JOIN entada.ent_categorie AS fcat ON fcat.id=ent_categorie.categorie_id I've tried different ways using joinAdd(), selectAs(), selectAdd() but never achieved my goalHi. I have a problem making a joined query using DB_DataObject to the table itself (tree structure). This is the php code to build the query : <?php $cat = & DB_DataObject::factory('ent_category'); $fcat = & DB_DataObject::factory('ent_category'); $cat->joinAdd($fcat); $cat->selectAs($fcat,'ent_category__%s'); ?> This is the resulting SQL : SELECT count(ent_category.id) as DATAOBJECT_NUM FROM ent_category INNER JOIN project.ent_category ON ent_category.category_id=project.ent_category.id Which makes a SQL error. And this is the query that would work : SELECT count(e1.id) as DATAOBJECT_NUM FROM ent_category e1 INNER JOIN project.ent_category ON e1.category_id=project.ent_category.id In my mind I would have written something like this : <?php $cat = & DB_DataObject::factory('ent_category'); $cat->setAlias('e1'); $fcat = & DB_DataObject::factory('ent_category'); $cat->joinAdd($fcat); $cat->selectAs($fcat,'ent_category__%s'); ?>Look at the API docs for joinAdd(). IIRC it has an option for the alias.
--Justin Patrin --PEAR General Mailing List (http://pear.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php