Jump to content

Recommended Posts

Posted

Hi guys,

here I am again with another question.

I have a template (name="comments") who has inside an integer field (name="totale_indice_01").

I have another template (name="single-comment") who has inside - among other fields - an integer field (name="indice_01"). This template is associated to several pages.

I would like, on every save page, to sum up the value of the field "indice_01" and save that value to the field "totale_indice_01".

I'm trying to insert this code:

$pages->addHook('saveReady', function($event) {
  $pages = $event->object; 
  $page = $event->arguments(0); 
  	$tot_indice01 = $pages->get('/comments/')->totale_indice_01;
  if($page->template == 'single-comment') {
  	  $total = 0;
      $total += $page->$indice_01;
      $tot_indice01 = $total;
  }
}); 

inside site/admin.php with no luck :(

Could anyone point me in the right direction? :)

 

Thanks!

Posted

I'm not sure how you want the result to look like, but this is adding the number to the total each time you're saving a page, meaning I'll just get more and more on each save. If you really want to have a sum of those field's you'd need load all instances each time and sum them up to get a (probably more useful) value.

10 minutes ago, 3fingers said:

$total += $page->$indice_01; 

Also here's a $ to much after the ->.

  • Like 1
Posted
2 minutes ago, LostKobrakai said:

I'm not sure how you want the result to look like, but this is adding the number to the total each time you're saving a page, meaning I'll just get more and more on each save. If you really want to have a sum of those field's you'd need load all instances each time and sum them up to get a (probably more useful) value.

Also here's a $ to much after the ->.

My typo, right.

But how would I get all instances each time? Kinda stuck :(

Posted

Think you're adding a hook not listening for one.  

$pages->addHookAfter or the before are listeners. Before or after shouldn't matter as they are both before the save.

  • Like 1
Posted

I've changed my code into this:

$pages->addHookAfter('saveReady', function($event) {
  $pages = $event->object; 
  $page = $event->arguments(0); 
  $tot_indice01 = $pages->get('/comments/')->totale_indice_01;
  if($page->template == 'single-comment') {
  	  $total = 0;
      $total += $page->indice_01;
      $tot_indice01 = $total;
  }
}); 

But still no luck.

Field "totale_indice_01" is still blank...

Posted
$pages->addHook('saved', function(){
	$pages = $event->object;
	$page = $event->arguments(0);
	if($page->template != 'single-comment') return;
	$total = array_sum($pages->find("template=single_comment, indice_01!=''")->explode('indice_01'));
	$pages->get('/comments/')->setAndSave('totale_indice_01', $total);
});

Just keep in mind that this won't scale well. It's loading all those pages at once with this. If you need a more scaleable alternative take a look at the new findMany in 2.8/3.0 or maybe even raw mysql counts/sums.

  • Like 1
Posted

Thanks LostKobrakai, even though I had to add $event as a parameter to your function, your code still doesnt work, and it's very strange because it looks like it should.

I'm going to post some screenshot in a minute.

Edit:

tree.png

comments.png

single-comment.png

Posted

Ok, we got it!

Final code:

$pages->addHookAfter('saved', function($event){
	$pages = $event->object;
	$page = $event->arguments(0);
	if($page->template != 'single-comment') return;
	$total = array_sum($pages->find("template=single-comment, indice_01!=''")->explode('indice_01'));
	$pages->get('/comments/')->setAndSave('totale_indice_01', $total);
});

Basically I had to insert "$event" as parameter of the function and correct an "_" to "-" in the template declaration :)

Super-many-giga thanks to LostKobraKai, you are my new hero for today :)

  • Like 1

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...