Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Observer_Typing ignores selected fields
  • I have this model:
    class Model_Philhealth extends Orm\Model
    {
      protected static $_table_name = 'philhealth_contribution';
    // protected static $_properties = array(
    // 'id',
    // 'SalaryRange_From', 'SalaryRange_To'
    // );

        protected static $_observers = array(
            // This typing should be modified to format and return only the specified fields!
            'Orm\\Observer_Typing'
        );

        /**
         * @param $monthlyBasicPay
         * @param int $year                 date('Y')
         * @return bool|array
         */
    public static function getShare($monthlyBasicPay, $year = 2014)
        {
            // query() doesn't support column alias
            $share = static::query()
                ->select('EmployeeShare', 'EmployerShare', 'SalaryBase')
                ->where('year', $year)
                ->where('SalaryRange_From', '<=', $monthlyBasicPay) // $monthlyBasicPay >= SalaryRange_From
                ->where('SalaryRange_To', '>=', $monthlyBasicPay)   // $monthlyBasicPay <= SalaryRange_To
                ->get_one()
                ->to_array()
            ;
    print_r($share);die;
            // % - get from phic
            $premiumRate = 2.5;

            $share['Employee'] = ($share['SalaryBase'] * ($premiumRate / 100) / 2);
            $share['Employer'] = ($share['SalaryBase'] * ($premiumRate / 100) / 2);

            unset($share['SalaryBase']);
            unset($share['EmployeeShare']);
            unset($share['EmployerShare']);

    return $share;
    }
    }


    The result of print_r:
    Array
    (
        [id] => 13
        [SalaryBase] => 20000
        [EmployeeShare] => 250
        [EmployerShare] => 250
        [philhealth_id] => 
        [year] => 
        [SalaryBracket] => 
        [SalaryRange_From] => 
        [SalaryRange_To] => 
        [TotalMonthlyPremium] => 
    )

    If I disable Observer_Typing, it returns only the selected fields:
    Array
    (
        [id] => 13
        [SalaryBase] => 20000.00
        [EmployeeShare] => 250.00
        [EmployerShare] => 250.00
    )

    My question is, should it be that even if Observer_Typing is enabled or applied, it should only return the selected fields?


  • Here is what I did:

    In fuel\packages\orm\classes\model.php
    public function __unset($property)
    {
    if (array_key_exists($property, static::properties()))
    {
    // $this->_data[$property] = null;
    unset($this->_data[$property]);
    }
    elseif ($rel = static::relations($property))
    {
    $this->_reset_relations[$property] = true;
    $this->_data_relations[$property] = $rel->singular ? null : array();
    }
    elseif ($this->_get_eav($property, true, true))
    {
    // no additional work needed here
    }
    elseif (array_key_exists($property, $this->_custom_data))
    {
    unset($this->_custom_data[$property]);
    }
    }

    I replaced the line:
    $this->_data[$property] = null;
    with:
    unset($this->_data[$property]);

    In the model:

    protected static $_observers = array(
            // This typing should be modified to format and return only the specified fields!
            'Orm\\Observer_Typing',
            'formatResult' => array(
                'events' => array('after_load')
            )
        );


    then the formatResult:

    class formatResult
    {
        // it may have many methods and properties before or after the orm_notify method

        public static function orm_notify($instance, $event)
        {
            $select = array('EmployeeShare', 'EmployerShare', 'SalaryBase');

            foreach ($instance->properties() as $key => $value) {
                if (!in_array($key, $select)) {
                    unset($instance->$key);
                }
            }
        }
    }

    The print_r result:

    Array
    (
        [SalaryBase] => 20000
        [EmployeeShare] => 250
        [EmployerShare] => 250
    )

  • Or this:

    In fuel\packages\orm\classes\model.php
    public function __unset($property)
    {
        if (array_key_exists($property, static::properties()))
        {
    // $this->_data[$property] = null;
            unset($this->_data[$property]);
        }
        elseif ($rel = static::relations($property))
        {
            $this->_reset_relations[$property] = true;
            $this->_data_relations[$property] = $rel->singular ? null : array();
        }
        elseif ($this->_get_eav($property, true, true))
        {
            // no additional work needed here
        }
        elseif (array_key_exists($property, $this->_custom_data))
        {
            unset($this->_custom_data[$property]);
        }
    }

    In model:

    protected static $_observers = array(
            // This typing should be modified to format and return only the specified fields!
            'Orm\\Observer_Typing',
    //        'formatResult' => array(
    //            'events' => array('after_load')
    //        )
        );

    public static function getShare($monthlyBasicPay, $year = 2014)
        {
            // query() doesn't support column alias
            $share = static::query()
                ->select('EmployeeShare', 'EmployerShare', 'SalaryBase')
                ->where('year', $year)
                ->where('SalaryRange_From', '<=', $monthlyBasicPay) // $monthlyBasicPay >= SalaryRange_From
                ->where('SalaryRange_To', '>=', $monthlyBasicPay)   // $monthlyBasicPay <= SalaryRange_To
                ->get_one()
    //            ->to_array()
            ;

            // % - get from phic
            $premiumRate = 2.5;

            $share['Employee'] = ($share['SalaryBase'] * ($premiumRate / 100) / 2);
            $share['Employer'] = ($share['SalaryBase'] * ($premiumRate / 100) / 2);

            $select = array('EmployeeShare', 'EmployerShare', 'SalaryBase');

            foreach ($share as $key => $value) {
                if (!in_array($key, $select)) {
                    unset($share[$key]);
                }
            }

            return $share;
        }
  • Harro VertonHarro Verton
    Accepted Answer
    See my answer to your other question.

    The ORM doesn't support making a sub-selection of columns, it interferes with the mapping part of ORM.

    However, this being Fuel you can if you want to, but you have to know it's restrictions and side-effects, one of them you encounter here...

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion