1

I'm trying to create a new entry from within the database. The section is defined in craft_sections, and the entries for craft_elements, and craft_entries seem correct after the script is run, but no entries are displayed in the UI.

2
  • 3
    I'd suggest not doing it this way, and instead go through the Craft APIs for building out the Craft data structures. Commented Feb 12, 2020 at 20:56
  • I concur, and have shared your recommendation.
    – ponies
    Commented Feb 12, 2020 at 21:03

1 Answer 1

3

If you really, really want to create an entry from within the database (you must have a VERY good reason to do so), then you should create a row in:

  • entries
  • elements
  • elements_sites
  • content

If you'd like the easier path, use Content migrations. Create a migration, and then add something like:

// Create the entry
$myNewEntry = new craft\elements\Entry();
$myNewEntry->setFieldValues([
    'title' => 'My new entry',
    'myCustomFieldHandle' => 'Look, I can even set a custom field!',
    'myCustomCategoryFieldHandle' => [$categoryILookedUp->id],
]);

// Save the entry. Everything else will be handled for you by Craft's API.
Craft::$app->getElements()->saveElement($myNewEntry);
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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