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");
}
}