Jump to content

Saving Data to Database


daslicht
 Share

Recommended Posts

Hello,

what is the proper way to save data to the database in PW?

Lets say I create a template file  which contains a form and I like to save some data to the database while using the PW templates  (data templates not visual) with its fields

Is the $fields->save($field) meant to be used for such kind of things ?

How about more complex thing like the following:

Lets say we have the following tables:

User //the users

Tags //the tags itself

Tag-relations //stores the tag relations, contains references to document_id and the tag_id

Documents // some kind of document such as pdf's, has a relation to a user

How would I add tags to documents, while documents belong to a user ?

Cheers

Marc 

  • Like 1
Link to comment
Share on other sites

Look at the bottom of the page: http://processwire.com/api/variables/pages/

Most likely, you want to save more data, a way to save is below. If you're doing it the API way, be sure that all data that gets stored is sanitized.

$parent = $pages->get('/about/');

$p = new Page(); // create new empty page object
$p->template = 'a template'; // required
$p->parent = $parent // required 
$p->title = 'My Page'; 

$p->save(); // save also creates a folder. That folder is needed if you use files/images in that template.

// Only need this step if you want to add images|files
$p->image = 'path/to/image.jpg'; 	// single image field
$p->images->add('path/to/image1.jpg'); 	// multiple images field

$p->save();
  • Like 2
Link to comment
Share on other sites

Thank you very much for your extensive answer!

Looks great !

But how would I save things with the relations I mentioned above, please :

User //the users

Tags //the tags itself

Tag-relations //stores the tag relations, contains references to document_id and the tag_id

Documents // some kind of document such as pdf's or the image from your example, has a relation to a user

          How would I add tags to documents, while documents belong to a user ?

 

~Marc

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