Jump to content

Having Problem With Callback Method Names On Event Triggering


Recommended Posts

Posted

I am having problems with triggering the method unless I name the method name run.

 

For example I have this code:

public function getEvents() {
	return array(
		array(
			'event' => "Transactions.add",
			'callback' => array("this", "notify")
		),
		array(
			'event' => "Transactions.edit",
			'callback' => array("this", "notify_edit")
		)			
	);
}

public function notify($event) {
	
	// Get params
	$params = $event->getParams();
	
	// Set transaction id
	$transaction_id = $params['transaction_id'];
	
	if (!isset($this->Transactions)) {
		Loader::loadModels($this, array("Transactions"));
	}
	
	// Now get transaction details
	$transaction_details = $this->Transactions->get($transaction_id);
	
	file_put_contents(dirname(__FILE__) . '/output.txt', print_r($transaction_details, true));
	
}

public function notify_edit($event) {
			
	// Get params
	$params = $event->getParams();
	
	// Set transaction id
	$transaction_id = $params['transaction_id'];
	
	if (!isset($this->Transactions)) {
		Loader::loadModels($this, array("Transactions"));
	}
	
	// Now get transaction details
	$transaction_details = $this->Transactions->get($transaction_id);
	
	file_put_contents(dirname(__FILE__) . '/output_edit.txt', print_r($transaction_details, true));
	
}	

This will not trigger the notify method at all; however if i change the method name to run it will trigger it.

 

Another problem is I cannot get the Transactions.edit method to run. I even changed the name of the method to run but it never would trigger.

 

Help!

Posted
public function getEvents() {
	return array(
		array(
			'event' => "Transactions.add",
			'callback' => array("this", "notify")
		),
		array(
			'event' => "Transactions.edit",
			'callback' => array("this", "notify_edit")
		)			
	);
}

 

Your events define a callback array with the string "this". I assume you're using this to register events, in which case the first index should be a reference to the object whose method (second index) is to be called.

e.g.

array(
	'event' => "Transactions.edit",
	'callback' => array($this, "notify_edit")
)
Posted

 

Your events define a callback array with the string "this". I assume you're using this to register events, in which case the first index should be a reference to the object whose method (second index) is to be called.

e.g.

array(
	'event' => "Transactions.edit",
	'callback' => array($this, "notify_edit")
)

 

Well from what I understood from the docs was that by doing something like what I am doing it should use the callback of $this->notify or $this->notify_edit as it works when I name the method "run" as that is $this->run no?

Posted

Which docs are you referring to? The callback specifies the "notify" and "notify_edit" methods, but does not specify from which class those would be called.

Posted

Looks like you're correct, the string "this" is supposed to automatically trigger an object instance from that class.

 

Are you sure your callback is not being called? How are you evaluating whether the method was called? They will only be called if a transaction was successfully added or edited.

Posted

Looks like you're correct, the string "this" is supposed to automatically trigger an object instance from that class.

 

Are you sure your callback is not being called? How are you evaluating whether the method was called? They will only be called if a transaction was successfully added or edited.

 

So I don't need to register it first?

 

Yes I'm sure they aren't being triggered. What I'm doing is I'm recording a payment for a client via admin which adds it to the transactions - if I call the method "run" then it will trigger it (which I can tell as it updates the output.txt file; if I change the name of the method and run that scenario again, the file isn't updated. Same goes for editing a transaction via admin - no creation of output_edit.txt.

Posted

Ok, figured the problem out.

 

If you make any changes to the plugin AFTER you install it, you need to first UNINSTALL it and then INSTALL it again.

or you should make the change in your upgrade function and upgrade the plugin from plugins tab .

Posted

I'm not sure how that would work? Isn't that more for database changes etc?

with upgrade function , you shoukd register the new event and delete the old event .

look at support manager plugin to insipre the how the upgrade work .

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...