AdamP
AdamP

Reputation: 73

Set::extract CakePHP 2.x equivalent Hash method?

I'm upgrading a CakePHP framework from 1.3 to 2.4.3.

As set is now deprecated, I've run into issues with the following syntax:

$parentRateGroup = Set::extract('/.[equip_type_id=1]', $parentRates);

This works with Set, but doesn't work with the equivalent Hash method, i.e.

$parentRateGroup = Hash::extract($parentRates, '/.[equip_type_id=1]'); 

I've tried numerous things, the only way I could solve it was by manually looping through the array and matching by key.

Here's a snippet of the $parentRates array. What makes it difficult is the fact there is no parent key to group the array.

    array(57) {
  [0]=>
  array(21) {
    ["id"]=>
    string(2) "93"
    ["equip_type_id"]=>
    string(1) "1"
    ["rate_group_id"]=>
    string(1) "3"
    ["price_effective_date"]=>
    string(10) "0000-00-00"
    ["price"]=>
    string(5) "85.50"
    ["operator_pay"]=>
    NULL
    ["inherit_from_parent"]=>
    string(5) "Fixed"
    ["inherit_percentage"]=>
    NULL
    ["is_poa"]=>
    bool(false)
    ["is_from"]=>
    bool(false)
    ["rental_unit_id"]=>
    string(1) "2"
    ["equip_min_hire_notes"]=>
    string(31) "Min hire 5 hrs + 1 hr transport"
    ["equip_type_size_grouping_id"]=>
    NULL
    ["size_id_min"]=>
    NULL
    ["size_id_max"]=>
    NULL
    ["RentalUnit"]=>
    array(2) {
      ["id"]=>
      string(1) "2"
      ["name"]=>
      string(8) "per hour"
    }
    ["SizeGrouping"]=>
    array(0) {
    }
    ["SizeMin"]=>
    array(0) {
    }
    ["SizeMax"]=>
    array(0) {
    }
    ["EquipTypesRateGroupAttribute"]=>
    array(10) {
      [0]=>
      array(14) {
        ["id"]=>
        string(3) "658"
        ["equip_type_rate_group_id"]=>
        string(2) "93"
        ["equip_type_attribute_id"]=>
        string(1) "1"
        ["price"]=>
        NULL
        ["operator_pay"]=>
        NULL
        ["inherit_from_parent"]=>
        string(5) "Fixed"
        ["inherit_percentage"]=>
        string(4) "0.00"
        ["is_poa"]=>
        bool(false)
        ["is_from"]=>
        bool(false)
        ["is_combination"]=>
        bool(false)
        ["rental_unit_id"]=>
        NULL
        ["equip_min_hire_notes"]=>
        string(0) ""
        ["EquipTypeAttribute"]=>
        array(6) {
          ["id"]=>
          string(1) "1"
          ["equip_type_id"]=>
          string(1) "1"
          ["attribute"]=>
          string(3) "4x4"
          ["is_active"]=>
          bool(true)
          ["show_in_rate_group"]=>
          bool(true)
          ["stock_code"]=>
          string(2) "BH"
        }
        ["RentalUnit"]=>
        array(0) {
        }

Any ideas?

Upvotes: 1

Views: 1043

Answers (1)

ADmad
ADmad

Reputation: 8100

Hash::extract() does not support providing extract path as xpath (The path you have shown in your example is in xpath format). It only supports providing path in same format as Set::classicExtract().

So your options are to either update your code or keeping using Set::extract(). Even though it's deprecated it will be still available in 2.x.

Upvotes: 1

Related Questions