$entry = new \craft\elements\Entry(); $entry->sectionId = 1; $entry->typeId = 1; $entry->fieldLayoutId = 1; $entry->authorId = 1; $entry->enabled = true; $entry->setFieldValues([ 'title' => 'My Entry', 'body' => 'Lorem ipsum dolor sit amet' ]); if (Craft::$app->getElements()->saveElement($entry)) { // Entry saved successfully } else { // Error saving entry }
$entry = Entry::find() ->section('mySection') ->id(1) ->one(); if ($entry) { // Entry found } else { // Entry not found }
$file = UploadedFile::getInstanceByName('file'); $asset = new Asset(); $asset->title = $file->name; $asset->tempFilePath = $file->tempName; $asset->filename = 'my-file.jpg'; $asset->newFolderId = 1; if (Craft::$app->getElements()->saveElement($asset)) { // File saved } else { // Error saving file }This code uploads a file to the CMS. The `getInstanceByName` method retrieves the uploaded file by its name, and the `tempName` property stores the temporary file path. Package Library: Craft CMS Assets API.