Answers.Body: <p>After some research, i found a way to achieve this. Here is my full controller:</p>
  
  <pre><code>&lt;?php
    namespace Craft;
  
    class Module_ControlController extends BaseController {
     protected $allowAnonymous = true;
  public function actionChangeStock()
  {
      $entry = $this-&gt;getElementByField("3081001100390", ElementType::Entry, null, "variantSku");
      $entry-&gt;enabled = false;
      $entry-&gt;getContent()-&gt;setAttributes(array(
          'variantSku' =&gt; "Hello my friend!",
          'variantStock'  =&gt; "3"
      ));
      $success = craft()-&gt;entries-&gt;saveEntry($entry);
      die(var_dump($success));
  }
  
  /**
   * Returns an element by any attribute.
   *
   * @param int $elementId
   * @param string|null $type
   * @param string|null $localeId
   * @return BaseElementModel|null
   */
  public function getElementByField($elementId, $elementType = null, $localeId = null, $field)
  {
      if (!$elementId)
      {
          return null;
      }
  
      if (!$elementType)
      {
          $elementType = craft()-&gt;elements-&gt;getElementTypeById($elementId);
  
          if (!$elementType)
          {
              return null;
          }
      }
  
      $criteria = craft()-&gt;elements-&gt;getCriteria($elementType);
      $criteria-&gt;$field = $elementId;
      $criteria-&gt;locale = $localeId;
      $criteria-&gt;status = null;
      $criteria-&gt;localeEnabled = null;
      return $criteria-&gt;first();
  }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-17T21:41:02.137Z
Answers.Text: This will not work on Craft 3. Any idea on how to implement this login in craft 3? The documentation is not helping me out...
Answers.UserDisplayName: Radmation
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-30T12:13:38.710Z
Answers.LastActivityDate: 2014-06-30T12:13:38.710Z
Answers.UserDisplayName: mbalparda
Body: <p>Im trying to update an entry field programatically. I can load an entry by any field with a function i created</p>
  
  <pre><code>    /**
   * Returns an element by any attribute.
   *
   * @param int $elementId
   * @param string|null $type
   * @param string|null $localeId
   * @param string $localeId
   * @return BaseElementModel|null
   */
  public function getElementByField($elementId, $elementType = null, $localeId = null, $field)
  {
      if (!$elementId)
      {
          return null;
      }
  
      if (!$elementType)
      {
          $elementType = craft()-&gt;elements-&gt;getElementTypeById($elementId);
  
          if (!$elementType)
          {
              return null;
          }
      }
  
      $criteria = craft()-&gt;elements-&gt;getCriteria($elementType);
      $criteria-&gt;$field = $elementId;
      $criteria-&gt;locale = $localeId;
      $criteria-&gt;status = null;
      $criteria-&gt;localeEnabled = null;
      return $criteria-&gt;first();
  }
  </code></pre>
  
  <p>but i cant save the new values of the fields i want.
  This is my save code:</p>
  
  <pre><code>$entry = $this-&gt;getElementByField("3081001100390", ElementType::Entry, null, "variantSku");
      $entry-&gt;variantSku = 1;
      craft()-&gt;entries-&gt;saveEntry($entry);
  </code></pre>
  
  <p>And the error is  Property "Craft\EntryModel.variantSku" is not defined. I know the variantSku exists since im loading a product by that attribute and i can see it when dumping $entry->variantSku.</p>
  
  <p>Complete error here: <a href="https://www.diigo.com/item/image/4u33k/972k" rel="nofollow">https://www.diigo.com/item/image/4u33k/972k</a></p>
  
  <p>Did anyone achieved this?</p>
  
Tags: <plugin-development>
Title: Update entry programatically
ThreadText: # Update entry programatically
  
  (Asked by: username_0 on 2014-06-30)
  
  Im trying to update an entry field programatically. I can load an entry by any field with a function i created
  
  ```
      /**
   * Returns an element by any attribute.
   *
   * @param int $elementId
   * @param string|null $type
   * @param string|null $localeId
   * @param string $localeId
   * @return BaseElementModel|null
   */
  public function getElementByField($elementId, $elementType = null, $localeId = null, $field)
  {
      if (!$elementId)
      {
          return null;
      }
  
      if (!$elementType)
      {
          $elementType = craft()->elements->getElementTypeById($elementId);
  
          if (!$elementType)
          {
              return null;
          }
      }
  
      $criteria = craft()->elements->getCriteria($elementType);
      $criteria->$field = $elementId;
      $criteria->locale = $localeId;
      $criteria->status = null;
      $criteria->localeEnabled = null;
      return $criteria->first();
  }
  
  ```
  
  but i cant save the new values of the fields i want.
  This is my save code:
  
  ```
  $entry = $this->getElementByField("3081001100390", ElementType::Entry, null, "variantSku");
      $entry->variantSku = 1;
      craft()->entries->saveEntry($entry);
  
  ```
  
  And the error is Property "Craft\EntryModel.variantSku" is not defined. I know the variantSku exists since im loading a product by that attribute and i can see it when dumping $entry->variantSku.
  
  Complete error here: <https://www.diigo.com/item/image/4u33k/972k>
  
  Did anyone achieved this?
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2014-06-30. Score: 4 | Accepted answer
  
  After some research, i found a way to achieve this. Here is my full controller:
  
  ```
  <?php
    namespace Craft;
  
    class Module_ControlController extends BaseController {
     protected $allowAnonymous = true;
  public function actionChangeStock()
  {
      $entry = $this->getElementByField("3081001100390", ElementType::Entry, null, "variantSku");
      $entry->enabled = false;
      $entry->getContent()->setAttributes(array(
          'variantSku' => "Hello my friend!",
          'variantStock'  => "3"
      ));
      $success = craft()->entries->saveEntry($entry);
      die(var_dump($success));
  }
  
  /**
   * Returns an element by any attribute.
   *
   * @param int $elementId
   * @param string|null $type
   * @param string|null $localeId
   * @return BaseElementModel|null
   */
  public function getElementByField($elementId, $elementType = null, $localeId = null, $field)
  {
      if (!$elementId)
      {
          return null;
      }
  
      if (!$elementType)
      {
          $elementType = craft()->elements->getElementTypeById($elementId);
  
          if (!$elementType)
          {
              return null;
          }
      }
  
      $criteria = craft()->elements->getCriteria($elementType);
      $criteria->$field = $elementId;
      $criteria->locale = $localeId;
      $criteria->status = null;
      $criteria->localeEnabled = null;
      return $criteria->first();
  }}
  
  ```
  
  > **Comments:**
  > **username_1**: This will not work on Craft 3. Any idea on how to implement this login in craft 3? The documentation is not helping me out...  
  
  ---
  
  