Jump to content

Recommended Posts

Posted (edited)

Hi Guys,

I am looking for the best way what you feel when doing this. Basically the idea is to save the history of a user.

Consider we have a bunch of products, and categories. And the user needs a way to save a product and check it for later use. Consider the same as you follow a topic in the PW forum.

I was thinking to add a page which have 2 fields

1 ) user_id which is of type Page which is linked to the user template of PW 

2 ) product_id which is also a Page which I have built from another template

I have removed the global title, but it needs the url always entered.

$page = new Page();
$page->template = 'product-history-user';
$page->parent_id = 'parent-id';
$page->name = 'something';
$page->history_user_id = $user->id;
$page->user_history_product_id = 'id-of-product';
$page->save();

I also have some more stuffs that needs similar logic. Interested to hear your thoughts to make this better.

Update :

One more problem I missed to write is, I need to sort it based on the priority, that is why I opted the way.

or is there a way we can store the date and time of the product when saved?

Thanks

Edited by Hari KT
Posted

@Martijn Geerts Thank you.

What I was wondering is whether that will give you a pain when upgrading to new ProcessWire, when you are editing root templates.

Posted

When you upgrade Processwire the /wire directory get replaced.  The /site directory (where templates are stored) is never touched.

  • Like 2
Posted

ok. So I will move with adding Page field in user template.

Sorry I missed to write one more problem, I need to sort it based on the priority ?

and also store the date and time of the product when saved.

Thank you cstevensjr, Martijn Geerts

  • Like 2
Posted

Sidenote:

For storing data on Field 'Type of Page' (multiple):

$user->pages_field->add($a_page_object); // single (Page) page
$user->pages_field->import($page_array); // store (Wirearray) pages
  • Like 2
Posted

Martjin, add() works for both actually.

The user template has nothing to do with template files.

And as long as you don't touch wire folder you're fine upgrading.

Posted

Hi Soma,  Martjin,

Let me make the question more clear with the examples you mentioned.

We have Categories->Products ( x1, x2, x3, x4, x5, x6 )

Let us assume the user saves products in the order x4, x1 .

Now in the user template we have a field with user_product which is multi page array whose template is product. So when the user save the first one we need to track the time when the product is added.

$user->user_product->add($product1); // May be id or page object

// Now we need to know when the product1 is added

That means we need an extra field for the user to know at what time the product is saved, else we will not be able to keep track of the order in which the user has saved.

Posted

Then I would create a new template "user-order" add a single page field "product" and a datetime field to the template. Then create a new order with this template , and save it under the user page. I think you just need to allow children on the family settings on the user template.

Posted

Thanks @Soma, that was what I ended up and then the first question comes, "I have removed the global title, but it needs the url always entered.".

Seems like I want to set a datetime string format for the url.

Thank you for all the support every one have showed. Marking as solved.

  • Like 1
Posted

Every page in PW requires a "name", it's like a ID that makes the page accessible either via a view or API too. Nothing actually to worry about, just use a generic name.

Posted

Or set the name with a Unix timestamp the user has ordered the product.

It's not likely that the customer orders multiple products in a second so you don't need the date field.

Posted

@Soma, <qoute>Martjin, add() works for both actually.</qoute>

Maybe I love the different terms (plurals vs nouns) in scripting that much that I never tried to use add() with Wirearray :-)

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
×
×
  • Create New...