balderdashy / waterline-docs

WARNING: The content in this repo is out of date! See https://github.com/balderdashy/sails-docs for the most up-to-date documentation
452 stars 161 forks source link

Multi-column association / condition #28

Closed dcolley closed 9 years ago

dcolley commented 10 years ago

I'm porting our api from yii to sails.js. In the Yii model for "Project" I have a 'relation' to "Comment" as follows:

    public function relations() {
        return array(
            'comments' => array( self::HAS_MANY, 'Comment', 'obj_id', 'condition'=>'obj = "Project" AND comments.status='.Comment::STATUS_APPROVED, 'order'=>'comments.create_time DESC'),
            'commentCount' => array(self::STAT, 'Comment', 'obj_id', 'condition'=>'obj = "Project" AND status='.Comment::STATUS_APPROVED),
        );
    }

How can I model this in sails.js?

I have created a model attribute function

    comments: function( cb ) {
        Comment.find({ obj: "Project", obj_id: this.id }).exec( function(err, list) {
            cb( err, list );
        });
    }

... but it should be possible via associations... :/ Is there any documentation on this?

micha-LEAP commented 10 years ago

This has similarities to what I have asked here: http://stackoverflow.com/questions/26837605/associations-with-combination-foreign-key-in-sails-js-waterline.

Having also come from Yii, I am used to being able to structure my data this way. I want to have a common association across multiple models - with a single join table OR a combination PK - the table name (or 'Type') and the ID. There's no way to define this in waterline syntax that I have been able to find.

devinivy commented 9 years ago

Currently there's no way to create this sort of association using waterline. If you think this is a missing feature, feel free to raise a feature request over on the waterline repo. For the time being, I would suggest adding a custom model method rather than an attribute.