Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

I'm unable to get related records using the magic getters

I am trying to figure out how to get my records here... I am querying a join table and want to get the records (from Contacts). I am pretty damn sure that my relationships are setup correctly. I've tried to $contactsInGroup->getContacts too, which seemed it whould work, but does not.

Any help appreciated. I like what I read about Phalcon, but I must admit, it's been a little rocky getting to full speed.

If it's not clear, the error I am getting is: Call to undefined method Phalcon\Mvc\Model\Resultset\Simple::getContacts()

            $ContactGroupsContacts = new ContactGroupsContacts();
            $contactsInGroup = $ContactGroupsContacts->find(array(
                'conditions' => "contact_group_id = '{$contactGroupId}'"
            ));  // works, but seems to return an instance of Simple..  not what I expected

            foreach ($contactsInGroup->contacts as $contact) { // does not work. ->contacts does not exist
                print_r($contact->email);
            }

This also does not work.. if I read your suggestion correctly:

// find all subscribers in this group
$ContactGroupsContacts = new ContactGroupsContacts();
$params = array('conditions' => "contact_group_id = '{$contactGroupId}'");
foreach ($ContactGroupsContacts->find($params)->getContacts() as $contact) {
        print_r($contact);
}

ContactGroupsContacts

/**
 * Join table between Contacts and ContactGroups
 *
 * @author joel
 */
class ContactGroupsContacts extends \Phalcon\Mvc\Model {

    public $contact_group_id;
    public $contact_id;

    public function initialize() {
        $this->belongsTo("id", "ContactGroups", "contact_group_id");
        $this->belongsTo("id", "Contacts", "contact_id");
    }

   /**
     * Return the related "contacts"
     * (unecessary, but will help IDE hinting)
     * 
     * @return \Contacts[]
     */
    public function getContacts($parameters = null) {
        return $this->getRelated('Contacts', $parameters);
    }

}

Contacts

/**
 *
 * @author joel
 */
class Contacts extends  \Phalcon\Mvc\Model {

    public $id;
    public $uid;
    public $account_id;
    public $first_name;
    public $email;
    public $status;
    public $creation_method;
    public $email_format;
    public $date_created;
    public $date_modified;
    public $hb_on_upload;
    public $is_test_contact;

    public function initialize() {
        $this->hasMany("id", "ContactsAttributes", "contact_id", array(
            //'alias' => 'contactattributes'
        ));
        $this->hasMany("id", "ContactGroupsContacts", "contact_id");
    }
}

ContactGroups

class ContactGroups extends \Phalcon\Mvc\Model {

    public $id;
    public $type;
    public $name;
    public $description;
    public $status;
    public $last_modified_date;
    public $xml_contact_query;
    public $project_id;

    public function initialize() {
        //parent::initialize();
        $this->hasMany("id", "ReportTickets", "contact_group_id");
        $this->hasMany("id", "ContactGroupsContacts", "contact_group_id");
    }

}

Try not querying the intermediate model if this is a n-n relationship. This is based on my guess of your models. If this doesnt work then edit your post to include the name of your models, and the the relationships you defined in their initialize() function

foreach (ContactGroups::findFirstById($contactGroupId)->getContacts() as $contact)
{
    ...
}

Please show your models and relationship setup code.

edited May '14

original post has been updated with the models. thank you for having a look.

@david-duncan: I did try what you were actually suggesting as well. I misread it the first time.

So, given a contact group id, you want to get all contacts associated with that group?

Since many contacts can belong to many contact groups, what you want to set up is a many-to-many relationship between Contacts and ContactGroups, via ContactGroupContacts (say that quickly 5 times).

I've never personally created a many-to-many relationship, but the docs do cover it here: http://docs.phalconphp.com/en/latest/reference/models.html#relationships-between-models

Yes, it is a many-to-many. My original post has all 3 models now.

I was following the RobotsParts example, but something ain't right..



47.6k
Accepted
answer

I think you want to define your relationship in ContactGroups like this:

$this->hasManyToMany(
    "id",
    "ContactGroupsContacts",
    "contact_group_id", "contact_id",
    "Contacts",
    "id"
);
edited May '14

@quasipickle - I think you're right. That is what I was missing. I saw the first set of relationships for the 3 models in the docs, then failed to see that you also need to set $this->hasManyToMany().

I now have foreach code returning my Contacts properties.

Thank you very much for your time.

Working foreach()

foreach (ContactGroups::findFirstById($contactGroupId)->getContacts() as $contact) {
    print_r($contact->email);
}

Glad to help.