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.

eZ debug

Timing: Jan 19 2025 04:34:12
Script start
Timing: Jan 19 2025 04:34:12
Module start 'layout'
Timing: Jan 19 2025 04:34:12
Module start 'content'
Timing: Jan 19 2025 04:34:13
Module end 'content'
Timing: Jan 19 2025 04:34:13
Script end

Main resources:

Total runtime0.8445 sec
Peak memory usage4,096.0000 KB
Database Queries62

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0082 589.0391152.6094
Module start 'layout' 0.00820.0031 741.648439.4297
Module start 'content' 0.01130.8317 781.0781592.9531
Module end 'content' 0.84310.0014 1,374.031316.1875
Script end 0.8444  1,390.2188 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00380.4517160.0002
Check MTime0.00140.1607160.0001
Mysql Total
Database connection0.00090.108610.0009
Mysqli_queries0.792293.8160620.0128
Looping result0.00050.0632600.0000
Template Total0.810396.020.4051
Template load0.00210.252320.0011
Template processing0.808195.698320.4041
Template load and register function0.00020.018510.0002
states
state_id_array0.00130.159410.0013
state_identifier_array0.00130.157420.0007
Override
Cache load0.00180.2119320.0001
Sytem overhead
Fetch class attribute can translate value0.00100.115230.0003
Fetch class attribute name0.00120.147360.0002
XML
Image XML parsing0.00120.145730.0004
class_abstraction
Instantiating content class attribute0.00000.001260.0000
General
dbfile0.00120.1372230.0001
String conversion0.00000.001240.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
4content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
7content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 18
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs