Answers.Body: <p>What strikes me as interesting here is your dump contains a Model instance.</p>
  
  <p>For example you said:</p>
  
  <blockquote>
    <p>When I dump the response from the service method i'm getting back is:
    Craft\Placid_ResourceModel Object</p>
  </blockquote>
  
  <p>When I query findAll through a CActiveRecord derived class I get an array of Records. In order to populate this into models, the populateModel(s) methods are typically used.</p>
  
  <p>If you have a relation at the ActiveRecord level, it is a pseudo-attribute and will only be carried through to the model if the attribute is defined as defined attributes can only be mass assigned. Yii also supports rules definitions to mark attributes as safe, but this is almost entirely optional. </p>
  
  <p>The question is really why you have a model being output.</p>
  
  <p>E.g. for me the following code:</p>
  
  <pre><code>$records = SomePlugin_ProductRecord::model()-&gt;with('pricing')-&gt;findAll($criteria);
  var_dump($records[0]);
  </code></pre>
  
  <p>Results in:</p>
  
  <pre><code>object(Craft\SomePlugin_ProductRecord)#179 (14) {
  </code></pre>
  
  <p>Do you have any code which is auto inflating into a model? or are you calling populateModel(s)? </p>
  
  <p>If so, then you have made the correct call of creating the attribute in your model if you want to access it from there, as far as I've experienced.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-08T14:09:54.330Z
