Answers.Body: <p>After digging through some code I found out, that the <code>MatrixBlockElementType</code> just returns a content table (which is necessary for the eager loading part), if a fieldId is given:</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::MatrixBlock);
  $criteria-&gt;type = 'mytype';
  $criteria-&gt;fieldId = 90;
  $criteria-&gt;order = 'field_mytype_myfield';
  
  $blocks = $criteria-&gt;find();
  </code></pre>
  
  <p>Like this everything seems to work as expected and I'm again in love with crafts flexibility :) </p>
  
  <p>Am I right that I have to pass in the id? I can't do it via the field handle 'mymatrixfieldhandle', correct?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-20T12:28:51.103Z
Answers.LastActivityDate: 2015-02-20T12:28:51.103Z
Answers.UserDisplayName: Sidney Widmer
Body: <p>Is there currently a way to eager load the matrix values of an element and thus be able to sort by one of its fields? Imagine you have something like the following:</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::MatrixBlock);
  $criteria-&gt;type = 'mytype';
  $blocks = $criteria-&gt;find();
  </code></pre>
  
  <p>This would create a select from the following tables:</p>
  
  <pre><code>FROM   `craft_elements` `elements` 
     JOIN `craft_elements_i18n` `elements_i18n` 
       ON elements_i18n.elementid = elements.id 
     JOIN `craft_matrixblocks` `matrixblocks` 
       ON matrixblocks.id = elements.id 
     JOIN `craft_matrixblocktypes` `matrixblocktypes` 
       ON matrixblocktypes.id = matrixblocks.typeid 
  </code></pre>
  
  <p>Obviously the most important table for my task at hand is missing: the <code>craft_matrixcontent_mytype</code> table. I'm not able order by mytable.myfield and additionally, while looping those blocks I have the N+1 problem where it builds an extra query to fetch the actual block content. </p>
  
  <p>Am I somehow able to load all MatrixBlocks of a given type, INCLUDING the values?</p>
  
Tags: <matrix><join>
Title: Sort by MatrixBlock Field / Eager load Matrix Field
ThreadText: # Sort by MatrixBlock Field / Eager load Matrix Field
  
  (Asked by: username_0 on 2015-02-20)
  
  Is there currently a way to eager load the matrix values of an element and thus be able to sort by one of its fields? Imagine you have something like the following:
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::MatrixBlock);
  $criteria->type = 'mytype';
  $blocks = $criteria->find();
  
  ```
  
  This would create a select from the following tables:
  
  ```
  FROM   `craft_elements` `elements` 
     JOIN `craft_elements_i18n` `elements_i18n` 
       ON elements_i18n.elementid = elements.id 
     JOIN `craft_matrixblocks` `matrixblocks` 
       ON matrixblocks.id = elements.id 
     JOIN `craft_matrixblocktypes` `matrixblocktypes` 
       ON matrixblocktypes.id = matrixblocks.typeid 
  
  ```
  
  Obviously the most important table for my task at hand is missing: the `craft_matrixcontent_mytype` table. I'm not able order by mytable.myfield and additionally, while looping those blocks I have the N+1 problem where it builds an extra query to fetch the actual block content.
  
  Am I somehow able to load all MatrixBlocks of a given type, INCLUDING the values?
  
  **Tags:** matrix, join
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-02-20. Score: 2
  
  After digging through some code I found out, that the `MatrixBlockElementType` just returns a content table (which is necessary for the eager loading part), if a fieldId is given:
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::MatrixBlock);
  $criteria->type = 'mytype';
  $criteria->fieldId = 90;
  $criteria->order = 'field_mytype_myfield';
  
  $blocks = $criteria->find();
  
  ```
  
  Like this everything seems to work as expected and I'm again in love with crafts flexibility :)
  
  Am I right that I have to pass in the id? I can't do it via the field handle 'mymatrixfieldhandle', correct?
  
  
  ---
  
  