Jump to content

Having Problem With Callback Method Names On Event Triggering


gutterboy

Recommended Posts

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!

Link to comment
Share on other sites

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")
)
Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

It was this one: http://docs.blesta.com/display/dev/Plugin+Events

 

But I just had a look at: http://docs.blesta.com/display/dev/Creating+Events and it seems more clear now. So basically I will need to register the callbacks to that event so they get triggered?

 

As for specifying the class, I thought that what was the "this" referred to, so it was a reference to "$this".

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...