Answers.Body: <p>Your <code>skillName</code> is a object of type <code>ElementCriteriaModel</code> so it is just a query. In order to fetch the element you need to do <code>$skill-&gt;skillName-&gt;first()</code></p>
  
  <p>You should read the docs about how to fetch entries and how the elementCriteriaModel works.</p>
  
  <p>Edit: your <code>$skillLevels</code> and your <code>$skill-&gt;skillName</code> are both the same type of objects. Both are ready to use queries that have a iterable interface. So when you loop through them (like you do) the <code>find()</code> method is executed which returns you all the populated query results. You don't load all relations for an entry when you do <code>getEntryById</code> - you get just queries you can execute </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-28T17:19:04.363Z
Answers.Text: I'd read the docs. I realised about 10 minutes after posting this that I needed to iterate through skillName or use first(). Thanks
Answers.UserDisplayName: foamcow
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-28T17:22:24.277Z
Answers.Text: Seems like my edit was wasted time then. This works for every relation.. Images, users, all assets, categories, even matrix fields are only relations. No matter what you relate you always get a criteriamodel unless you specify it otherwise. It can be a little bit confusing at the first time
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-28T17:25:34.667Z
Answers.Text: Thanks, it's all helpful. I knew that it returned a model but I didn't make the connection that i needed to iterate through it. Dumb because I've done it before!
Answers.UserDisplayName: foamcow
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-28T17:14:00.673Z
Answers.LastActivityDate: 2017-11-28T17:19:13.203Z
Answers.LastEditDate: 2017-11-28T17:19:13.203Z
Answers.UserDisplayName: Robin Schambach
Answers.Body: <p>It’s an entries field, so <code>$skill-&gt;skillName</code> returns just the ElementCriteriaModel prepared to query for one or more elements matching the criteria.</p>
  
  <p>You need to do something like this:</p>
  
  <pre><code>$entry = $skill-&gt;skillName-&gt;first();
  $skillName = $entry ? $entry-&gt;title : null;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-28T17:13:04.197Z
Answers.LastActivityDate: 2017-11-28T17:13:04.197Z
Answers.UserDisplayName: carlcs
Body: <p>I have a Matrix field named "skillLevels" with a single block type.
  Each row has 2 fields, "skillName" and "skillLevel"</p>
  
  <p>skillName is a relationship to another entry (Skills)
  skillLevel is a simple integer.</p>
  
  <p>I am building a custom plugin which takes post data from a form.
  The form contains an entry ID ($groupId), a skill name and a value to use to modify the level of that skill.</p>
  
  <pre><code>$groupEntry = craft()-&gt;entries-&gt;getEntryById($groupId);
  
      $skillLevels = $groupEntry-&gt;skillLevels;
  
      foreach($skillLevels as $skill){
         $skillLevel = $skill-&gt;skillLevel; // outputs the skill level
         $skillName = $skill-&gt;skillName; // this doesn't work
      }
  </code></pre>
  
  <p><code>$skill-&gt;$skillLevel</code> gives me the integer stored in that field.
  But <code>$skill-&gt;skillLevel</code> does not gives me a model. I then tried using $skill->skillLevel->id but still no luck.</p>
  
  <p>How can I look through the Matrix rows to find the row that matches the skill name coming from the form Post data?</p>
  
Tags: <plugin-development><matrix><relations>
Title: How can I work with related entries within a Matrix in a plug in?
ThreadText: # How can I work with related entries within a Matrix in a plug in?
  
  (Asked by: username_0 on 2017-11-28)
  
  I have a Matrix field named "skillLevels" with a single block type.
  Each row has 2 fields, "skillName" and "skillLevel"
  
  skillName is a relationship to another entry (Skills)
  skillLevel is a simple integer.
  
  I am building a custom plugin which takes post data from a form.
  The form contains an entry ID ($groupId), a skill name and a value to use to modify the level of that skill.
  
  ```
  $groupEntry = craft()->entries->getEntryById($groupId);
  
      $skillLevels = $groupEntry->skillLevels;
  
      foreach($skillLevels as $skill){
         $skillLevel = $skill->skillLevel; // outputs the skill level
         $skillName = $skill->skillName; // this doesn't work
      }
  
  ```
  
  `$skill->$skillLevel` gives me the integer stored in that field.
  But `$skill->skillLevel` does not gives me a model. I then tried using $skill->skillLevel->id but still no luck.
  
  How can I look through the Matrix rows to find the row that matches the skill name coming from the form Post data?
  
  **Tags:** plugin-development, matrix, relations
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-11-28. Score: 3 | Accepted answer
  
  Your `skillName` is a object of type `ElementCriteriaModel` so it is just a query. In order to fetch the element you need to do `$skill->skillName->first()`
  
  You should read the docs about how to fetch entries and how the elementCriteriaModel works.
  
  Edit: your `$skillLevels` and your `$skill->skillName` are both the same type of objects. Both are ready to use queries that have a iterable interface. So when you loop through them (like you do) the `find()` method is executed which returns you all the populated query results. You don't load all relations for an entry when you do `getEntryById` - you get just queries you can execute
  
  > **Comments:**
  > **username_0**: I'd read the docs. I realised about 10 minutes after posting this that I needed to iterate through skillName or use first(). Thanks  
  > **username_1**: Seems like my edit was wasted time then. This works for every relation.. Images, users, all assets, categories, even matrix fields are only relations. No matter what you relate you always get a criteriamodel unless you specify it otherwise. It can be a little bit confusing at the first time  
  > **username_0**: Thanks, it's all helpful. I knew that it returned a model but I didn't make the connection that i needed to iterate through it. Dumb because I've done it before!  
  
  ---
  
  ### Answer by username_2 on 2017-11-28. Score: 3
  
  It’s an entries field, so `$skill->skillName` returns just the ElementCriteriaModel prepared to query for one or more elements matching the criteria.
  
  You need to do something like this:
  
  ```
  $entry = $skill->skillName->first();
  $skillName = $entry ? $entry->title : null;
  
  ```
  
  
  ---
  
  