<p>To save a custom field value on an Asset, you'll first need to pull the actual AssetFileModel, set the custom attribute on the Asset's <a href="https://craftcms.stackexchange.com/questions/4486/what-is-getcontent-the-contentmodel-and-how-do-i-use-it-to-get-and-set-cust">ContentModel</a>, and finally re-save the Asset.</p>

<p>The following example assumes you've set a field <code>photoId</code> on your asset source:</p>

<pre><code>$fileId = $response-&gt;getDataItem('fileId');
$asset = craft()-&gt;assets-&gt;getFileById($fileId);
$asset-&gt;getContent()-&gt;setAttribute('photoId', $photo['PhotoId']);
$success = craft()-&gt;assets-&gt;storeFile($asset);
</code></pre>

CC BY-SA 3.0
2016-06-26T18:47:59.443Z
Awesome thanks!
Dave Coggins
CC BY-SA 3.0
2016-06-26T18:22:38.677Z
2016-06-27T10:20:34.533Z
2017-04-13T12:51:00.253Z
Mats Mikkel Rummelhoff
<p>I am using the following code to save an asset that has been downloaded to the storage/runtime/temp directory from an external API</p>

<pre><code>$response = craft()-&gt;assets-&gt;insertFileByLocalPath(
    $tempPath,
    $photo['FileName'],
    $folderId,
    AssetConflictResolution::Replace
);

// if the response is a success, get the file id
if ($response &amp;&amp; $response-&gt;isSuccess()) {
    $fileIds[] = $response-&gt;getDataItem('fileId');
}
</code></pre>

<p>In this example <code>$photo[]</code> array is the response from the api with all photo information. What I need to do is also save $photo['PhotoId'] to a field I have setup on the photo asset source in Craft.</p>

<p>I can't however find an example of how to achieve this. Does anyone know how?</p>

<assets><assetfilemodel>
How to set a field of an asset from within a plugin
# How to set a field of an asset from within a plugin

(Asked by: username_0 on 2016-06-22)

I am using the following code to save an asset that has been downloaded to the storage/runtime/temp directory from an external API
$response = craft()->assets->insertFileByLocalPath(
    $tempPath,
    $photo['FileName'],
    $folderId,
    AssetConflictResolution::Replace
);

// if the response is a success, get the file id
if ($response && $response->isSuccess()) {
    $fileIds[] = $response->getDataItem('fileId');
}

In this example $photo[] array is the response from the api with all photo information. What I need to do is also save $photo['PhotoId'] to a field I have setup on the photo asset source in Craft.
I can't however find an example of how to achieve this. Does anyone know how?

**Tags:** assets, assetfilemodel


---

## 1 answers

### Answer by username_1 on 2016-06-26. Score: 6 | Accepted answer

To save a custom field value on an Asset, you'll first need to pull the actual AssetFileModel, set the custom attribute on the Asset's ContentModel, and finally re-save the Asset.
The following example assumes you've set a field photoId on your asset source:
$fileId = $response->getDataItem('fileId');
$asset = craft()->assets->getFileById($fileId);
$asset->getContent()->setAttribute('photoId', $photo['PhotoId']);
$success = craft()->assets->storeFile($asset);

> **Comments:**
> **username_0**: Awesome thanks!  

---

