INVOICE_AFTER_INSERT or INVOICE_BEFORE_INSERT

Discussion in 'Troubleshooting' started by mylkhead, Jan 29, 2013.

  1. mylkhead

    mylkhead New Member

    Joined:
    May 28, 2010
    Messages:
    16
    For some reason I'm not able to get the current Invoice object in my hook. I must be doing something wrong. Here's my code in my plugin bootstrap:

    PHP:
    public function onInvoiceAfterInsert(Am_Event $event)
        {
            
    $invoice $event->getReturn();
            
    $inv_params $invoice->public_id "-" $invoice->invoice_key;
            
    $this->getDi()->errorLogTable->log($inv_params);
        }
    But all it prints is the hyphen, as if the public_id and invoice_key are empty. Maybe there are properties in this event I can use to fetch the Invoice object and then access methods instead?

    I appreciate your guidance, thank you.
  2. mylkhead

    mylkhead New Member

    Joined:
    May 28, 2010
    Messages:
    16
    Nevermind, I figured it out.
    PHP:
    $event->getInvoice();
    But I don't know what makes that method available. I don't see an method or property in the Event class called invoice. For the future, where is a good place to look to know what the returned $event might be? It does not seem to return the Invoice object explicitly. Thanks.
  3. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    That method is dynamic.
    have a look to Am_Event::_call :
    PHP:
        public function __call($name$arguments)
        {
            if (
    strpos($name'get')===0)
            {
                
    $var lcfirst(substr($name3));
                if (!
    array_key_exists($var$this->vars))
                {
                    
    $id $this->getId();
                    
    trigger_error("Event variable [$var] is not set for [$id]"E_USER_WARNING);
                    return 
    null;
                }
                return 
    $this->vars[$var];
            }
            
    trigger_error("Method [$name] does not exists in " __CLASS__E_USER_ERROR);
        }

  4. mylkhead

    mylkhead New Member

    Joined:
    May 28, 2010
    Messages:
    16
    Oh, ha - pretty clever! So can we use $event->get(ClassName) to get any object that called the event and included itself in the payload?

    Also, could you please recommend a good way to connect event hooks to plugin controllers? Because I have only been able to catch them with the Bootstrap and am not sure the best way to have the Controller notified.

    Thanks a lot for your time Alexander.

Share This Page