I'm new to Phalcon and have a relation set up between managers and agents.
Each manager has many agents, and each agent has only 1 manager.
I have a table/model of survey data in which each agent can have many surveys, but each survey only has 1 agent.
Manager Model:
<?php
use Phalcon\Mvc\Model;
class Manager extends Model
{
public $id;
public $clientId;
public function initialize()
{
$this->hasMany('client_id','Agent','supervisor_id');
}
}
Agent Model:
<?php
use Phalcon\Mvc\Model;
class Agent extends Model
{
public $id;
public $client_id;
public function initialize()
{
$this->belongsTo('supervisor_id','Manager','client_id');
$this->hasMany('client_id','Survey','client_id');
}
}
Survey Model:
<?php
use Phalcon\Mvc\Model;
class Survey extends Model
{
public $id;
public $client_id;
public $driver;
public $score;
public function initialize()
{
$this->belongsTo('client_id','Agent','client_id');
}
}
I don't have any issues utilizing these relationships. I am able to use this code to output each survey that belongs to a certain manager:
$manager = Manager::findFirstByClientId(648);
foreach ($manager->agent as $agent) {
foreach ($agent->survey as $survey) {
//$surveys[] = $survey->id;
echo $survey->id.' / '.$survey->score.' / '.$survey->client_id.'<br>';
}
}
I want to get a bit more control over this data and have yet to identify how. My main question is that if I do:
$manager = Manager::findFirstByClientId(648);
Which will pull manager id 648. Each survey has additional data that defines the surveys (id, client_id, driver, score). How can I continue to use the agents that belong to this manager, to further define the result set?
For example I'd like to pull surveys that belong to the agents that belong to manager 648, but also contain "driver 1". I've made some basic queries such as:
$query = $this->modelsManager->createQuery('SELECT * FROM survey');
$surveys = $query->execute();
But I'm not sure how I can insert my "array" or list of agents that belong to that manager into this query.
If this is too vague please let me know and I'll be more than happy to expand on anything.