This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

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 - 9 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

Tuesday 24 December 2013 1:52:43 pm

Almost 2014 and nothing new, even on the 5 branch.
I made a quick patch, it should work on 2013.11 release.

This is not seriously tested and provided in hope that someone will find it usefull, i used ez publish for 1 month now and i'm far from being an expert (!)

Thank to Guillaume Kulakowski (in this thread) and Cesar Verdes ( http://projects.ez.no/search_and_order_by_float_or_price_datatype_fields ) for the inspiration.

In legacy

diff --git a/kernel/classes/datatypes/ezfloat/ezfloattype.php b/kernel/classes/datatypes/ezfloat/ezfloattype.php
index d3a8be2..d61df6c 100644
--- a/kernel/classes/datatypes/ezfloat/ezfloattype.php
+++ b/kernel/classes/datatypes/ezfloat/ezfloattype.php
@@ -399,11 +399,40 @@ class eZFloatType extends eZDataType
         $default = $classAttribute->attribute( 'data_float3' );
         if ( $default !== 0 )
         {
-            return array( 'data_float' => $default );
+            return array(   'data_float' => $default,
+                            'sort_key_float' => $default );
         }
 
         return array();
     }
+    
+    /*!
+      \return true if the datatype can be indexed
+     */
+ 
+     function isIndexable()
+     {
+        return true;
+     }
+     
+     function isInformationCollector()
+     {
+         return true;
+     }
+ 
+     function sortKey( &$contentObjectAttribute )
+     {
+         return $contentObjectAttribute->attribute( 'data_float' );
+     }
+ 
+     /*!
+      \reimp
+     */
+ 
+     function sortKeyType()
+     {
+         return 'float';
+     }
 
     /// \privatesection
     /// The float value validator
diff --git a/kernel/classes/ezcontentobjectattribute.php b/kernel/classes/ezcontentobjectattribute.php
index 02fef23..9dbf2b7 100644
--- a/kernel/classes/ezcontentobjectattribute.php
+++ b/kernel/classes/ezcontentobjectattribute.php
@@ -79,6 +79,10 @@ class eZContentObjectAttribute extends eZPersistentObject
                                                                   'datatype' => 'integer',
                                                                   'default' => '0',
                                                                   'required' => true ),
