<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>

CC BY-SA 3.0
2015-05-08T14:09:54.330Z
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.
Alec Ritson
CC BY-SA 3.0
2015-05-01T14:06:49.410Z
2015-05-01T14:06:49.410Z
Jay
<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>

CC BY-SA 3.0
2015-04-14T14:30:51.243Z
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..
Alec Ritson
CC BY-SA 3.0
2015-04-13T20:57:43.433Z
2015-04-13T20:57:43.433Z
Brad Bell
<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>

<plugin-development>
Plugin eager loading not loading relations
# 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 @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.  

---

### 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."";
}

> **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..  

---

