<p>You should insert an array of ids - not an array of elements - in order to make it work. That's basically it</p>

<pre><code>$new_$nearby_locations[] = $nearby_location-&gt;id;
</code></pre>

<p>(that's usually better than array push too)</p>

<p>Edit</p>

<p>But your error message sounds more like your slug is an Array or something like this. Do you change any other fields somewhere? </p>

CC BY-SA 3.0
2018-02-27T05:50:30.690Z
2018-02-27T06:03:47.907Z
2018-02-27T06:03:47.907Z
Robin Schambach
<p>I have a Category element, <code>locations</code>, which I am trying to update programatically through a Craft 3 plugin that reads data from a CSV.</p>

<p>The <code>locations</code> category has a Field Layout: <code>latitude</code> (Number), <code>longitude</code> (Number), <code>nearbyLocations</code> (Categories).</p>

<p><strong>I am having trouble updating a locations' <code>nearbyLocations</code> field</strong>. <em>I am inserting an array of Categories</em> into the <code>nearbyLocations</code> field on the <code>setFieldValues()</code> call.</p>

<p>A TypeError gets thrown deep in the bowels of Craft:</p>

<blockquote>
  <p>craft\helpers\StringHelper::toLowerCase() must be of the type string, 
  array given, called in 
  /home/vagrant/code/vendor/craftcms/cms/src/helpers/Db.php on line 448</p>
</blockquote>

<p>Below is my code:</p>

<p><strong>$new_nearby_locations is an array of Category elements.</strong></p>



<pre class="lang-php prettyprint-override"><code>$new_nearby_locations = [];
foreach ($nearby_locations_slugs_from_csv as $nearby_location_slug) {
  $nearby_location = Category::find()
                    -&gt;slug($nearby_location_slug)
                    -&gt;one();

  if (!is_null($nearby_location)) {
      array_push($new_$nearby_locations, $nearby_location);
  }
}
</code></pre>

<p><strong>Update $category's field values.</strong></p>

<pre class="lang-php prettyprint-override"><code>$category = Category::find()-&gt;slug($slug)-&gt;one();
$category-&gt;setFieldValues([
    'latitude' =&gt; $new_latitude, // previously defined
    'longitude' =&gt; $new_longitude, // previously defined        
    'nearbyLocations' =&gt; $new_nearby_locations
]);
</code></pre>

<p><strong>Save the changes</strong></p>

<pre class="lang-php prettyprint-override"><code> Craft::$app-&gt;elements-&gt;saveElement($category);
</code></pre>

<p>I am suspicious that updating <code>nearbyLocations</code> by inserting an array of Category elements is wrong. If so, what is the right format?</p>

<p>Or am I doing something else wrong.</p>

<p>Thanks in advance!</p>

<plugin-development><categories><craft3>
Craft 3 - How do you programatically update a field type that is a Category?
# Craft 3 - How do you programatically update a field type that is a Category?

(Asked by: username_0 on 2018-02-27)

I have a Category element, locations, which I am trying to update programatically through a Craft 3 plugin that reads data from a CSV.
The locations category has a Field Layout: latitude (Number), longitude (Number), nearbyLocations (Categories).
I am having trouble updating a locations' nearbyLocations field. I am inserting an array of Categories into the nearbyLocations field on the setFieldValues() call.
A TypeError gets thrown deep in the bowels of Craft:

craft\helpers\StringHelper::toLowerCase() must be of the type string, 
  array given, called in 
  /home/vagrant/code/vendor/craftcms/cms/src/helpers/Db.php on line 448

Below is my code:
$new_nearby_locations is an array of Category elements.
$new_nearby_locations = [];
foreach ($nearby_locations_slugs_from_csv as $nearby_location_slug) {
  $nearby_location = Category::find()
                    ->slug($nearby_location_slug)
                    ->one();

  if (!is_null($nearby_location)) {
      array_push($new_$nearby_locations, $nearby_location);
  }
}

Update $category's field values.
$category = Category::find()->slug($slug)->one();
$category->setFieldValues([
    'latitude' => $new_latitude, // previously defined
    'longitude' => $new_longitude, // previously defined        
    'nearbyLocations' => $new_nearby_locations
]);

Save the changes
 Craft::$app->elements->saveElement($category);

I am suspicious that updating nearbyLocations by inserting an array of Category elements is wrong. If so, what is the right format?
Or am I doing something else wrong.
Thanks in advance!

**Tags:** plugin-development, categories, craft3

---

## 1 answers

### Answer by username_1 on 2018-02-27. Score: 1 | Accepted answer

You should insert an array of ids - not an array of elements - in order to make it work. That's basically it
$new_$nearby_locations[] = $nearby_location->id;

(that's usually better than array push too)
Edit
But your error message sounds more like your slug is an Array or something like this. Do you change any other fields somewhere?


---

