<p>You need to fetch the asset and set it as a field value to your entry</p>

<pre><code>$pushImageId = Asset::find()-&gt;filename($name[0])-&gt;ids();
</code></pre>

<p><code>id</code> will display the <code>id</code> property of your query. You won't fetch any results with it <code>ids()</code> will return an array of asset ids</p>

<pre><code>$product-&gt;setFieldValue ('productImages', $productImages);
</code></pre>

<p>Will set the relation and finally you can store the element</p>

<p>Edit: and you need to fetch the product as well. Currently you just create the query for it</p>

CC BY-SA 4.0
2018-10-09T19:02:17.267Z
2018-10-09T19:34:04.967Z
2018-10-09T19:34:04.967Z
Robin Schambach
<p>I have spreadsheet that i'm parsing and I have available to me a Entry ID and the Asset filename. Can I add the asset to the entry by the filename?</p>

<p>Im targeting the product entry with:</p>

<pre><code>$product = Entry::find()-&gt;section('products')-&gt;id($data[3]);
$productImages = ($product-&gt;productImages) ? $product-&gt;productImages : array();
$name = explode('?', basename($data[2])); 
$pushImageId = Asset::find()-&gt;filename($name[0])-&gt;id; // $name[0] = myfilename.jpg
$productImages = array_push($productImages,$pushImageId);
$product-&gt;productImages($productImages);
Craft::$app-&gt;elements-&gt;saveElement($product);
</code></pre>

<p>The handle for the asset field is:
<code>productImages</code> </p>

<p>So when I run this i get this error:</p>

<p><code>Exception: Argument 1 passed to craft\services\Elements::saveElement() must implement interface craft\base\ElementInterface, instance of craft\elements\db\EntryQuery given</code></p>

<p>Thanks!</p>

<p><strong>Solution</strong></p>

<p>Based on the answer from robin in this question <a href="https://craftcms.stackexchange.com/questions/25305/how-to-save-asset-field-of-an-entry-programmatically-in-craft-3">How to save Asset field of an Entry programmatically in Craft 3</a></p>

<p>It you just need to attach the asset ID's as an array. So i re-wrote the following code to create a new array of the original asset ids plus the new one. And it worked!</p>

<pre><code>$product = Entry::find()-&gt;section('products')-&gt;id($data[3])-&gt;one();
$name = explode('?', basename($data[2]));
$pushImageIds = Asset::find()-&gt;filename($name[0])-&gt;ids(); // $name[0] = myfilename.jpg
$newProductImagesArray = array();
foreach($product-&gt;productImages as $image){
    array_push($newProductImagesArray,$image-&gt;id);
}
array_push($newProductImagesArray,$pushImageIds[0]);

$product-&gt;setFieldValue('productImages',$newProductImagesArray);
Craft::$app-&gt;getElements()-&gt;saveElement($product);
</code></pre>

<entries><assets><craft3>
Push/Add asset to Entry by asset filename
# Push/Add asset to Entry by asset filename

(Asked by: username_0 on 2018-10-09)

I have spreadsheet that i'm parsing and I have available to me a Entry ID and the Asset filename. Can I add the asset to the entry by the filename?
Im targeting the product entry with:
$product = Entry::find()->section('products')->id($data[3]);
$productImages = ($product->productImages) ? $product->productImages : array();
$name = explode('?', basename($data[2])); 
$pushImageId = Asset::find()->filename($name[0])->id; // $name[0] = myfilename.jpg
$productImages = array_push($productImages,$pushImageId);
$product->productImages($productImages);
Craft::$app->elements->saveElement($product);

The handle for the asset field is:
productImages 
So when I run this i get this error:
Exception: Argument 1 passed to craft\services\Elements::saveElement() must implement interface craft\base\ElementInterface, instance of craft\elements\db\EntryQuery given
Thanks!
Solution
Based on the answer from robin in this question How to save Asset field of an Entry programmatically in Craft 3
It you just need to attach the asset ID's as an array. So i re-wrote the following code to create a new array of the original asset ids plus the new one. And it worked!
$product = Entry::find()->section('products')->id($data[3])->one();
$name = explode('?', basename($data[2]));
$pushImageIds = Asset::find()->filename($name[0])->ids(); // $name[0] = myfilename.jpg
$newProductImagesArray = array();
foreach($product->productImages as $image){
    array_push($newProductImagesArray,$image->id);
}
array_push($newProductImagesArray,$pushImageIds[0]);

$product->setFieldValue('productImages',$newProductImagesArray);
Craft::$app->getElements()->saveElement($product);

**Tags:** entries, assets, craft3

---

## 1 answers

### Answer by username_1 on 2018-10-09. Score: 2 | Accepted answer

You need to fetch the asset and set it as a field value to your entry
$pushImageId = Asset::find()->filename($name[0])->ids();

id will display the id property of your query. You won't fetch any results with it ids() will return an array of asset ids
$product->setFieldValue ('productImages', $productImages);

Will set the relation and finally you can store the element
Edit: and you need to fetch the product as well. Currently you just create the query for it


---

