eZ Community » Forums » Developer » Sort node list by float attribute:...
expandshrink

Sort node list by float attribute: hack or new contribution?

Sort node list by float attribute: hack or new contribution?

Sunday 11 March 2007 3:48:29 pm - 8 replies

My content classes have a lot of attributes of type ezfloat.
I have to print lists of objects (nodes) sorted by those float attributes.

Content fetch functions don't support sorting by float attributes, so i should sort my node lists after fetching.

I need your avice. What would you suggest:
1. hack ezfloat type to allow sorting (similar to ezprice type)
2. create new template operator
3. ...or something else?

Sunday 11 March 2007 5:12:42 pm

I believe you can redistribute datatypes as extensions.
http://ezpedia.org/wiki/en/ez/datatype

Modified on Sunday 11 March 2007 5:13:40 pm by Heath

Monday 12 March 2007 4:00:50 am

Hi Andre

I think that the ezfloat datatype <b>should</b> support sorting. I'd submit a bug.

You just need to add the following functions to
kernel/classes/datatypes/ezfloat/ezfloattype.php if you are willing to modify the core.

    /*!
     \return true if the datatype can be indexed
    */
    function isIndexable()
    {
        return true;
    }

    function sortKey( &$contentObjectAttribute )
    {
        $intPrice = (int)($contentObjectAttribute->attribute( 'data_float' ) * 100.00);
        return $intPrice;
    }

    /*!
     \reimp
    */
    function sortKeyType()
    {
        return 'int';
    }

Taken from ezprice datatype. Untested of course and will only work for values with 2 decimal places.

Cheers
Bruce

Modified on Monday 12 March 2007 4:20:15 am by Bruce Morrison

Monday 12 March 2007 4:20:36 am

This datatype has some issues already documented, take a look
http://issues.ez.no/IssueList.php?Search=ezfloat&SearchIn=1

It sure seems like this issue in particular is the same as the topic of this thread, the issue is over a year old!

* http://issues.ez.no/IssueView.php?Id=7538&activeItem=2

There is also another older entry which sands out as being older and a described just a little differently enough yet still possibly related,
http://issues.ez.no/IssueView.php?Id=3321&activeItem=4

I commented on issue #7538 and provided in diff format the patch Bruce provided earlier.

So there is already an existing issue documented on the subject, why not instead we all post comments to the (above) existing issue #7538

hth

Modified on Monday 12 March 2007 4:49:09 am by Brookins Consulting

Monday 12 March 2007 5:18:20 am

There is also a contribution here http://ez.no/community/contribs/h...er_by_float_or_price_datatype_fields that seems to handle the issues.

@Graham - My patch is not suitable for the core as it will not work reliably when there are more that 2 decimal places.

To do it properly you need to add a new sort_key_type for float values as per the above contrib.

Cheers
Bruce

Modified on Monday 12 March 2007 5:24:08 am by Bruce Morrison

Thursday 31 January 2008 9:50:53 am

eZFloat still doesn't support sorting in 4.0 !
I'm going to report this as a bug since it has never be fixed... I guess this sould also be fixed in eZPrice

Thursday 22 May 2008 12:00:59 pm

Better than a kernel hack, you can create a datatype who extends eZFloatType like that :

<?php
/**
 * classe extendedFloatType similaire à ezFloatType mais avec la possibilitée de trier.
 */

include_once( "kernel/classes/datatypes/ezfloat/ezfloattype.php" );

class extendedFloatType extends eZFloatType
{
	const DATA_TYPE_STRING = "extendedfloat";
	
    function extendedFloatType()
    {
		$this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "Float", 'Datatype name' )." 2",
                           array( 'serialize_supported' => true,
                                  'object_serialize_map' => array( 'data_float' => 'value' ) ) );
        $this->FloatValidator = new eZFloatValidator();
    }
	
	
	
   	/*!
     \return true if the datatype can be indexed
   	*/
   function isIndexable()
   {
       return true;
   }
 

   function sortKey( $contentObjectAttribute )
   {
       $intPrice = (int)($contentObjectAttribute->attribute( 'data_float' ) * 100.00);
       return $intPrice;
   }
 

   /*!
    \reimp
   */
   function sortKeyType()
   {
       return 'int';
   }
}

eZDataType::register( extendedFloatType::DATA_TYPE_STRING, "extendedFloatType" );

?>

Thursday 22 May 2008 6:21:22 pm

It is a good solution.
There is only one small disadvantage: sometimes it is difficult to support own datatypes. E.g. it should be rewritten with transition to ezp 4.x/PHP5.

Thursday 29 May 2008 10:58:27 am

After hack / new datatype, think to update your database like that :

// Update classe datatype
UPDATE `mydb`.`ezcontentclass_attribute` SET `data_type_string` = 'extendedfloat' WHERE `ezcontentclass_attribute`.`id` =827 AND `ezcontentclass_attribute`.`version` =0 LIMIT 1 ;

// Update attribute
UPDATE `mydb`.`ezcontentobject_attribute` SET `data_type_string` = 'extendedfloat' WHERE `contentclassattribute_id` =827

// Update sort_key
UPDATE `mydb`.`ezcontentobject_attribute` SET `sort_key_int` = `data_float`*100 WHERE `contentclassattribute_id` =827
expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu