I've been struggling with this for a while and I'm at a loss.
I have 2 related classes Organization and Campaigns
class Organizations extends \Phalcon\Mvc\Model
{
public $id;
public function getSource()
{
return "bwg_entities";
}
public function initialize() {
$this->hasMany("id", "Users", "entity_id" );
$this->hasMany("campaign_id", "Campaigns", "id", array("foreignKey" =>true));
}
....
}
class Campaigns extends \Phalcon\Mvc\Model
{
public $id;
public $entity_id;
public function getSource()
{
return "bwg_campaigns";
}
public function initialize() {
$this->belongsTo("entity_id", "Organizations", "id" );
$this->hasMany("id", "CampaignDatafields", "campaign_id");
$this->skipAttributesOnCreate(array('date_created'));
}
....
}
In my controller, I retrieve organizations but I never get back the related campaigns.
$organization = Organizations::findFirst(array(
"entity_code = :org_id: AND is_active = 1",
"bind" => array('org_id' => $orgId)
));
foreach($organization->campaigns as $c) {
echo json_encode($c);
}
exit;
}
I've tried using getRelated() fail. i've tried above code with $organization->getCampaigns().. no error but no result.
Finally, from my mysql :
select e.*, c.entity_id, c.name from bwg_entities e left join bwg_campaigns c on c.entity_id = e.id;
returns:
1000 HRC Human Rights Campaign Steven Churchill [email protected] 1 1000 HRC dev
so the data is there.