<p><code>$matrixRow-&gt;relationshipField</code> returns an ElementCriteriaModel pre-configured to query for the related elements. You can add / override additional criteria and then query for a matching element using the <code>first()</code> method.</p>

<pre><code>$randomEntry = $matrixRow-&gt;relationshipField-&gt;order('RAND()')-&gt;first();
</code></pre>

<p>This is basically the same as what you’ve tried with your custom ElementCriteriaModel. It should also work once you have fixed the mistake in the <code>relatedTo</code> param, as the entry that holds the field is the “sourceElement” of a relation.</p>

CC BY-SA 3.0
2017-12-05T16:07:00.963Z
I have just discovered this and came here to edit my question. Brilliant! Makes life much easier!
foamcow
CC BY-SA 3.0
2017-12-05T15:51:27.327Z
2017-12-05T15:57:42.553Z
2017-12-05T15:57:42.553Z
carlcs
<p>The <code>ElementCriteriaModel</code> has a function <code>nth($offset)</code> <a href="https://craftcms.com/classreference/models/ElementCriteriaModel#nth-detail" rel="nofollow noreferrer">link</a> so you could get a random number between 0 and <code>count()</code> and put it in your <code>nth()</code> function</p>

<pre><code>$randomNumber = rand(0, $matrixRow-&gt;relationshipField-&gt;count());
$relatedEntry = $matrixRow-&gt;relationshipField-&gt;nth($randomNumber);
</code></pre>

<p>As an answer to your edit: you know that <code>$matrixRow-&gt;relationshipField</code> is nearly the same as your edit? <code>$matrixRow-&gt;relationshipField</code> is an object, it is not an array of elements. It has an iterable interface so the <code>$criteria-&gt;find()</code> method is executed as soon as you loop through it. So in the end the criteria you build is nearly the same object like Craft gives you with <code>$matrixRow-&gt;relationshipField</code></p>

CC BY-SA 3.0
2017-12-05T15:42:50.863Z
2017-12-05T15:48:35.487Z
2017-12-05T15:48:35.487Z
Robin Schambach
<p>Given a Matrix with each row containing a text field and a multi relationship field. And I am picking 1 Matrix row based on the value in the text field. How would I go about selecting a single, random, related entry for that row?</p>

<p>I've tried to do this a couple of ways but I'm not really getting anywhere.</p>

<p>I'm selecting the Matrix row and getting the related entries using</p>

<pre><code>$matrixRow = $entry-&gt;matrixField-&gt;textField($stringToMatch)-&gt;first();

$relatedEntries = $matrixRow-&gt;relationshipField;
</code></pre>

<p>But how can I now pick one of those related entries at random?
Is the only way to create an array and shuffle it?
Or is there a way I can do it when fetching the Matrix row?</p>

<p>I've tried adding the following in an attempt to get all the related entries associated with the Matrix row. Figuring I can then modify this query by adding a limit and ordering randomly to get my 1 related entry. But it's coming back empty.</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section         = 'videos';
$criteria-&gt;relatedTo       = array(
    'targetElement'   =&gt; $matrixRow,
    'field'     =&gt; 'relationshipField'
);

$entries = $criteria-&gt;find();

foreach($entries as $entry){
    var_dump($entry-&gt;title);
}
</code></pre>

<plugin-development><matrix><elementcriteriamodel>
Getting a single, random, related entry within a plug in
# Getting a single, random, related entry within a plug in

(Asked by: username_0 on 2017-12-05)

Given a Matrix with each row containing a text field and a multi relationship field. And I am picking 1 Matrix row based on the value in the text field. How would I go about selecting a single, random, related entry for that row?
I've tried to do this a couple of ways but I'm not really getting anywhere.
I'm selecting the Matrix row and getting the related entries using
$matrixRow = $entry->matrixField->textField($stringToMatch)->first();

$relatedEntries = $matrixRow->relationshipField;

But how can I now pick one of those related entries at random?
Is the only way to create an array and shuffle it?
Or is there a way I can do it when fetching the Matrix row?
I've tried adding the following in an attempt to get all the related entries associated with the Matrix row. Figuring I can then modify this query by adding a limit and ordering randomly to get my 1 related entry. But it's coming back empty.
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section         = 'videos';
$criteria->relatedTo       = array(
    'targetElement'   => $matrixRow,
    'field'     => 'relationshipField'
);

$entries = $criteria->find();

foreach($entries as $entry){
    var_dump($entry->title);
}

**Tags:** plugin-development, matrix, elementcriteriamodel


---

## 2 answers

### Answer by username_1 on 2017-12-05. Score: 2 | Accepted answer

$matrixRow->relationshipField returns an ElementCriteriaModel pre-configured to query for the related elements. You can add / override additional criteria and then query for a matching element using the first() method.
$randomEntry = $matrixRow->relationshipField->order('RAND()')->first();

This is basically the same as what you’ve tried with your custom ElementCriteriaModel. It should also work once you have fixed the mistake in the relatedTo param, as the entry that holds the field is the “sourceElement” of a relation.

> **Comments:**
> **username_0**: I have just discovered this and came here to edit my question. Brilliant! Makes life much easier!  

---

### Answer by username_2 on 2017-12-05. Score: 1

The ElementCriteriaModel has a function nth($offset) link so you could get a random number between 0 and count() and put it in your nth() function
$randomNumber = rand(0, $matrixRow->relationshipField->count());
$relatedEntry = $matrixRow->relationshipField->nth($randomNumber);

As an answer to your edit: you know that $matrixRow->relationshipField is nearly the same as your edit? $matrixRow->relationshipField is an object, it is not an array of elements. It has an iterable interface so the $criteria->find() method is executed as soon as you loop through it. So in the end the criteria you build is nearly the same object like Craft gives you with $matrixRow->relationshipField


---