Answers.Text: Hey @Jay, thanks for the answer and info, I haven't had time to look into this but when I have i'll update, as far as I know I don't think I'm inflating anything as it's dumping straight from `return Placid_ResourceRecord::model()->with('endpoints')->findAll($options);` but i'll double check to be sure.
Answers.UserDisplayName: Alec Ritson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-01T14:06:49.410Z
Answers.LastActivityDate: 2015-05-01T14:06:49.410Z
Answers.UserDisplayName: Jay
Answers.Body: <p>What happens if you do something like this:</p>
  
  <pre><code>$resourceRecords = Placid_ResourceRecord::model()-&gt;with('endpoints')-&gt;findAll($options);
  
  foreach ($resourceRecords as $resourceRecord)
  {
      echo "Found ".count($resourceRecords)." endpoints for resource: ".$resourceRecord-&gt;id."&lt;br/&gt;";
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T14:30:51.243Z
Answers.Text: That just output the count of the resources, not the related endpoints. But I see what you mean. I tried putting `count($resourceRecord->endpoints)` but got the model attribute not found.So I added a `endpoints => AttributeType::Mixed` to the model and now they are coming through, but this will be the same whether ::Bool, ::String, ::Mixed, ::Number. Which seems a bit strange?
  
  When I was adding the relations I was looking at the 'CategoryGroupsModel` and they weren't defined there, which made me think they shouldn't need to be perhaps..
Answers.UserDisplayName: Alec Ritson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-13T20:57:43.433Z
Answers.LastActivityDate: 2015-04-13T20:57:43.433Z
Answers.UserDisplayName: Brad Bell
Body: <p>I have a plugin with two records with the relationships as follows:</p>
  
  <p><strong>Placid_EndpointRecord</strong>  </p>
  
  <pre><code>public function defineRelations()
  {
      return array(
          'resource' =&gt; array(static::BELONGS_TO, 'Placid_ResourceRecord', 'required' =&gt; false, 'onDelete' =&gt; static::SET_NULL)
      );
  }
  </code></pre>
  
  <p><strong>Placid_ResourceRecord</strong></p>
  
  <pre><code>public function defineRelations()
  {
      return array(
          'endpoints' =&gt; array(static::HAS_MANY, 'Placid_EndpointRecord', 'resourceId'),
      );
  }
  </code></pre>
  
  <p>In a service class I have (options being an empty array at this point):</p>
  
  <pre><code>return Placid_ResourceRecord::model()-&gt;with('endpoints')-&gt;findAll($options);
  </code></pre>
  
  <p>When I dump the response from the service method i'm getting back is:</p>
  
  <pre><code>Craft\Placid_ResourceModel Object
      (
          [classSuffix:protected] =&gt; Model
          [strictAttributes:protected] =&gt; 1
          [_classHandle:Craft\BaseModel:private] =&gt; 
          [_attributeConfigs:Craft\BaseModel:private] =&gt; Array
              (
                  [id] =&gt; Array
                      (
                          [maxLength] =&gt; 11
                          [min] =&gt; -2147483648
                          [max] =&gt; 2147483647
                          [decimals] =&gt; 0
                          [type] =&gt; number
                          [unsigned] =&gt; 
                          [length] =&gt; 10
                          [column] =&gt; integer
                      )
                  [name] =&gt; Array
                      (
                          [type] =&gt; string
                      )
                  [handle] =&gt; Array
                      (
                          [type] =&gt; string
                      )
                  [host] =&gt; Array
                      (
                          [type] =&gt; string
                      )
              )
  
          [_attributes:Craft\BaseModel:private] =&gt; Array
              (
                  [id] =&gt; 1
                  [name] =&gt; Foo
                  [handle] =&gt; Bar
                  [host] =&gt; Bar
              )
  
          [_extraAttributeNames:Craft\BaseModel:private] =&gt; 
          [_errors:CModel:private] =&gt; Array
              (
              )
  
          [_validators:CModel:private] =&gt; 
          [_scenario:CModel:private] =&gt; 
          [_e:CComponent:private] =&gt; 
          [_m:CComponent:private] =&gt; 
      )
  </code></pre>
  
  <p>There doesn't seem to be any hint of the endpoints relation, there is definitely a row in the related table but even if not i'd expected to see an empty array of some sorts.</p>
  
  <p>Is there anything i'm missing or doing wrong? I looked at some of the core files and it seems the records are set up the same way for similar relations and I get no errors so i'm a bit clueless as to what's going wrong. Any help would be awesome, thank you!</p>
  
Tags: <plugin-development>
Title: Plugin eager loading not loading relations
ThreadText: # Plugin eager loading not loading relations
  
  (Asked by: username_0 on 2015-04-13)
  
  I have a plugin with two records with the relationships as follows:
  
  **Placid\_EndpointRecord**
  
  ```
  public function defineRelations()
  {
      return array(
          'resource' => array(static::BELONGS_TO, 'Placid_ResourceRecord', 'required' => false, 'onDelete' => static::SET_NULL)
      );
  }
  
  ```
  
  **Placid\_ResourceRecord**
  
  ```
  public function defineRelations()
  {
      return array(
          'endpoints' => array(static::HAS_MANY, 'Placid_EndpointRecord', 'resourceId'),
      );
  }
  
  ```
  
  In a service class I have (options being an empty array at this point):
  
  ```
  return Placid_ResourceRecord::model()->with('endpoints')->findAll($options);
  
  ```
  
  When I dump the response from the service method i'm getting back is:
  
  ```
  Craft\Placid_ResourceModel Object
      (
          [classSuffix:protected] => Model
          [strictAttributes:protected] => 1
          [_classHandle:Craft\BaseModel:private] => 
          [_attributeConfigs:Craft\BaseModel:private] => Array
              (
                  [id] => Array
                      (
                          [maxLength] => 11
                          [min] => -2147483648
                          [max] => 2147483647
                          [decimals] => 0
                          [type] => number
                          [unsigned] => 
                          [length] => 10
                          [column] => integer
                      )
                  [name] => Array
                      (
                          [type] => string
                      )
                  [handle] => Array
                      (
                          [type] => string
                      )
                  [host] => Array
                      (
                          [type] => string
                      )
              )
  
          [_attributes:Craft\BaseModel:private] => Array
              (
                  [id] => 1
                  [name] => Foo
                  [handle] => Bar
                  [host] => Bar
              )
  
          [_extraAttributeNames:Craft\BaseModel:private] => 
          [_errors:CModel:private] => Array
              (
              )
  
          [_validators:CModel:private] => 
          [_scenario:CModel:private] => 
          [_e:CComponent:private] => 
          [_m:CComponent:private] => 
      )
  
  ```
  
  There doesn't seem to be any hint of the endpoints relation, there is definitely a row in the related table but even if not i'd expected to see an empty array of some sorts.
  
  Is there anything i'm missing or doing wrong? I looked at some of the core files and it seems the records are set up the same way for similar relations and I get no errors so i'm a bit clueless as to what's going wrong. Any help would be awesome, thank you!
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-05-01. Score: 1
  
  What strikes me as interesting here is your dump contains a Model instance.
  
  For example you said:
  
  > When I dump the response from the service method i'm getting back is:
  > Craft\Placid\_ResourceModel Object
  
  When I query findAll through a CActiveRecord derived class I get an array of Records. In order to populate this into models, the populateModel(s) methods are typically used.
  
  If you have a relation at the ActiveRecord level, it is a pseudo-attribute and will only be carried through to the model if the attribute is defined as defined attributes can only be mass assigned. Yii also supports rules definitions to mark attributes as safe, but this is almost entirely optional.
  
  The question is really why you have a model being output.
  
  E.g. for me the following code:
  
  ```
  $records = SomePlugin_ProductRecord::model()->with('pricing')->findAll($criteria);
  var_dump($records[0]);
  
  ```
  
  Results in:
  
  ```
  object(Craft\SomePlugin_ProductRecord)#179 (14) {
  
  ```
  
  Do you have any code which is auto inflating into a model? or are you calling populateModel(s)?
  
  If so, then you have made the correct call of creating the attribute in your model if you want to access it from there, as far as I've experienced.
  
  > **Comments:**
  > **username_0**: Hey @username_1, thanks for the answer and info, I haven't had time to look into this but when I have i'll update, as far as I know I don't think I'm inflating anything as it's dumping straight from `return Placid_ResourceRecord::model()->with('endpoints')->findAll($options);` but i'll double check to be sure.  
  
  ---
  
  ### Answer by username_2 on 2015-04-13. Score: 0
  
  What happens if you do something like this:
  
  ```
  $resourceRecords = Placid_ResourceRecord::model()->with('endpoints')->findAll($options);
  
  foreach ($resourceRecords as $resourceRecord)
  {
      echo "Found ".count($resourceRecords)." endpoints for resource: ".$resourceRecord->id."<br/>";
  }
  
  ```
  
  > **Comments:**
  > **username_0**: That just output the count of the resources, not the related endpoints. But I see what you mean. I tried putting `count($resourceRecord->endpoints)` but got the model attribute not found.So I added a `endpoints => AttributeType::Mixed` to the model and now they are coming through, but this will be the same whether ::Bool, ::String, ::Mixed, ::Number. Which seems a bit strange?  When I was adding the relations I was looking at the 'CategoryGroupsModel` and they weren't defined there, which made me think they shouldn't need to be perhaps..  
  
  ---
  
  