Path

ez.no / exponential / documentation / customization / components / datatypes / ezkeyword / automatic object relation


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.

Comments

Bug Found and How To Make It Work

If in debug you are finding that a bad sql error shows up with the text ARRAY in the query it is possible that you need to make some minor adjustments to the ezkeyword.php file in the kernal/classes/datatype/ dir .

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

I copied the example exactly as show and was unable to get it to show related objects. I then debugged the code using {$node.object.data_map.keyword|attribute(show,2)} and {$node.object.data_map.keywords|attribute(show,2)} and was unable to get any output. Any suggestions?

Requires patches SVN to work

This feature doesn't work with the 3.1-1 (latest stable) version. You require patched files from SVN.

See: http://ez.no/content/view/full/29028/

Log in or create a user account to comment.

Contents

Customization

Access control
Exponential API Documentation
Content structure
Custom design
Components
    Images
    Datatypes
       eZXMLText
       eZKeyword
          Automatic object relation
       eZObjectRelation
    Shop
    Information collection
    Search
    Form processing
    Error handling
    Icons
    Menus
    Toolbars
Tips & Tricks
Troubleshooting


Created

07/07/2003
3:31:23 pm
by Paul Borgermans

Last updated

08/07/2003
2:01:43 pm
by B�rd Farstad

Authors

Paul Borgermans
B�rd Farstad



This page is part of the Exponential documentation. The documentation is available under the GNU Free Documentation License. All contributions will be released under the terms of this license.