Forums / Developer / Best way to query for attribute ID?

Best way to query for attribute ID?

Author Message

Russell Michell

Wednesday 05 May 2010 8:38:22 pm

Hi folks,

What's the best way to query the ezcontentobject_attribute table and fetch an attribute's contentobject_id using only a ezcontentobject_attribute.id as a 'key'?

I have a method as part of a mod to an extension that parses the eZ contentobject_attribute ID from an <img> element (in ezxmlexport) and returns its contentobject_id. But it's just a plain DB query (see below)

So is there a more robust way to perform these queries? eZ Components maybe?

Here is the method:

/*
* @desc Fetch ezcontentobject_id FROM ezcontentobject_attribute TABLE given an id
*/
 function fetchContentObjectFromCOAID($id)
 {
 $query = '';
 $db =& eZDB::instance();
 // Multiple version will be returned unless we restrict the version to latest:
 $query .= 'SELECT contentobject_id FROM ezcontentobject_attribute WHERE id = '.$id;
 $query .= ' AND version = (SELECT MAX(version) FROM ezcontentobject_attribute  WHERE id = '.$id.')';
 $rows = $db->arrayQuery($query);
 if($rows)
 {
 return $rows;
 } 
 }

If you're interested - here is the other custom method I wrote for the ezxmlexport extension - used with fetchContentObjectFromCOAID() above - to parse exported XML for <img> elements and replace with <embed> elements so they get imported correctly by the data_import extension:

/*
* @desc Replace "faulty" <img> tags which don't play well with data_import extension
* This method converts <img> elements to <embed> elements, which _do_ import
* @ToDo Is there some XSL magic that could do this as this would be the correct tier to perform this dort of task
* @author Russell Michell April/May 2010
* @param str $xmlstring the string converted via eZXMLTextType::domString()
* @return str $xmlstring to be passed to custom source handler for data_import
*/
 function imgToEmbed($xmlstring)
 {
 // Get the string nmerical identifier from the URL path of the <img> tag's src attribute:
 preg_match_all('#<img src=".*/([0-9]{1,9})-.*".*>#',$xmlstring,$matches);
 $treeData = array();
 foreach($matches[1] as $key=>$contentobject_attribute_id)
 { 
$treeData[] = $this->fetchContentObjectFromCOAID($contentobject_attribute_id);
 }
 // use $val[$i] notation as retured array from DB query uses a numeric index as does $treeData 
 $replacement = array();
 $i=0;
 foreach($treeData as $key=>$val)
 {
 $replacement[] = '<embed href="ezobject://'.$val[$i]['contentobject_id'].'" size="standard" 
/>';
 }
 $pattern = array();
 foreach($matches[0] as $match)
 {
 // Create an array so we can use array:array replacement in preg_replace()
 $pattern[] = '#'.preg_quote($match).'#';
 }
 
 $retstring = preg_replace($pattern,$replacement,$xmlstring);
 return $retstring;
 }

Any help on this would be very gratefully received as I'm pretty much poking about in the dark otherwise! Oh and if you know anything more about importing/exporting content objects like 'images' - that would be very helpful.

Many thanks,
Russ

Russell Michell, Wellington, New Zealand.
We're building! http://www.theruss.com/blog/
I'm on Twitter: http://twitter.com/therussdotcom

Believe nothing, consider everything.

Bertrand Dunogier

Thursday 06 May 2010 1:36:48 am

You can use the API for that. It would even work as a one-liner, but some error control wouldn't hurt :-)

$attribute = eZContentObjectAttribute::fetch( $attributeID )
             ->attribute( 'contentobject_id' );

Bertrand Dunogier
eZ Systems Engineering, Lyon
http://twitter.com/bdunogier
http://gplus.to/BertrandDunogier

André R.

Thursday 06 May 2010 3:25:39 am

"SDK":

http://pubsvn.ez.no/doxygen/trunk/html/index.html

(Change 'trunk' with version if you want, but trunk has always "best" doc as we continually improve it as we change it from doxygen to phpdoc syntax we tend to improve it while at it)

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

eZ debug

Timing: Jan 30 2025 00:16:35
Script start
Timing: Jan 30 2025 00:16:35
Module start 'content'
Timing: Jan 30 2025 00:16:35
Module end 'content'
Timing: Jan 30 2025 00:16:35
Script end

Main resources:

Total runtime0.2629 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0061 587.9531180.8281
Module start 'content' 0.00620.0053 768.781398.1016
Module end 'content' 0.01150.2513 866.8828529.2969
Script end 0.2628  1,396.1797 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00321.2355200.0002
Check MTime0.00130.4774200.0001
Mysql Total
Database connection0.00060.219410.0006
Mysqli_queries0.216582.32501410.0015
Looping result0.00120.46341390.0000
Template Total0.251095.510.2510
Template load0.00060.236910.0006
Template processing0.250495.240710.2504
Override
Cache load0.00040.160110.0004
Sytem overhead
Fetch class attribute can translate value0.00110.422910.0011
XML
Image XML parsing0.00040.150210.0004
General
dbfile0.00883.3454200.0004
String conversion0.00000.002230.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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs