Jump to content

Managing custom data in PW


TomPich
 Share

Recommended Posts

Hello to all,
So I just finished my third website with PW and I love it more and more. These were very simple sites, though.
I would like now to explore deeper how to create modules for specific tasks, and I need some guidelines from you guys, as I don’t really know where to start from.

Let take this case : I had, a few months ago, to do a website that has a public list of physiotherapists. Each member has some basic data. The client must be able to update the data from the admin side and add a picture.
On the front, there is a main page with a list of all practitioners and each practitioner has their own page.
All the initial data were exported from their previous Joomla website in a CSV file + picture for each member.
I know how to import a CSV file content in a PHP array, and how to import the CSV data in a specific table in the database, so I can go either way.
I was able to implement all that on WordPress (but I decided to quit WP).

My goals – learn how to :

  • import these data in PW in an automated way.
  • make a specific admin page that allow CRUD operation on these practitioners.
  • easily export this list when needed, in a convenient format (CSV would be the best).

I guess this use case must be fairly common (importing existing data and managing them from PW admin).

My questions :

  1. What would be, according to you, the best way to go : use a dedicated table in PW database (option A), or create a page for each practitioner (total of 50-100 practitioners) (option B) ? The last option sounds like making data export more complicated, but would benefit from PW native functionalities.
  2. Which docs / posts / tutorials would you recommand for a starting point ? I saw some stuff, but I still feel confused where to start from. In the meantime, I will start with ProcessHello module from Ryan.

Thank you guys, for your time and your help. Even short answers would be greatly appreciated. ?

Link to comment
Share on other sites

Hello,

46 minutes ago, TomPich said:

What would be, according to you, the best way to go : use a dedicated table in PW database (option A), or create a page for each practitioner (total of 50-100 practitioners) (option B) ? The last option sounds like making data export more complicated, but would benefit from PW native functionalities.

I would use PW pages with a "practitioner" template.
Data export won't be more complicated, you can use PW API for that. If it needs to be optimized you can use several options like $pages->findMany(), findJoin(), findRaw() or even use raw MySQL query.
That way your practitioners are already editable from admin without effort.

Also check custom page classes if you need to attach your own class to this practitioners pages.

46 minutes ago, TomPich said:

Which docs / posts / tutorials would you recommand for a starting point ? I saw some stuff, but I still feel confused where to start from. In the meantime, I will start with ProcessHello module from Ryan.

It depends on your actual knowledge on PW, PW has a good documentation, you can start by looking Pages and Page. First one is useful to find your practitioners pages, second one to access a single page or create a new one.

  • Like 2
Link to comment
Share on other sites

One important thing to add: when exporting your pages, don't forget to disable output formatting before to read properties on pages: $myPage->of(false);
So you're reading data as it was entered in database, without applying formatter defined in admin on this field.


And when importing data, an easy optimization is to use database transaction:

try {

    wire()->database->beginTransaction();
    
    // Import data (creating and updating pages)

    wire()->database->commit();

} catch (Exception|WireException $e) {

    wire()->database->rollBack();
}

For 100 pages, you won't really need optimization.

  • Like 1
Link to comment
Share on other sites

Thank you @da²

So the actual role of the module would just be to handle the practitioner pages in a more convenient way than standard pages (like showing each page data in a line of a table) using PW API.
Sounds interesting, and not too complicated. I will dig deeper in that direction in the coming days.

  • Like 1
Link to comment
Share on other sites

2 hours ago, TomPich said:

easily export this list when needed, in a convenient format (CSV would be the best).

You might want to use the BatchChildEditor module for this.

  • Like 1
Link to comment
Share on other sites

4 hours ago, TomPich said:

My goals – learn how to :

  • import these data in PW in an automated way.
  • make a specific admin page that allow CRUD operation on these practitioners.
  • easily export this list when needed, in a convenient format (CSV would be the best).

I guess this use case must be fairly common (importing existing data and managing them from PW admin).

My questions :

  1. What would be, according to you, the best way to go : use a dedicated table in PW database (option A), or create a page for each practitioner (total of 50-100 practitioners) (option B) ? The last option sounds like making data export more complicated, but would benefit from PW native functionalities.
  2. Which docs / posts / tutorials would you recommand for a starting point ? I saw some stuff, but I still feel confused where to start from. In the meantime, I will start with ProcessHello module from Ryan.

For your goals:

$files->getCSV() - https://processwire.com/api/ref/wire-file-tools/get-c-s-v/

Images are a little trickier but if you know where they are stored on the server there's just a little post processing required.

https://processwire.com/modules/process-page-field-select-creator/ - then populate the child template with fields you need editing

As said above, turn off formatting, then you can iterate through using $pages->findRaw() and implode(), etc.

https://processwire.com/api/ref/pages/find-raw/

Write to files using $files->putContents();

https://processwire.com/api/ref/wire-file-tools/file-put-contents/

Using a dedicated table breaks one of the features of PW vs WP - every field is a table!

https://processwire.com/blog/posts/making-efficient-use-of-fields-in-processwire/

That being said, if you wanted to create a special field that compounded a bunch of related values together, you could look at this module:

https://processwire.com/modules/fieldtype-events/

If you are looking for a tutorial that includes creating a custom class to store your value, a more advanced overview is here:

 

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...

So, @da², @MarkE, @gornycreative, thanks to you guys, I am able to do what I aimed for.
Once again, I’m baffled with ProcessWire API smartness and simplicity.
Thanks you again, guys.

  • 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...