Valery Posted September 3, 2013 Share Posted September 3, 2013 Hi everybody, I have a setup where I have to assign one or more categories to a page. After spending some time wondering how I could make PW fields store lists, I decided upon keeping a serialized array in a page field (type = "text"). Here's how: $categories = array ("brushes", "watercolor", "pencils", "charcoal", "acrylics", "egg tempera",); $p = $pages->get(9999); $p->of(false); // the usual stuff... $p->category = serialize($categories); // transform the array of categories into a string $p->save(); unset ($categories); // wipe the slate clean for testing purposes // Now Let's Get 'em! $categories = unserialize($p->category); foreach ($categories as $c) echo $c . "<br />"; The array with the list of categories becomes a:6:{i:0;s:7:"brushes";i:1;s:10:"watercolor";i:2;s:7:"pencils";i:3;s:8:"charcoal";i:4;s:8:"acrylics";i:5;s:11:"egg tempera";} It can be stored in a page field and fetched as an array when needed. Neat! (Just remember to set MaxLength to a higher value if you intend to use "text" fields for this purpose.) Thanks for reading! 4 Link to comment Share on other sites More sharing options...
kongondo Posted September 3, 2013 Share Posted September 3, 2013 (edited) Thanks for the tip Valery. You've given me ideas about something I'm working on Btw, I believe PW stores some of its data this way? E.g. data column in the modules table? I could be wrong.. I'd like to hear more thoughts about serialize actually...anyone? Edited September 3, 2013 by kongondo Link to comment Share on other sites More sharing options...
adrian Posted September 3, 2013 Share Posted September 3, 2013 Hi Vallery, Did you think about using a Page fieldtype for this? That seems to be the PW way of assigning categories to a page. Set up a parent page, with child pages for each category. Then create a field with the "Page" fieldtype and in the input tab have it point to the parent of the category pages. Choose Asm select (or other) from the Input field type and you should be good to go. I get the feeling you are populating this from the front end? These fields can also be populated via the API, so no problem there. I am not completely sure of your goal, so maybe I am off base here, but I think this approach will give you more flexibility. A bit of a read on the pros/cons of serializing: http://www.mysqlperformanceblog.com/2010/01/21/when-should-you-store-serialized-objects-in-the-database/ 2 Link to comment Share on other sites More sharing options...
Valery Posted September 4, 2013 Author Share Posted September 4, 2013 Thanks, Adrian. I was not aware of the Page field until today, so thanks for this hint. I definitely like the way it allows you to link pages by fields without rearranging them hierarchically. However, I find this method more appropriate for large-scale structures of page relations, especially when they get updated often. My case is simple: plain list of post categories not longer than 20-30, and quite rarely updated. MySQL will live if I pull the serialized list out, update it and update every half a year Seriously though, those who are reading this topic should consider making use of the Page field for the purpose of linking pages by one or several fields. Iterating them is as simple as foreach ($page->page as $p) { $categories[] = $p->category; } 1 Link to comment Share on other sites More sharing options...
kongondo Posted September 4, 2013 Share Posted September 4, 2013 ...Seriously though, those who are reading this topic should consider making use of the Page field for the purpose of linking pages by one or several fields. Iterating them is as simple as foreach ($page->page as $p) { $categories[] = $p->category; } Yes, that's what most of us do . Reading your post I just assumed you knew and that this was for a different use case (good catch Adrian). Helpful resources about Page Fieldtypes are here: http://processwire.com/videos/page-fieldtype/ http://processwire.com/talk/topic/3579-tutorial-approaches-to-categorising-site-content/ I still like your method for some stuff I'm working on 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