daslicht Posted January 8, 2014 Share Posted January 8, 2014 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 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted January 9, 2014 Share Posted January 9, 2014 Your reading should start here. Link to comment Share on other sites More sharing options...
daslicht Posted January 10, 2014 Author Share Posted January 10, 2014 Thank you for your reply! thats where I found: $fields->save($field) so how would you solve the above mentioned thing ? I haven't fount an answer in the API ~Marc Link to comment Share on other sites More sharing options...
Martijn Geerts Posted January 10, 2014 Share Posted January 10, 2014 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(); 2 Link to comment Share on other sites More sharing options...
daslicht Posted January 10, 2014 Author Share Posted January 10, 2014 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 More sharing options...
Martijn Geerts Posted January 10, 2014 Share Posted January 10, 2014 @daslicht, Search your answer in here. This came up today. Actualy I still think "Your reading should start here." 1 Link to comment Share on other sites More sharing options...
daslicht Posted January 10, 2014 Author Share Posted January 10, 2014 Thank you ! I play with the api functions now. 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