Answers.Body: <p>Crafts <code>ActiveRecord</code> has a function <code>beforeSave</code></p>
  
  <pre><code>/**
   * @inheritdoc
   */
  public function beforeSave($insert)
  {
      foreach ($this-&gt;fields() as $attribute) {
          $this-&gt;$attribute = Db::prepareValueForDb($this-&gt;$attribute);
      }
  
      // Prepare the values
      $now = Db::prepareDateForDb(new \DateTime());
  
      if ($this-&gt;getIsNewRecord()) {
          if ($this-&gt;hasAttribute('dateCreated')) {
              $this-&gt;dateCreated = $now;
          }
  
          if ($this-&gt;hasAttribute('uid')) {
              $this-&gt;uid = StringHelper::UUID();
          }
      }
  
      if ($this-&gt;hasAttribute('dateUpdated')) {
          $this-&gt;dateUpdated = $now;
      }
  
      return parent::beforeSave($insert);
  }
  </code></pre>
  
  <p>You can overwrite this method and include your <code>StringHelper::encodeMb4()</code> into it so every time <code>$record-&gt;save()</code> is executed your string will be converted </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-28T15:25:30.603Z
Answers.Text: Just make sure you convert them back before displaying them, via: `html_entity_decode($value, ENT_NOQUOTES, 'UTF-8');`
Answers.UserDisplayName: andrew.welch
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-28T08:19:37.940Z
Answers.LastActivityDate: 2018-06-28T08:19:37.940Z
Answers.UserDisplayName: Robin Schambach
Body: <p>the plugin I'm currently working on has to store user inputs which might contain 4 byte unicode characters like emojis. MySQL is not able to handle this with a utf-8 charset. It works if the charset of my tables is set to <code>utf8mb4</code> and if I add <code>'charset' =&gt; 'utf8mb4'</code> to <code>config/db.php</code>. Since the file <code>config/db.php</code> does not belong to my plugin, but to the installation of Craft itself, I'd like to not modify it. </p>
  
  <p>Alternatively, I can convert the inputs with <code>StringHelper::encodeMb4()</code> but then I would have to do that at every place where something from the outside enters my tables. </p>
  
  <p>Is there any pragmatic way of handling this? </p>
  
  <p>Greetings Jus</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-28T08:05:07.990Z
Comments.Text: Would it be an option to use your records `beforeSave` event to convert the string automatically when you do `$record->save()`?
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-06-28T08:14:11.850Z
Comments.Text: Yes, that solves the issue. Thanks @RobinSchambach
Comments.UserDisplayName: Justus M.
Tags: <plugin-development><mysql>
Title: Use utf8mb4 for database operations
ThreadText: # Use utf8mb4 for database operations
  
  (Asked by: username_0 on 2018-06-28)
  
  the plugin I'm currently working on has to store user inputs which might contain 4 byte unicode characters like emojis. MySQL is not able to handle this with a utf-8 charset. It works if the charset of my tables is set to `utf8mb4` and if I add `'charset' => 'utf8mb4'` to `config/db.php`. Since the file `config/db.php` does not belong to my plugin, but to the installation of Craft itself, I'd like to not modify it.
  
  Alternatively, I can convert the inputs with `StringHelper::encodeMb4()` but then I would have to do that at every place where something from the outside enters my tables.
  
  Is there any pragmatic way of handling this?
  
  Greetings Jus
  
  **Tags:** plugin-development, mysql
  
  > **Comments:**
  > **username_1**: Would it be an option to use your records `beforeSave` event to convert the string automatically when you do `$record->save()`?  
  > **username_0**: Yes, that solves the issue. Thanks @username_1  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-06-28. Score: 1 | Accepted answer
  
  Crafts `ActiveRecord` has a function `beforeSave`
  
  ```
  /**
   * @inheritdoc
   */
  public function beforeSave($insert)
  {
      foreach ($this->fields() as $attribute) {
          $this->$attribute = Db::prepareValueForDb($this->$attribute);
      }
  
      // Prepare the values
      $now = Db::prepareDateForDb(new \DateTime());
  
      if ($this->getIsNewRecord()) {
          if ($this->hasAttribute('dateCreated')) {
              $this->dateCreated = $now;
          }
  
          if ($this->hasAttribute('uid')) {
              $this->uid = StringHelper::UUID();
          }
      }
  
      if ($this->hasAttribute('dateUpdated')) {
          $this->dateUpdated = $now;
      }
  
      return parent::beforeSave($insert);
  }
  
  ```
  
  You can overwrite this method and include your `StringHelper::encodeMb4()` into it so every time `$record->save()` is executed your string will be converted
  
  > **Comments:**
  > **username_2**: Just make sure you convert them back before displaying them, via: `html_entity_decode($value, ENT_NOQUOTES, 'UTF-8');`  
  
  ---
  
  