majkiris Posted November 30, 2014 Share Posted November 30, 2014 Hi everyone!I need some help with a module that saves sum of integer fields as a hidden field value.There are 3 integer fields in template: rating1, rating2, rating3 and one hidden field: total.Can't figure out, how to save the field value to DB...I tried use $field->set and $fields->save, but all what i get is Call to a member function save() on a non-object error. <?php class TotalRating extends WireData implements Module { /** * @return array */ public static function getModuleInfo() { return array( 'title' => 'Total rating', 'version' => 100, 'summary' => '', 'singular' => true, 'autoload' => true, ); } public function init() { $this->pages->addHookAfter('save', $this, 'total'); } public function total($event) { $page = $event->arguments[0]; if ($page->template == "cat") { $rat1 = $page->rating1; $rat2 = $page->rating2; $rat3 = $page->rating3; $ratTotal = $rat1 + $rat2 + $rat3; //save $ratTotal as "total" field value } } } Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted November 30, 2014 Share Posted November 30, 2014 Welcome to the friendly forums of ProcessWire dear Majkiris! Check this out. It will probably help with you in your quest. Link to comment Share on other sites More sharing options...
adrian Posted November 30, 2014 Share Posted November 30, 2014 I think what you are looking for is: $page->save("total"); If you did just $page->save() you'd end up in a loop because your hook would get called again each time it saves itself. EDIT: Oh and in case you haven't figured it out, you need to set the field value first: $page->total = $ratTotal; 4 Link to comment Share on other sites More sharing options...
majkiris Posted November 30, 2014 Author Share Posted November 30, 2014 Thank you adrian! Of course it works. I swear to my life that i did tried that..but it didn't work (maybe i used $page->save($total)). Welcome to the friendly forums of ProcessWire dear Majkiris!Check this out. It will probably help with you in your quest. Thank you for the link Ivan. I will dive into it later. 2 Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted November 30, 2014 Share Posted November 30, 2014 My bad. I thought it is out of scope error (which probably happened when majkris was performing $fields->save if I got it right). I am glad Adrian helped us here. But I want to take something out of this for myself too . As I understand, $page = $event->arguments[0]; sets $page variable in the scope of the class method. But I do not know what is $event and its arguments. Where do they come from? I would be happy to get an explanation or a link to documentation article/forum post. 1 Link to comment Share on other sites More sharing options...
adrian Posted November 30, 2014 Share Posted November 30, 2014 $fields->save() would save details about that field - various settings etc, rather than the contents of the field on that particular page. $event is coming from the event that is hooked, so in this case, @majkiris is hooking page save, so by getting the first argument from the save event, he is getting the page object. You could also do: $page = $event->argumentsByName("page"); The first argument, eg arguments[0] won't always be the page object, it depends on the hook. Sometimes, for example, it might be the width of an image. Hope that helps a little. 3 Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted November 30, 2014 Share Posted November 30, 2014 I'll dive into it )) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now