<p>Answer: </p>

<p>So I looked in the craft core to see how the CP does it. Turns out we have to use this </p>

<pre class="lang-php prettyprint-override"><code>$gs = new \craft\elements\GlobalSet();
$gs-&gt;name = "Meta";
$gs-&gt;handle = "meta";

Craft::$app-&gt;getGlobals()-&gt;saveSet($gs);
</code></pre>

<p>Hope this helps someone. </p>

<p>Craft core reference</p>

<p><a href="https://github.com/craftcms/cms/blob/develop/src/controllers/GlobalsController.php#L55-L96" rel="nofollow noreferrer">https://github.com/craftcms/cms/blob/develop/src/controllers/GlobalsController.php#L55-L96</a></p>

CC BY-SA 4.0
2019-09-26T08:50:22.033Z
2019-09-26T08:50:22.033Z
32423hjh32423
<p>The following code is in a migration. It runs fine and there are no errors and prints <code>SAVED</code>, but nothing is saved in the <code>globalsets</code> database table. </p>

<p>All my other content migrations run fine so it is not a DB issue. </p>

<pre><code>    $globalConfig = [
      "type" =&gt; \craft\elements\GlobalSet::class,
      "settings" =&gt; [
        "name" =&gt; "Meta",
        "handle" =&gt; "meta"
      ],
    ];

    $element = Craft::$app-&gt;getElements()-&gt;createElement($globalConfig);

    if (Craft::$app-&gt;getElements()-&gt;saveElement($element)) {
        print "SAVED";
    }
</code></pre>

<p>Why is this not saving ? </p>

CC BY-SA 4.0
2019-09-25T19:41:40.803Z
After you call `saveElement()`, does `$element->getErrors()` return anything?
Brad Bell
CC BY-SA 4.0
2019-09-26T07:25:00.733Z
@BradBell getErrors is just an empty array. Do I also need to save a field layout first ?
32423hjh32423
<craft3><migration><elementquery>
Creating and saving a GlobalSet element type in Craft 3
# Creating and saving a GlobalSet element type in Craft 3

(Asked by: username_0 on 2019-09-25)

The following code is in a migration. It runs fine and there are no errors and prints SAVED, but nothing is saved in the globalsets database table. 
All my other content migrations run fine so it is not a DB issue. 
    $globalConfig = [
      "type" => \craft\elements\GlobalSet::class,
      "settings" => [
        "name" => "Meta",
        "handle" => "meta"
      ],
    ];

    $element = Craft::$app->getElements()->createElement($globalConfig);

    if (Craft::$app->getElements()->saveElement($element)) {
        print "SAVED";
    }

Why is this not saving ?

**Tags:** craft3, migration, elementquery

> **Comments:**
> **username_1**: After you call `saveElement()`, does `$element->getErrors()` return anything?  
> **username_0**: @BradBell getErrors is just an empty array. Do I also need to save a field layout first ?  

---

## 1 answers

### Answer by username_0 on 2019-09-26. Score: 1 | Accepted answer

Answer: 
So I looked in the craft core to see how the CP does it. Turns out we have to use this 
$gs = new \craft\elements\GlobalSet();
$gs->name = "Meta";
$gs->handle = "meta";

Craft::$app->getGlobals()->saveSet($gs);

Hope this helps someone. 
Craft core reference
https://github.com/craftcms/cms/blob/develop/src/controllers/GlobalsController.php#L55-L96


---

