1

I'm looking for a simple way to save form data to a database and be able to access that information. I'm not sure if I should use the custom fields or an entry (or a custom table) and if creating a module is the way to go or another easy way to have my form post submit handled with PHP.

I was hoping for a simple example of a module for Craft but I'm not finding it.

For the sake of an example, I'd like to add and edit books in my library:

Book Type:
BookTitle - 50 characters
BookAuthor - 50 characters
BookSummary - 300 characters
Pages - Integer

Thanks.

  • 1
    Please read this and tell me if you would like to use records or elements to store your data. I'll show you the code depending on your choice/needs craftcms.stackexchange.com/questions/23859/… – Robin Schambach Apr 3 '18 at 19:18
  • Seems like elements is the way to go since I believe the customers will need access to the table. However, there's going to be quite a bit of coding anyways so we may go with records since everything will be customized. Without seeing the difference, I would go with elements. – Aaron Jay Lev Apr 4 '18 at 20:27
1

Based on the information in the comments I'll provide an example how to save elements in Craft. I'll just show an example with an existing element type Entry. You can see how to create a custom element type well documented in the docs

You'll create a new section in your CP and add the required fields to the layout. In your case you'll need title (Booktitle), author (BookAuthor), summary (BookSummary) and pages (Pages)

// get the section and the entrytype
// when you know your section id and entrytype id you don't
// need this. You can get the Ids by looking at the URL in the
// when you edit them
$section = Craft::$app->sections->getSectionByHandle('books');
$entryType = reset($section->getEntryTypes());
// Create an entry
$entry = new Entry([
    'sectionId' => $section->id,
    'typeId' => $entryType->id, // or insert the required type id manually
    'fieldLayoutId' => $entryType->fieldLayoutId,
    'authorId' => 1,
    'title' => 'A cool title of a book',
    'slug' => 'a-cool-title-of-a-book', // <- not required
]);

$entry->setFieldValues([
    'author' => 'Me and Myself',
    'summary' => 'it was awesome',
    'pages' => 9000
]);

// Save the entry
Craft::$app->elements->saveElement($entry);

// in case there is something wrong..
$errors = $entry->getErrors();

When you are going to create your custom element type without a field layout your code could look like this

$book = new Book([
    'title' => 'A cool title of a book',
    'author' => 'Me and Myself',
    'summary' => 'it was awesome',
    'pages' => 9000
]);
// Save the entry
Craft::$app->elements->saveElement($book);

// in case there is something wrong..
$errors = $book->getErrors();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.