Body: <pre><code>// Upload and save as temp file
  $request = Craft::$app-&gt;request;
  $file = UploadedFile::getInstanceByName('file');
  $tempPath = $file-&gt;saveAsTempFile();
  
  // Create as permanent asset
  $folder = Craft::$app-&gt;getAssets()-&gt;findFolder(['id' =&gt; 5]);
  $asset = new Asset();
  $asset-&gt;tempFilePath = $tempPath;
  $asset-&gt;filename = Assets::prepareAssetName($file-&gt;name);
  $asset-&gt;newFolderId = $folder-&gt;id;
  $asset-&gt;volumeId = $folder-&gt;volumeId;
  $asset-&gt;avoidFilenameConflicts = true;
  $asset-&gt;setScenario(Asset::SCENARIO_CREATE);
  Craft::$app-&gt;getElements()-&gt;saveElement($asset);
  
  // Save ticket to db
  $ticket = new ErrorReportingRecord();
  $ticket-&gt;description = $description;
  ...
  $ticket-&gt;assetId = $asset-&gt;id;
  ...
  $ticket-&gt;save();
  </code></pre>
  <p>The problem is <code>$ticket-&gt;assetId = $asset-&gt;id;</code>. When <code>devMode</code> is <code>true</code> the asset id is stored in the db table as expected (<code>assetId int(11) DEFAULT NULL</code>). But when <code>devMode</code> is <code>false</code> the value of <code>assetId</code> in the table always becomes <code>0</code>.</p>
  <p>I should note that everything works on both my local machine with <code>devMode</code> set to both <code>true/false</code> as well as on the staging server which is identical to the production server, yet somehow the production server does not work. <code>return $asset-&gt;id</code> on production returns the id, so I don't understand why <code>0</code> is saved to the db.</p>
  <p>If I do</p>
  <pre><code>return $asset-&gt;id;
  $ticket-&gt;assetId = $asset-&gt;id;
  </code></pre>
  <p>on production (with <code>devMode false</code>) as a test, it returns the correct id, however removing <code>return</code> and running again still saves <code>$ticket-&gt;assetId</code> as <code>0</code>.</p>
  <p>I'm genuinely baffled by this.</p>
  <p>When <code>devMode</code> is <code>false</code> no logs are produced when running the code.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-08-04T15:06:56.577Z
Comments.Text: Is the asset being saved correctly? On your ``saveElement`` call, change it to``$assetService = Craft::$app->getElements()->saveElement($asset);`` Then add something like ``if (! $assetService) { throw new \Exception("Couldn't save asset: " . print_r($assetService->getErrors(), true)); }``
Comments.UserDisplayName: RitterKnight
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-08-06T10:49:59.597Z
Comments.Text: Now I'm even more baffled. I used your code suggestion, no error was thrown and everything is working. However the only difference between your code and mine is that I had no exception included. So I reverted back to my code and it still works! (But I'm keeping your version with the exception for the future). I just don't understand why it wasn't working but is now.
Comments.UserDisplayName: Stian Karlsen
Tags: <plugin-development><mysql>
Title: Saving to db with devMode true works, but not when false
ThreadText: # Saving to db with devMode true works, but not when false
  
  (Asked by: username_0 on 2020-08-04)
  
  ```
  // Upload and save as temp file
  $request = Craft::$app->request;
  $file = UploadedFile::getInstanceByName('file');
  $tempPath = $file->saveAsTempFile();
  
  // Create as permanent asset
  $folder = Craft::$app->getAssets()->findFolder(['id' => 5]);
  $asset = new Asset();
  $asset->tempFilePath = $tempPath;
  $asset->filename = Assets::prepareAssetName($file->name);
  $asset->newFolderId = $folder->id;
  $asset->volumeId = $folder->volumeId;
  $asset->avoidFilenameConflicts = true;
  $asset->setScenario(Asset::SCENARIO_CREATE);
  Craft::$app->getElements()->saveElement($asset);
  
  // Save ticket to db
  $ticket = new ErrorReportingRecord();
  $ticket->description = $description;
  ...
  $ticket->assetId = $asset->id;
  ...
  $ticket->save();
  
  ```
  
  The problem is `$ticket->assetId = $asset->id;`. When `devMode` is `true` the asset id is stored in the db table as expected (`assetId int(11) DEFAULT NULL`). But when `devMode` is `false` the value of `assetId` in the table always becomes `0`.
  
  I should note that everything works on both my local machine with `devMode` set to both `true/false` as well as on the staging server which is identical to the production server, yet somehow the production server does not work. `return $asset->id` on production returns the id, so I don't understand why `0` is saved to the db.
  
  If I do
  
  ```
  return $asset->id;
  $ticket->assetId = $asset->id;
  
  ```
  
  on production (with `devMode false`) as a test, it returns the correct id, however removing `return` and running again still saves `$ticket->assetId` as `0`.
  
  I'm genuinely baffled by this.
  
  When `devMode` is `false` no logs are produced when running the code.
  
  **Tags:** plugin-development, mysql
  
  > **Comments:**
  > **username_1**: Is the asset being saved correctly? On your ``saveElement`` call, change it to``$assetService = Craft::$app->getElements()->saveElement($asset);`` Then add something like ``if (! $assetService) { throw new \Exception("Couldn't save asset: " . print_r($assetService->getErrors(), true)); }``  
  > **username_0**: Now I'm even more baffled. I used your code suggestion, no error was thrown and everything is working. However the only difference between your code and mine is that I had no exception included. So I reverted back to my code and it still works! (But I'm keeping your version with the exception for the future). I just don't understand why it wasn't working but is now.  
  
  ---
  
  ## 0 answers
  
  