ez.no / exponential / documentation / customization / components / datatypes / ezkeyword / automatic object relation
Version 3.1 of Exponential introduced a powerful datatype: keyword. This datatype can be used as an additional attribute in content classes to assign one or more keywords to an object.
Individual keywords need to be separated with a comma. Extra whitespace at the beginning or end of a keyword is stripped automatically when they are stored.
Objects which have one or more common keyword to the current node can be related in the templates as follows:
{let related=$node.object.data_map.keywords.content.related_objects} {section show=$related} <h2>Related documents</h2> <ol> {section name=Related loop=$related} <li><a href={$:item.url_alias|ezurl}>{$:item.name}</a></li> {/section} </ol> {/section} {/let}
The first part is used to set an array of related objects. If there are no related objects through common keywords, the section is not producing any output (equivalent to php evaluation: empty arrays are considered "false"):
{let related=$node.object.data_map.keywords.content.related_objects} {section show=$related}
The rest of the template code loops around any related objects and displays a link and their name.
Log in or create a user account to comment.
Comments
Bug Found and How To Make It Work
New User
Friday 24 October 2003 10:00:55 pm
The function relatedObjects should be
function &relatedObjects()
{
$return = false;
if ( $this->ObjectAttributeID )
{
// Fetch words
$db =& eZDB::instance();
$wordArray =& $db->arrayQuery( "SELECT * FROM ezkeyword_attribute_link
WHERE objectattribute_id='" . $this->ObjectAttributeID ."' " );
$keywordIDArray = array();
// Fetch the objects which have one of these words
foreach ( $wordArray as $word )
{
$keywordIDArray[] = $word['keyword_id'];
}
$keywordString = implode( ", ", $keywordIDArray );
if ( count( $keywordIDArray ) > 0 )
{
$objectArray =& $db->arrayQuery( "SELECT DISTINCT ezcontentobject_attribute.contentobject_id FROM ezkeyword_attribute_link, ezcontentobject_attribute
WHERE keyword_id IN ( $keywordString ) AND
ezcontentobject_attribute.id = ezkeyword_attribute_link.objectattribute_id
AND objectattribute_id <> '" . $this->ObjectAttributeID ."' " );
$objectIDArray = array();
foreach ( $objectArray as $object )
{
$objectIDArray[] = $object['contentobject_id'];
//echo $object['contentobject_id']; currently outputs all related documents...
$resultarray[] = & eZContentObjectTreeNode::findMainNode( $object['contentobject_id'], true );
}
$return = $resultarray;
}
}
return $return;
}
instead of
function &relatedObjects()
{
$return = false;
if ( $this->ObjectAttributeID )
{
// Fetch words
$db =& eZDB::instance();
$wordArray =& $db->arrayQuery( "SELECT * FROM ezkeyword_attribute_link
WHERE objectattribute_id='" . $this->ObjectAttributeID ."' " );
$keywordIDArray = array();
// Fetch the objects which have one of these words
foreach ( $wordArray as $word )
{
$keywordIDArray[] = $word['keyword_id'];
}
$keywordString = implode( ", ", $keywordIDArray );
if ( count( $keywordIDArray ) > 0 )
{
$objectArray =& $db->arrayQuery( "SELECT DISTINCT ezcontentobject_attribute.contentobject_id FROM ezkeyword_attribute_link, ezcontentobject_attribute
WHERE keyword_id IN ( $keywordString ) AND
ezcontentobject_attribute.id = ezkeyword_attribute_link.objectattribute_id
AND objectattribute_id <> '" . $this->ObjectAttributeID ."' " );
$objectIDArray = array();
foreach ( $objectArray as $object )
{
$objectIDArray[] = $object['contentobject_id'];
}
$return =& eZContentObjectTreeNode::findMainNode( $objectIDArray );
}
}
return $return;
}
You'll notice that the line :
eZContentObjectTreeNode::findMainNode( $object['contentobject_id'], true );
is moved within the previous foreach statement instead of outside of it. Also, the 'true' parameter is put in place to tell ezContentObjectTreeNode::findMainNode() to return result as a complete object instead of just an id.
After this change the related items worked. I appear to be using 3.2 so I think this should be a relevant bug. Anyone have any feedback on that?
Related Objects Not Showing Up
New User
Thursday 23 October 2003 10:54:08 pm
Requires patches SVN to work
Bruce Morrison
Tuesday 12 August 2003 3:42:40 am
See: http://ez.no/content/view/full/29028/