[NFR] Default set of conditions for related records
There are 3 ways to get related records:
$robot->getRelated('parts', [array $parameters]);
$robot->getParts([array $parameters]);
$robot->parts;
Sometimes the third way is preferred, but it's unable to pass additional query options like Order, Limit, etc. It would be nice to be able to set these options in the model's relations initialization.
class Robots extends Phalcon\Mvc\Model {
// ...
public function initialize() {
$this->hasMany('id', 'RobotsParts', 'robot_id', [
'alias' => 'parts',
'conditions' => 'valid >= :dttm:',
'bind' => ['dttm', date('Y-m-d H:i:s')],
'order' => 'name DESC',
]);
}
}
class RobotsParts extends Phalcon\Mvc\Model {
// ...
public function initialize() {
$this->belongsTo('robot_id', 'Robots', 'id', ['alias' => 'robot']);
}
}
$robot = Robots::findFirst();
$robot->parts;
// WHERE `robot_id` = :0 AND `valid` >= :dttm: ORDER BY `name` DESC
// is applied to query
Want to back this issue? Place a bounty on it! We accept bounties via Bountysource.
Moreover I've got a question regarding this issue.
Call $robot->parts executes SQL query only ones.
While $robot->getParts() executes query for every call (the same query).
Should I create a bug report or this is normal behavior?
https://gist.github.com/Agent-J/c4e1a3afa9998ec13277
+1 Yes I face the same situation that makes me write many helper getter to get related records.
+1 (#983)
+1 I sort of expected this to be implemented already and spent ages looking through the docs. Good to know it's a known issue. Back to writing helper getters for now ;)
+1
+1
+1
Maybe a workaround in some cases, change the default ordering for your mysql table with:
ALTER TABLE `table_name` ORDER BY `field_name`
in my case this worked out.
+1
Please write somewhere in the docs that this is not available yet. Spent ages trying to figure out what was wrong. +1
+1
+1
Is this available now?
+1 on this. I think I'll handle it in Volt, so that I can keep the simplicity of $robot->robotsParts. I know it's bad to sort each and every time, but that's easier that rewriting ->getRobotsParts() everywhere.
@kaphert : your solution only works as long as no more data is inserted: http://dev.mysql.com/doc/refman/5.0/en/alter-table.html
Note that the table does not remain in this order after inserts and deletes
This is implemented in Phalcon 2 as follows:
class Robots extends Phalcon\Mvc\Model
{
public function initialize()
{
$this->hasMany('id', 'RobotsParts', 'robots_id', [
'params' => ['order' => 'id DESC']
]);
}
}
Hello,
When I use your solution as follows :
// one band has many info (due to the language)
$this->hasMany('id', 'Fymh\Api\Models\BandInfo', 'bid', [
'alias' => 'Info',
'foreignKey' => [
'action' => Relation::ACTION_CASCADE,
'message' => 'The info cannot be deleted'
],
'params' => [
'conditions' => 'language = "FR"'
]
]);
It doesn't work with 'conditions' but works fine with 'order'. May have I missed something?
Phalcon 2.0.0 PHP 5.6.9 dev
I can confirm the problem.
After checking method getRelationRecords in mvc/model/manager.zep I came to the following conclusion:
Parameter 'conditions' and 'bind' are being processed before merging the (extra) parameters from hasMany.
Moving the merging code to the top fixed the problem. Would love to see that in a future release but am not sure if it breaks anything else.
Hmm,
Yesterday my VPS was running Phalcon 2.0.2. I tested again my own code :

And this worked.. I don't know if I was doing something wrong or if it always worked ..
I just updated Phalcon from 2.0.2 to 2.0.3 and works again.
Have a try!
Indeed...
Maybe I was looking in the wrong place or my test data was faulty.
I am happy that it works for now.