Forums / Developer / Query the database

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?

eZ debug

Timing: Jan 19 2025 01:13:21
Script start
Timing: Jan 19 2025 01:13:21
Module start 'content'
Timing: Jan 19 2025 01:13:22
Module end 'content'
Timing: Jan 19 2025 01:13:22
Script end

Main resources:

Total runtime0.9248 sec
Peak memory usage4,096.0000 KB
Database Queries199

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0054 589.9688180.8438
Module start 'content' 0.00540.7776 770.8125598.0156
Module end 'content' 0.78300.1416 1,368.8281337.3828
Script end 0.9246  1,706.2109 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00400.4296210.0002
Check MTime0.00140.1555210.0001
Mysql Total
Database connection0.00070.071710.0007
Mysqli_queries0.843791.23751990.0042
Looping result0.00270.29051970.0000
Template Total0.891796.420.4459
Template load0.00210.224820.0010
Template processing0.889796.203620.4448
Template load and register function0.00020.017510.0002
states
state_id_array0.00110.122610.0011
state_identifier_array0.00160.175220.0008
Override
Cache load0.00170.1800320.0001
Sytem overhead
Fetch class attribute can translate value0.00210.225740.0005
Fetch class attribute name0.00090.097160.0001
XML
Image XML parsing0.00150.166340.0004
class_abstraction
Instantiating content class attribute0.00000.001260.0000
General
dbfile0.00240.2647340.0001
String conversion0.00000.000430.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 18
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs