Answers.Body: <p>It's actually not SQL friendly at all to store multiple values (Title and Video ID) in a single column. The data is stored as a serialized string and all you can do is to use the pattern matching operator <a href="https://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html" rel="nofollow"><code>LIKE</code></a> to get the row you're after.</p>
  
  <p>For best performance you should refactor your fields and store each of them individually. If all the data has to be in one field, store the field in its own table (like Matrix fields do).</p>
  
  <p>If this is no option for you, I would recommend to not use the <code>search</code> parameter with your ElementCriteriaModel, but to prepare a <a href="http://buildwithcraft.com/classreference/etc/db/DbCommand" rel="nofollow">DbCommand</a> object using <a href="http://buildwithcraft.com/classreference/services/ElementsService#buildElementsQuery-detail" rel="nofollow">buildElementsQuery</a> and add your "Video ID condition" using the <code>LIKE</code> operator I mentioned above, so you don't rely on up-to-date search indexes.</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $criteria-&gt;section = 'video';
  
  $query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);
  $params = array(
      ':video_id' =&gt; '%video_id='.$video_id.'%',
  );
  $query-&gt;andWhere('entries.video LIKE :video_id', $params);
  $result = $query-&gt;queryRow();
  
  return EntryModel::populateModel($result);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-21T07:41:35.173Z
Answers.LastActivityDate: 2015-07-21T07:41:35.173Z
Answers.UserDisplayName: carlcs
Body: <p>I have a field defined in my plugin called <code>video</code> with <code>AttributeType::Mixed</code> where I store a video_id and a title of a video. I need to look up the entry by the video_id (I can't do it via the the entry ID).</p>
  
  <pre><code>$entry = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $entry-&gt;section = 'video';
  $entry-&gt;search('video: video_id=' . $videoID);
  return $entry-&gt;first();
  </code></pre>
  
  <p>This works, but there has to be a better solution?</p>
  
Tags: <plugin-development><plugins><entrymodel>
Title: Get entry by value from custom field
ThreadText: # Get entry by value from custom field
  
  (Asked by: username_0 on 2015-07-09)
  
  I have a field defined in my plugin called `video` with `AttributeType::Mixed` where I store a video\_id and a title of a video. I need to look up the entry by the video\_id (I can't do it via the the entry ID).
  
  ```
  $entry = craft()->elements->getCriteria(ElementType::Entry);
  $entry->section = 'video';
  $entry->search('video: video_id=' . $videoID);
  return $entry->first();
  
  ```
  
  This works, but there has to be a better solution?
  
  **Tags:** plugin-development, plugins, entrymodel
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-07-21. Score: 2
  
  It's actually not SQL friendly at all to store multiple values (Title and Video ID) in a single column. The data is stored as a serialized string and all you can do is to use the pattern matching operator [`LIKE`](https://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html) to get the row you're after.
  
  For best performance you should refactor your fields and store each of them individually. If all the data has to be in one field, store the field in its own table (like Matrix fields do).
  
  If this is no option for you, I would recommend to not use the `search` parameter with your ElementCriteriaModel, but to prepare a [DbCommand](http://buildwithcraft.com/classreference/etc/db/DbCommand) object using [buildElementsQuery](http://buildwithcraft.com/classreference/services/ElementsService#buildElementsQuery-detail) and add your "Video ID condition" using the `LIKE` operator I mentioned above, so you don't rely on up-to-date search indexes.
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  $criteria->section = 'video';
  
  $query = craft()->elements->buildElementsQuery($criteria);
  $params = array(
      ':video_id' => '%video_id='.$video_id.'%',
  );
  $query->andWhere('entries.video LIKE :video_id', $params);
  $result = $query->queryRow();
  
  return EntryModel::populateModel($result);
  
  ```
  
  
  ---
  
  