Forums / Developer / Storing content in an ezselection attribute

Storing content in an ezselection attribute

Author Message

Roy Bøhmer

Friday 02 September 2005 2:43:11 am

Hi!
After reading a lot of code in ezcontentobject.php, ezcontentobjectattribute.php, ezselectiontype.php etc. I still can't figure out how to store content in an object attribute of datatype ezselection.

The attribute is set up to only store one selection (so I later can filter on it in the template).

I've tried with

$attribs =& $contentObject->contentObjectAttributes();
for($k=0;$k<count($attribs);$k++)
{
   switch($attribs[$k]->attribute("contentclass_attribute_identifier"))
   {
      case 'county':
      $attribs[$k]->setAttribute('data_int', $county);
      attribs[$k]->store();
      break;
    }
}

The $county variable conatins an integer, refering to the index in the selection datatype.
The data_int-value is stored, but I think the problem is that the value should be stored somewhere else.
But where and how?

Kristof Coomans

Friday 02 September 2005 3:30:59 am

eZSelection stores it's values in data_text, to support multiple selections.

For multiple selections, the different values will be seperated by -.

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Roy Bøhmer

Friday 02 September 2005 3:36:34 am

Thank you, Kristof!
Works like a charm

Roy

Laurent DIBON

Wednesday 05 April 2006 12:24:32 pm

I try to import user with ezselection attributes. If we suppose that we have an attribute called 'genre' wich can take 'male' or 'female' as option values (only one can be selected), what will be ecactly the code to store this attribute in php ?

is it :

switch($attribs[$k]->attribute("contentclass_attribute_identifier"))
{
case 'county':
$attribs[$k]->setAttribute('data_int', $county);
$attribs[$k]->setAttribute('data_text', 'yes');
attribs[$k]->store();
break;
}

Roy Bøhmer

Wednesday 05 April 2006 12:50:38 pm

The value in 'data_text' should be an integer. If you have to options it would likely be 0 or 1. Whitch is 'male' and which is 'female' is controlled by you in the (content)classdefenition.

$attribs =& $contentObject->contentObjectAttributes();  

for($k=0;$k<count($attribs);$k++)  
{    
   switch($attribs[$k]->attribute("contentclass_attribute_identifier"))   
   {      
         case 'genre':       
            $attribs[$k]->setAttribute('data_text', 0|1);       
            $attribs[$k]->store();       
         break;     
    }  
} 

- Roy

Laurent DIBON

Wednesday 05 April 2006 11:40:28 pm

Thank you very much for your answer,

It was'nt clear for me that I had to put an integer value whith data_text.
Now I'm right. It works fine.

Kristof Coomans

Thursday 06 April 2006 1:42:45 am

This has been answered already, but Laurent mailed me about this subject and I've send him a mail. So I'll post my reply here too, just as a future reference:

eZSelection stores it's value in data_text. But it stores the internal id of the option, not the option's name as many people think. So you'll have to lookup the id corresponding to your option name in the attribute's class attribute's content.

$selectedGenre = 'female';

$classContent = $attribs[$k]->attribute( 'class_content' );
$optionId = false;
foreach ( $classContent['options'] as $option )
{
   if ( $option['name'] == $selectedGenre )
   {
         $optionId = $option['id'];
         break;
   }
}

if ( $optionId )
{
    $attribs[$k]->setAttribute('data_text', $optionId);
    $attribs[$k]->store();
} 

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org