cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Model find conditionally include or exclude related table

Open nghenglim opened this issue 12 years ago • 4 comments

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.

nghenglim avatar Dec 30 '13 04:12 nghenglim

any workaround for this? I'm also facing the same problem.

2n2n avatar Jun 20 '14 03:06 2n2n

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

naNuke avatar Jun 20 '14 05:06 naNuke

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

csantosdev avatar Jun 29 '14 00:06 csantosdev

Duplicate of #1117

andresgutierrez avatar Jun 19 '15 19:06 andresgutierrez