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

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 18 2025 10:29:47
Script start
Timing: Jan 18 2025 10:29:47
Module start 'layout'
Timing: Jan 18 2025 10:29:47
Module start 'content'
Timing: Jan 18 2025 10:29:47
Module end 'content'
Timing: Jan 18 2025 10:29:47
Script end

Main resources:

Total runtime0.8650 sec
Peak memory usage4,096.0000 KB
Database Queries60

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0066 587.9219152.6250
Module start 'layout' 0.00670.0030 740.546939.4609
Module start 'content' 0.00970.8535 780.0078601.2031
Module end 'content' 0.86320.0017 1,381.210916.1563
Script end 0.8650  1,397.3672 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00370.4305160.0002
Check MTime0.00140.1658160.0001
Mysql Total
Database connection0.00080.097710.0008
Mysqli_queries0.801892.6896600.0134
Looping result0.00090.1007580.0000
Template Total0.829095.820.4145
Template load0.00240.282220.0012
Template processing0.826695.555720.4133
Template load and register function0.00010.016010.0001
states
state_id_array0.00140.160710.0014
state_identifier_array0.00180.204220.0009
Override
Cache load0.00210.2405250.0001
Sytem overhead
Fetch class attribute can translate value0.00060.071630.0002
Fetch class attribute name0.00150.174760.0003
XML
Image XML parsing0.00690.797330.0023
class_abstraction
Instantiating content class attribute0.00000.001160.0000
General
dbfile0.00620.7190290.0002
String conversion0.00000.001140.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
3content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
3content/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
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 20
 Number of unique templates used: 8

Time used to render debug report: 0.0002 secs