2

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!

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.

| improve this answer | |
  • 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 May 8 '15 at 14:09
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/>";
}
| improve this answer | |
  • 1
    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 Apr 14 '15 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.