[NFR] Model find conditionally include or exclude related table
After reading the documentation on model at http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model.html, I've find that all related model will be included by default. I'm worrying about speed lost and resources it consume when scaling.
From other frameworks experience, will it be nice to do something like
// fetches all related model
$robot = Robots::findFirst(array("type = 'virtual'", "order" => "name"));
//fetches only robotParts model
$robot = Robots::findFirst(array("type = 'virtual'", "order" => "name","contain"=>array("robotParts")));
// fetches no model
$robot = Robots::findFirst(array("type = 'virtual'", "order" => "name","contain"=>array("")));
and is the source code inside cphalcon-master\build\64bits\phalcon.c ? Perhaps it will be more practical after looking on how findFirst function ( sadly still having trouble looking into the source code )
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
any workaround for this? I'm also facing the same problem.
Related models won't be selected from database unless you actually request them through property or getter method.
// Get all robot parts related to robot
$robot->robotParts;
// Or
$robot->getRobotParts();
If you need more conditions on related model then you will have to do custom getter method manually. Read here: Magic Getters vs. Explicit methods
I was under the impression that Phalcon supported this exact functionality: the ability to LEFT/INNER JOIN a ForeignKey relationship. Ex:
Product::find('...')->select_related('Site');
echo $product->title;
echo $product->site->name; // No query executed since data was already pulled
// with a LEFT JOIN on the 'product' table.
But it seems this is not the case.
Similar to Django's ORM: https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related
Duplicate of #1117