Jump to content

Storing Lists in PW fields


Valery
 Share

Recommended Posts

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!

  • Like 4
Link to comment
Share on other sites

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 by kongondo
Link to comment
Share on other sites

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/

  • Like 2
Link to comment
Share on other sites

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;    
} 
  • Like 1
Link to comment
Share on other sites

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

  • Like 2
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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