Skip to content

hasMany countAccessor #68

Closed
Closed

Description

@czkody

Hello,
is possible to add countAccessor to Many association?

Model

var db = config.db;
var Category = db.define('category', {
    //properties
    id : Number,
    name : String
},{
    //options
}); 
Category.hasMany("successors", {distance : Number}, Category, {reverse: 'predecessors', mergeTable: 'category_tree', mergeId: 'predecessor_id', mergeAssocId: 'successor_id'});

Controller

var Category = require('../models/Category');
exports.countSuccessors = function(req, res, next) {
    category_id = Number(req.params['category']);
    Category(category_id).countSuccessors({distance:1}, function (err, count) {     
        res.json(count);
    });

}

Activity

  1. dresende commented on Mar 8, 2013

    @dresende
    Owner

    Yes, absolutely. But you might need to use the git version for a while when I implement it.

  2. dresende commented on Mar 8, 2013

    @dresende
    Owner

    Wait.. (so many stuff I sometimes forget about features). Try this:

    Controller

    var Category = require('../models/Category');
    exports.countSuccessors = function(req, res, next) {
        category_id = Number(req.params['category']);
        Category(category_id).getSuccessors({ distance: 1 }).count(function (err, count) {      
            res.json(count);
        });
    }

    Explanation: calling the hasMany.getAccessor without callback makes it return a ChainFind instance. Look here:

    https://github.com/dresende/node-orm2/blob/master/lib/ChainFind.js#L31

  3. czkody commented on Mar 10, 2013

    @czkody
    Author

    Hi,
    I have just tried , but your code produced this query:

    SELECT COUNT(*) AS `c` FROM `category` WHERE (`distance` = 1)

    Have last versions of orm and sql-query.

  4. dresende commented on Mar 10, 2013

    @dresende
    Owner

    I have to look at this closer, it seems the chain is not taking care of the join option..

  5. dresende commented on Mar 11, 2013

    @dresende
    Owner

    I think I solved it, please try the latest git.

  6. czkody commented on Mar 11, 2013

    @czkody
    Author

    Hi,
    Yes, you made it. Works fine. Thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions