Query the database

Author Message

Michael Scofield

Sunday 08 June 2008 7:52:34 pm

Hi guys,

Is it possible to query the database inside a template override?

I'm creating a datatype that must provide Country, State and City fields and they must be related to each other.

I have registered the countries, states and cities in my database, and now I'm doing a design override at /design/standard/templates/content/datatype/edit and I want to show 3 drop downs (HTML SELECT tag) with their respective options.

How do I query the database? Do I have to create a template function or template operator to do that?

Thanks
Michael Scofield

Piotrek Karaƛ

Sunday 08 June 2008 9:09:42 pm

Well, the real question is: what do you want to do with those drop down lists. If you want to store it per content object, then you need a custom datatype. If some operation that would not be content-related, that you may need a custom extension with an operator to return the db inventory and a view to collect choice and do some logic when the form with the lists gets sent.

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

Bruce Morrison

Sunday 08 June 2008 11:17:28 pm

Hi Micheal

Like Piotrek indicates I'd say you need a custom datatype. Because I do a lot of work integrating external data I created a custom datatype that allows that data to be chosen from an existing database table. See: http://projects.ez.no/index.php/integrate & http://ez.no/developer/contribs/datatypes/integrate_datatypes

I've used this in the past as a basis for the type of datatype you describe. I had to extend it by creating custom fetch and ajax ( using the xajax extension ) to populate the selects with appropriate values.

Cheers
Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

Michael Scofield

Monday 09 June 2008 11:15:43 pm

Thank you guys. You were very helpfull to me!

I was able to create the datatype and it's working like a charm now. Very hard work!! :-), but the result pays the price.

I created 3 tables in my database: one for Countries, other for Provinces (states) and other for Cities.

From the object edit template override I'm calling some fetch functions I made available inside a custom module. These fetch functions retrieves the correct countries, provinces and cities from the database.

With countries, when I change the item in the drop-down (html SELECT), I call a Javascript Ajax function that requests a PHP file. Then, the provinces and cities are updated. When I change the item for provinces, an Ajax request returns the new updated list for cities.

I don't know if it's correctly, but to use the database ($db instance), inside the Ajax script, I did the following:

$script =& eZScript::instance( array( 'description' => 'My first test script',
                                     'use-session' => false,
                                     'use-modules' => true,
                                     'use-extensions' => true ) );
$script->initialize();

// gets instance to eZ Publish DB
$db = eZDB::instance();

..rest of code here...

$script->shutdown();

Without the $script->initialize() the instance for $db wouldn't work.

Btw, Inside the Ajax php script I would like to have used some code to make sure the user is logged in. I tried to find examples in how to do that but after a while I gave up. I concluded that no problem, since the information returned by the Ajax script is not important.

Now I have a new task. In the administration interface I have to create a new administration tab, so I can use it to manage the countries, provinces and cities that are registered in the database.

Anyone have already did that?

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.