leoric Posted November 25, 2013 Share Posted November 25, 2013 hi,everyone! i could add some pages in page field like this: $user->user_app->add($page1); $user->user_app->add($page2); $user->user_app->add($page3); ...etc $user->save(); now the issue is i dont know how to delete one page. thanks! Link to comment Share on other sites More sharing options...
leoric Posted November 25, 2013 Author Share Posted November 25, 2013 ok,i did it like this: $user->user_app->removeAll($page); its working, is it right? Link to comment Share on other sites More sharing options...
Soma Posted November 25, 2013 Share Posted November 25, 2013 I think this should work but you can also do $user->user_app->remove($page); 1 Link to comment Share on other sites More sharing options...
leoric Posted November 26, 2013 Author Share Posted November 26, 2013 I think this should work but you can also do $user->user_app->remove($page); hi ,soma before this i had tried to use "remove" ,but it does not work. so i change to "removeAll" and i found : if there have this pages in page field [Multiple pages (PageArray)]: page1 , page2, page 3..etc removeall() : will remove all pages => its worked removeall(page1): page1 will be removed. => its worked remove(page1): nothing changed.. => its not working Link to comment Share on other sites More sharing options...
Soma Posted November 26, 2013 Share Posted November 26, 2013 I just looked into this again and there's no argument for removeAll() just for remove($key). See here in source https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/core/WireArray.php#L790 removeAll() removes ALL pages from a Wire/PageArray (pages, images, POST vars etc). So I'm not sure about why it works for you because it shouldn't and also doesn't work for me. Assuming user_app is a page field allowing multiple pages to be selected. It's a WireArray/PageArray and you add pages to it like this from API: $user->of(false); $page1 = $pages->get("/somepage/"); $page2 = $pages->get("/someotherpage/"); $user->user_app->add($page1); $user->user_app->add($page2); $user->save(); Now we got 2 pages added to the page field. If you now do a $page->user_app->removeAll($page1) they all get removed so does $user->user_app->removeAll(); But if you do: $page2 = $pages->get("/someotherpage/"); $user->user_app->remove($page2); $user->of(false); $user->save(); Now only the $page2 which is in the array gets removed. It works all as expected and make sense. 6 Link to comment Share on other sites More sharing options...
leoric Posted November 27, 2013 Author Share Posted November 27, 2013 I just looked into this again and there's no argument for removeAll() just for remove($key). See here in source https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/core/WireArray.php#L790 removeAll() removes ALL pages from a Wire/PageArray (pages, images, POST vars etc). So I'm not sure about why it works for you because it shouldn't and also doesn't work for me. Assuming user_app is a page field allowing multiple pages to be selected. It's a WireArray/PageArray and you add pages to it like this from API: $user->of(false); $page1 = $pages->get("/somepage/"); $page2 = $pages->get("/someotherpage/"); $user->user_app->add($page1); $user->user_app->add($page2); $user->save(); Now we got 2 pages added to the page field. If you now do a $page->user_app->removeAll($page1) they all get removed so does $user->user_app->removeAll(); But if you do: $page2 = $pages->get("/someotherpage/"); $user->user_app->remove($page2); $user->of(false); $user->save(); Now only the $page2 which is in the array gets removed. It works all as expected and make sense. @soma ,your reply is very helpful... my fault: $app is a page id variable, not a $page object. $removepage = $pages->get($app); -> add this line $user->user_app->remove($removepage); ->change removeall to remove its working thank you very much! 1 Link to comment Share on other sites More sharing options...
Hari KT Posted February 24, 2014 Share Posted February 24, 2014 Hey in PW 2.4 I was having a PageArray field assigned to a template. And the operations like has, remove etc $page->page_array_field->has($id); $page->page_array_field->remove($id); If the value is integer it don't remove the value or find a value. But only passing a real page object removes the value from the db. Or is it still behaving for everyone else ? Link to comment Share on other sites More sharing options...
mcwhitey Posted October 19, 2017 Share Posted October 19, 2017 (edited) I'm having the same problem. Yet I can not find a solution. How do I change my integer into a real object? This is my code. According to the docs this should work. Right? $u->projectmanagers->add(1044); // THIS WORKS! $u->projectmanagers->remove(1044); // THIS DOESNT WORK! $u->save(); Only thing I can think of doing is first store the array, then remove all, then remove the one item from the array and then adding the array again. But I'd rather not Edited October 19, 2017 by mcwhitey Wanted to add something Link to comment Share on other sites More sharing options...
abdus Posted October 19, 2017 Share Posted October 19, 2017 You can add pages using page objects or page ids, but can only remove them using page objects. A bit inconsistent, to be honest. So you need to do $u->projectmanagers->remove($pages->get(1044)); From the core: // PageArray.php public function add($page) { // ... } else if(ctype_digit("$page")) { $page = $this->wire('pages')->get("id=$page"); if($page->id) { parent::add($page); $this->numTotal++; } } return $this; } public function remove($key) { // if a Page object has been passed, determine its key if($this->isValidItem($key)) { $key = $this->getItemKey($key); } if($this->has($key)) { parent::remove($key); $this->numTotal--; } return $this; } Edit: I created a feature request 2 Link to comment Share on other sites More sharing options...
mcwhitey Posted October 19, 2017 Share Posted October 19, 2017 Thanks Abdus, Can't say I fully understand but it does work now! Awesome! Good idea to submit a feature request Cheers! Link to comment Share on other sites More sharing options...
adrian Posted May 23, 2018 Share Posted May 23, 2018 https://github.com/processwire/processwire-requests/issues/198 2 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