+                                         "sort_key_float" => array( 'name' => "SortKeyFloat",
+                                                                   'datatype' => 'float',
+                                                                   'default' => '0',
+                                                                   'required' => true ),
                                          "sort_key_string" => array( 'name' => "SortKeyString",
                                                                      'datatype' => 'string',
                                                                      'max_length' => 255,
@@ -328,16 +332,21 @@ class eZContentObjectAttribute extends eZPersistentObject
             $sortKey = $dataType->sortKey( $this );
             $this->setAttribute( 'sort_key_string', "" );
             $this->setAttribute( 'sort_key_int', 0 );
+            $this->setAttribute( 'sort_key_float', 0 );
             if ( $dataType->sortKeyType() == 'string' )
             {
                 $this->setAttribute( 'sort_key_string', $sortKey );
                 $this->setAttribute( 'sort_key_int', 0 );
-
+                $this->setAttribute( 'sort_key_float', 0 );
             }
             else if ( $dataType->sortKeyType() == 'int' )
             {
                 $this->setAttribute( 'sort_key_int', $sortKey );
             }
+            if ( $dataType->sortKeyType() == 'float' )
+            {
+                $this->setAttribute( 'sort_key_float', $sortKey );
+            }
 
             if ( $storeData )
             {
diff --git a/kernel/classes/ezcontentobjecttreenode.php b/kernel/classes/ezcontentobjecttreenode.php
index 6352716..631dc57 100644
--- a/kernel/classes/ezcontentobjecttreenode.php
+++ b/kernel/classes/ezcontentobjecttreenode.php
@@ -687,6 +687,10 @@ class eZContentObjectTreeNode extends eZPersistentObject
                                             $sortKey = 'sort_key_string';
                                         } break;
 
+                                    case 'float':
+                                        {
+                                            $sortKey = 'sort_key_float';
+                                        } break;
                                     case 'int':
                                     default:
                                         {
diff --git a/kernel/search/plugins/ezsearchengine/ezsearchengine.php b/kernel/search/plugins/ezsearchengine/ezsearchengine.php
index fbac00f..49435a7 100644
--- a/kernel/search/plugins/ezsearchengine/ezsearchengine.php
+++ b/kernel/search/plugins/ezsearchengine/ezsearchengine.php
@@ -1189,6 +1189,10 @@ class eZSearchEngine implements ezpSearchEngine
                             {
                                 $sortKey = 'sort_key_string';
                             }
+                            else if ($sortDataType == 'float') 
+                            {
+                                $sortKey = 'sort_key_float';
+                            }
                             else
                             {
                                 $sortKey = 'sort_key_int';

In new stack

diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/FieldValue/Converter/Float.php b/eZ/Publish/Core/Persistence/Legacy/Content/FieldValue/Converter/Float.php
index 85f2080..85c9521 100644
--- a/eZ/Publish/Core/Persistence/Legacy/Content/FieldValue/Converter/Float.php
+++ b/eZ/Publish/Core/Persistence/Legacy/Content/FieldValue/Converter/Float.php
@@ -122,7 +122,7 @@ class Float implements Converter
      */
     public function getIndexColumn()
     {
-        return 'sort_key_int';
+        return 'sort_key_float';
     }
 
     /**
diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/EzcDatabase.php b/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/EzcDatabase.php
index b6200fd..a165801 100644
--- a/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/EzcDatabase.php
+++ b/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/EzcDatabase.php
@@ -665,6 +665,9 @@ class EzcDatabase extends Gateway
             $this->dbHandler->quoteColumn( 'sort_key_int' ),
             $q->bindValue( $value->sortKeyInt, null, \PDO::PARAM_INT )
         )->set(
+            $this->dbHandler->quoteColumn( 'sort_key_float' ),
+            $q->bindValue( $value->sortKeyFloat )
+        )->set(
             $this->dbHandler->quoteColumn( 'sort_key_string' ),
             $q->bindValue( $value->sortKeyString )
         )->set(
@@ -734,6 +737,9 @@ class EzcDatabase extends Gateway
             $this->dbHandler->quoteColumn( 'sort_key_int' ),
             $q->bindValue( $value->sortKeyInt, null, \PDO::PARAM_INT )
         )->set(
+            $this->dbHandler->quoteColumn( 'sort_key_float' ),
+            $q->bindValue( $value->sortKeyFloat )
+        )->set(
             $this->dbHandler->quoteColumn( 'sort_key_string' ),
             $q->bindValue( $value->sortKeyString )
         );
diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/EzcDatabase/QueryBuilder.php b/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/EzcDatabase/QueryBuilder.php
index d5aa63e..91ed789 100644
--- a/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/EzcDatabase/QueryBuilder.php
+++ b/eZ/Publish/Core/Persistence/Legacy/Content/Gateway/EzcDatabase/QueryBuilder.php
@@ -82,6 +82,7 @@ class QueryBuilder
             $this->dbHandler->aliasedColumn( $query, 'data_int', 'ezcontentobject_attribute' ),
             $this->dbHandler->aliasedColumn( $query, 'data_text', 'ezcontentobject_attribute' ),
             $this->dbHandler->aliasedColumn( $query, 'sort_key_int', 'ezcontentobject_attribute' ),
+            $this->dbHandler->aliasedColumn( $query, 'sort_key_float', 'ezcontentobject_attribute' ),
             $this->dbHandler->aliasedColumn( $query, 'sort_key_string', 'ezcontentobject_attribute' ),
             // Content object names
             $this->dbHandler->aliasedColumn( $query, 'name', 'ezcontentobject_name' ),
diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/Mapper.php b/eZ/Publish/Core/Persistence/Legacy/Content/Mapper.php
index affe18c..d2e6412 100644
--- a/eZ/Publish/Core/Persistence/Legacy/Content/Mapper.php
+++ b/eZ/Publish/Core/Persistence/Legacy/Content/Mapper.php
@@ -390,6 +390,7 @@ class Mapper
         $storageValue->dataText = $row['ezcontentobject_attribute_data_text'];
         // Not nullable field
         $storageValue->sortKeyInt = (int)$row['ezcontentobject_attribute_sort_key_int'];
+        $storageValue->sortKeyFloat = (float)$row['ezcontentobject_attribute_sort_key_float'];
         $storageValue->sortKeyString = $row['ezcontentobject_attribute_sort_key_string'];
 
         $fieldValue = new FieldValue();
diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/Search/Gateway/SortClauseHandler/Field.php b/eZ/Publish/Core/Persistence/Legacy/Content/Search/Gateway/SortClauseHandler/Field.php
index ab21016..14d3762 100644
--- a/eZ/Publish/Core/Persistence/Legacy/Content/Search/Gateway/SortClauseHandler/Field.php
+++ b/eZ/Publish/Core/Persistence/Legacy/Content/Search/Gateway/SortClauseHandler/Field.php
@@ -81,10 +81,17 @@ class Field extends SortClauseHandler
                         $this->getSortTableName( $number )
                     ),
                     $column2 = $this->getSortColumnName( $number . "_bis" )
+                ),
+                $query->alias(
+                    $this->dbHandler->quoteColumn(
+                        "sort_key_float",
+                        $this->getSortTableName( $number )
+                    ),
+                    $column3 = $this->getSortColumnName( $number . "_bis_bis" )
                 )
             );
 
-        return array( $column1, $column2 );
+        return array( $column1, $column2, $column3 );
     }
 
     /**
diff --git a/eZ/Publish/Core/Persistence/Legacy/Content/StorageFieldValue.php b/eZ/Publish/Core/Persistence/Legacy/Content/StorageFieldValue.php
index 60cee9a..7c2adba 100644
--- a/eZ/Publish/Core/Persistence/Legacy/Content/StorageFieldValue.php
+++ b/eZ/Publish/Core/Persistence/Legacy/Content/StorageFieldValue.php
@@ -40,6 +40,13 @@ class StorageFieldValue extends ValueObject
      * @var int
      */
     public $sortKeyInt = 0;
+    
+    /**
+     * Float sort key.
+     *
+     * @var int
+     */
+    public $sortKeyFloat = 0;
 
     /**
      * Text sort key.

New column in the database :

ALTER TABLE `ezcontentobject_attribute`
    ADD COLUMN `sort_key_float` DOUBLE NOT NULL DEFAULT '0' AFTER `sort_key_string`,
    ADD INDEX `sort_key_float` (`sort_key_float`);

Updating current datas

 UPDATE `ezcontentobject_attribute` INNER JOIN `ezcontentclass_attribute` ON `ezcontentobject_attribute`.`contentclassattribute_id`=`ezcontentclass_attribute`.`id` SET `ezcontentobject_attribute`.`sort_key_float`=`ezcontentobject_attribute`.`data_float` WHERE `ezcontentclass_attribute`.`data_type_string`= 'ezfloat';
expandshrink

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

36 542 Users on board!

Forums menu

Proudly Developed with from