RE: [PEAR] XML/Query2XML.php : question :
| From: | Lukas Feiler | Date: | Tue, 04 Mar 2008 18:49:10 +0000 |
| Subject: | RE: [PEAR] XML/Query2XML.php : question : | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-28595@lists.php.net to get a copy of this message | ||
Hi Lucas!
First of all, I'm glad to see somebody making use of that many advanced
features of Query2XML :-)
Lucas Mommers wrote:
>
>
> Hi Lukas and others,
>
> I have a question regarding the XML/Query2XML.php package.
>
> <?php
> $dom = $query2xml->getXML(
> 'SELECT * FROM BA_AANBOD WHERE LAND = "Nederland" LIMIT 1',
> array(
> 'rootTag' => 'woningen',
> 'idColumn' => 'id',
> 'rowTag' => 'woning',
> 'elements' => array(
> 'deeplinkURL' => '?#Utils::deepLink()',
> 'prijs' => '?#Utils::returnPrice(prijs)',
> 'prijsType' => '?#Utils::priceCond()',
> 'status' => '?#Utils::statusObject()',
> 'koop' => '?#Utils::buyRent()',
> 'type' => 'object_soort',
> 'bouwjaar',
> 'postcode',
> 'adres',
> 'plaats',
> 'inhoud',
> 'perceelOppervlak' => 'opp_perceel',
> 'beschrijvingKort' => 'algemeen_omschr1',
> 'beschrijving' => array(
> 'value' => '?#Utils::descCombine()',
> 'attributes' => array(
> 'html' => ':ja'
> )
> ),
> 'afbeeldingen' => array(
> 'sql' => array(
> 'data' => array(
> 'id'
> ),
> 'query' => 'SELECT ID,URL FROM BA_FOTOS WHERE
> OBJECTID = ?
> ORDER BY MAIN_IMG DESC'
> ),
> 'rootTag' => '',
> 'idColumn' => 'id',
> 'elements' => array(
> 'afbeeldingURL' => 'url'
> )
> ),
The most obvious (but not the neatest) way to solve your problem is by
changing the above complex element specification as follows:
'afbeeldingen' => array(
'sql' => array(
'data' => array(
'id'
),
'query' => 'SELECT ID,URL FROM BA_FOTOS WHERE OBJECTID = ?
ORDER BY MAIN_IMG DESC'
),
'rowTag' => '__something',
'idColumn' => 'id',
'elements' => array(
'afbeeldingURL' => 'url'
)
)
A couple of notes here:
- What you want to get rid of is the rowTag that encloses your
afbeeldingURL element. If the rowTag option is not set it is
determined by the array key used for your complex element
specification (here: 'afbeeldingen')
- in order to hide a tag in the output (it still needs to be
created internally to keep your elements in order) prefix its
name with two underscores ('__'). Please see [1]
- As define in [2], if you don't want a rootTag, just don't use
the rootTag option
An even cleaner way of writing your complex element specification would be
as follows:
'afbeeldingURL' => array(
'sql' => array(
'data' => array(
'id'
),
'query' => 'SELECT ID,URL FROM BA_FOTOS WHERE OBJECTID = ?
ORDER BY MAIN_IMG DESC'
),
'idColumn' => 'id',
'value' => 'url'
)
This of course only works, if you only want one XML element per BA_FOTOS
record (the afbeeldingURL element).
If you want multiple XML elements per BA_FOTOS record (e.g. a URL and an
ID):
<afbeeldingen>
<afbeeldingURL>1859_huiskensstraat3voorgevel.jpg</afbeeldingURL>
<afbeeldingID>1</afbeeldingID>
<afbeeldingURL>1859_huiskensstraat3woonkamer.jpg</afbeeldingURL>
<afbeeldingID>2</afbeeldingID>
</afbeeldingen>
you would have to use the method described first.
Note: I would strongly recommend against using the '__' prefix if you don't
have to.
It certainly produces some overhead and doesn't look too pretty :-)
A completely different (and the best performing) approach would be to use a
single query (a LEFT JOIN in your case) instead of two queries.
The query passed as the first argument to getXML() would look something like
this:
SELECT
BA_AANBOD.*,
BA_FOTOS.ID AS ba_fotos_id, -- to prevent a name collision with
BA_AANBOD.ID
BA_FOTOS.URL
FROM
BA_AANBOD
LEFT JOIN BA_FOTOS ON BA_FOTOS.OBJECTID = BA_AANBOD.ID
WHERE
LAND = "Nederland"
ORDER BY
BA_AANBOD.ID ASC, BA_FOTOS.MAIN_IMG DESC
The complex element specification to create the afbeeldingURL elements would
look like this:
'afbeeldingURL' => array(
'idColumn' => ba_fotos_id',
'value' => 'url'
)
[snip]
Indeed, the manual could be clearer on this complex issue.
Thanks a lot for brining it up - I will update the manual accordingly
(hopefully this weekend).
>
> --
> Lucas M
>
take care,
Lukas
[1]
http://query2xml.sf.net/docs/html/XML_Query2XML/tutorial_XML_Query2XML.pkg.h
tml#global_options.hidden_container_prefix
[2]
http://query2xml.sf.net/docs/html/XML_Query2XML/tutorial_XML_Query2XML.pkg.h
tml#query2xml_getxml.options_rowtag