<p>It sort of depends on what information you have to identify the entry. If your external service knows the entry id that would be the most direct — otherwise you would have to find the entry by some other unique property.</p>

<p>To update an entry (given $entryId, $newTitle):</p>

<pre><code>$entry = craft()-&gt;entries-&gt;getEntryById($entryId);
if ($entry) {
    $entry-&gt;getContent()-&gt;setAttributes(array(
        'title' =&gt; $newTitle
    ));
    craft()-&gt;entries-&gt;saveEntry($entry);
}
</code></pre>

<p>To update an entry (given $sectionHandle, $title, and $newTitle - or other unique property):</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = $sectionHandle;
$criteria-&gt;title = $title;
$entries = craft()-&gt;elements-&gt;findElements($criteria);

foreach ($entries as $entry) {
    $entry-&gt;getContent()-&gt;setAttributes(array(
        'title' =&gt; $newTitle
    ));
    craft()-&gt;entries-&gt;saveEntry($entry);
}
</code></pre>

<p>Could also use the 'search' method (although less reliable):</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;search = 'fieldHandle:searchstring';
</code></pre>

<p>In the last two cases, would need to handle the possibility that more than 1 entry is returned.</p>

<p>Also, when creating entries might want to include the authorId and typeId, in addition to sectionId:</p>

<pre><code>$entry = new EntryModel();
$entry-&gt;sectionId  = $sectionId
$entry-&gt;typeId     = $typeId
$entry-&gt;authorId   = $authorId;
$entry-&gt;enabled    = true;
$entry-&gt;getContent()-&gt;setAttributes(array(
    'title' =&gt; $title
));
craft()-&gt;entries-&gt;saveEntry($entry);
</code></pre>

CC BY-SA 3.0
2015-02-11T01:08:24.907Z
2015-02-11T02:04:31.060Z
2015-02-11T02:04:31.060Z
Douglas McDonald
<p>I'm currently creating a plugin for craft and have stumbled across a slight problem. I'll try summarise of what I am doing and what I am trying to achieve.</p>

<p>So, I'm trying creating a plugin which will update entries within craft via an external API. I have managed to get the initial save to work using these following lines:</p>

<pre><code>$entry = new EntryModel();
$entry-&gt;getContent()-&gt;title = $project-&gt;name;
$entry-&gt;sectionId = 10;
craft()-&gt;entries-&gt;saveEntry($entry);
</code></pre>

<p>The issue arises when I'm trying to update the model. Obviously, I don't want to duplicate entries.</p>

<p>I can get the entry using this line:</p>

<pre><code>EntryRecord::model()-&gt;findById(45);
</code></pre>

<p>(this is just used for testing, eventually I will be getting the record via a custom variable).</p>

<p>What I'm trying to do here is update the title of this record, from what I can see this is stored in the craft_content table.</p>

<p>Basically what I'd like to do is something like :</p>

<pre><code>EntryModel::model()-&gt;findByAttributes(array('title'=&gt;$project-&gt;name);
</code></pre>

<p>So I can then update the content title of that row.
I can see I can't do it this way, would you be able point me in the right direction?</p>

<database><queries>
Finding entry by content
# Finding entry by content

(Asked by: username_0 on 2015-02-10)

I'm currently creating a plugin for craft and have stumbled across a slight problem. I'll try summarise of what I am doing and what I am trying to achieve.
So, I'm trying creating a plugin which will update entries within craft via an external API. I have managed to get the initial save to work using these following lines:
$entry = new EntryModel();
$entry->getContent()->title = $project->name;
$entry->sectionId = 10;
craft()->entries->saveEntry($entry);

The issue arises when I'm trying to update the model. Obviously, I don't want to duplicate entries.
I can get the entry using this line:
EntryRecord::model()->findById(45);

(this is just used for testing, eventually I will be getting the record via a custom variable).
What I'm trying to do here is update the title of this record, from what I can see this is stored in the craft_content table.
Basically what I'd like to do is something like :
EntryModel::model()->findByAttributes(array('title'=>$project->name);

So I can then update the content title of that row.
I can see I can't do it this way, would you be able point me in the right direction?

**Tags:** database, queries


---

## 1 answers

### Answer by username_1 on 2015-02-11. Score: 3

It sort of depends on what information you have to identify the entry. If your external service knows the entry id that would be the most direct — otherwise you would have to find the entry by some other unique property.
To update an entry (given $entryId, $newTitle):
$entry = craft()->entries->getEntryById($entryId);
if ($entry) {
    $entry->getContent()->setAttributes(array(
        'title' => $newTitle
    ));
    craft()->entries->saveEntry($entry);
}

To update an entry (given $sectionHandle, $title, and $newTitle - or other unique property):
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = $sectionHandle;
$criteria->title = $title;
$entries = craft()->elements->findElements($criteria);

foreach ($entries as $entry) {
    $entry->getContent()->setAttributes(array(
        'title' => $newTitle
    ));
    craft()->entries->saveEntry($entry);
}

Could also use the 'search' method (although less reliable):
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->search = 'fieldHandle:searchstring';

In the last two cases, would need to handle the possibility that more than 1 entry is returned.
Also, when creating entries might want to include the authorId and typeId, in addition to sectionId:
$entry = new EntryModel();
$entry->sectionId  = $sectionId
$entry->typeId     = $typeId
$entry->authorId   = $authorId;
$entry->enabled    = true;
$entry->getContent()->setAttributes(array(
    'title' => $title
));
craft()->entries->saveEntry($entry);


---

