cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Default set of conditions for related records

Open iforp opened this issue 13 years ago • 19 comments

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.

iforp avatar Jul 25 '13 10:07 iforp

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

iforp avatar Jul 25 '13 11:07 iforp

+1 Yes I face the same situation that makes me write many helper getter to get related records.

jeffreycahyono avatar Jul 25 '13 12:07 jeffreycahyono

+1 (#983)

jodator avatar Aug 11 '13 13:08 jodator

+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 ;)

codefocus avatar Nov 27 '13 18:11 codefocus

+1

netoec84 avatar Jan 12 '14 23:01 netoec84

+1

sergiors avatar Feb 19 '14 13:02 sergiors

+1

willianpts avatar Feb 19 '14 14:02 willianpts

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.

kaphert avatar Mar 25 '14 15:03 kaphert

+1

konsultaner avatar Apr 01 '14 14:04 konsultaner

Please write somewhere in the docs that this is not available yet. Spent ages trying to figure out what was wrong. +1

benfavre avatar May 15 '14 19:05 benfavre

+1

mattrmiller avatar Jul 09 '14 00:07 mattrmiller

+1

lothern avatar Jul 26 '14 12:07 lothern

Is this available now?

phoenixgao avatar Sep 10 '14 06:09 phoenixgao

+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

dugwood avatar Sep 22 '14 12:09 dugwood

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']
        ]);
    }
}

ghost avatar Oct 16 '14 02:10 ghost

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

samijnih avatar May 29 '15 23:05 samijnih

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.

loopsframework avatar Jun 10 '15 07:06 loopsframework

Hmm,

Yesterday my VPS was running Phalcon 2.0.2. I tested again my own code :

image

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!

samijnih avatar Jun 11 '15 18:06 samijnih

Indeed...

Maybe I was looking in the wrong place or my test data was faulty.

I am happy that it works for now.

loopsframework avatar Jun 12 '15 01:06 loopsframework