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