Jump to content

Update object in a Hook method


stopher
 Share

Recommended Posts

Hi all,

 

i have a problem with one of my modules, i can't update one object field value in the hook method, as you can see below

public function hookActionValidateOrder($params){

$params['order']->customerRefOrder = $params['cart']->customerRefOrder;
$params['order']->update();
}

With this code, nothing is updated .

However my variable "$params['cart']->customerRefOrder" contain a value.

But the update method seem not to work in my case . :(

Have you got any idea to help me to resolve my issue?

Thank you

Ch.

Share this post


Link to post
Share on other sites

Hello

thank you for your answer,

 

no, no error .

 

Note: i use v 1.6.0

 

I investigated this issue, and i remarked that the method "getFields()" in Object Model class not return my added fields in overridden class declaration.

 

In fact my added fields disappear when i use the "update()" method in my Hook.

 

To patch this problem i have to create a simple method in my overridden class ( see the code below )

class Order extends OrderCore{

    public $customerRefOrder;

    public function __construct($id = null, $id_lang = null){
   
        self::$definition['fields']['customerRefOrder'] =  array('type' => self::TYPE_STRING);
        parent::__construct($id, $id_lang);
     }

    //My patch 
    public function patchNewFields(){
        $this->def['fields'] = array_merge($this->def['fields'], self::$definition['fields']);
    }
}

Now in my Hook method i call my method "patchNewFields",

public function hookActionValidateOrder($params){
        
    $params['order']->patchNewFields();//Patch 
    $params['order']->customerRefOrder = $params['cart']->customerRefOrder;
    $params['order']->update();
        
}

And it works but i don't think that is a normal situation.

 

 

And to complete my investigation, i noticed different results depending on hook used. As exemple :

public function hookActionValidateOrder($params){
      
    $order - new Order(10);  
    $order->customerRefOrder = "foo";
    $order->update();//Not work
        
}

The code above not work ( no error )

public function hookDisplayAdminOrder($params){
      
    $order - new Order(10);  
    $order->customerRefOrder = "foo";
    $order->update();//Works
        
}

The code above works fine

public function hookActionObjectOrderAddAfter($params){

$order - new Order(10);
$order->customerRefOrder = "foo";
$order->update();//Not work

}

The code above not work ( no error )

Edited by stopher (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

  • 8 months later...

So you are trying to add a field dynamically to the object?

 

How does the value get saved in the database? Did you add another column?

 

I don't really follow what you are doing in the code.

 

Not really ,

 

the new fields declared inside the constructor are not inside the result of getFields() method .

 

2 solutions :

  1. Call a "patch" method before update object .
  2. Copy all definition fields ( and add the new felds ) inside the overrided class, don't use a declaration inside the constructor. exemple

 

This comportement is very strange, so i decided to use the second method by default. Now all works fine.

 

Regards,

Ch.

Share this post


Link to post
Share on other sites

  • 5 years later...
On 7/30/2015 at 9:48 PM, stopher said:

I investigated this issue, and i remarked that the method "getFields()" in Object Model class not return my added fields in overridden class declaration.

In fact my added fields disappear when i use the "update()" method in my Hook.

I just stumbled upon this issue (in PS 1.6.1.20) after losing so much time trying to figure out what was happening. Sad this was never fixed in PS 1.6

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More