Hi All!
I have the same problem as david-duncan here and matt-sharp at https://forum.phalcon.io/discussion/1437/getting-all-children-grandchildren-etc-from-model-with-hasmany-r
In the original post matt-sharp wrote:
Customer hasMany Order
Order hasMany Product
Product hasMany Part
My solution for now is:
In a controller:
//obtaining the customer object
$customer = Customer::findFirst($id);
//obtaining the resultset of all parts that belong to the customer
$his_parts = $customer->getParts();
In a model:
then in the Customer
model I would define the following getParts()
method:
public function getParts() {
//obtain resultset
$parts = Part::query()
->innerJoin('Product')
->innerJoin('Order')
->innerJoin('Customer')
->where("customer_id = :customer_id:")
->bind(array("customer_id" => $this->getCustomerId()))
->execute();
return $parts;
}
In the above model my id field is customer_id
and its getter is getCustomerId()
method. I assume that all models has properly defined relations.
Please write what do you think about it? What other solution you are using?