Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hasMany countAccessor #68

Closed
czkody opened this issue Mar 8, 2013 · 6 comments
Closed

hasMany countAccessor #68

czkody opened this issue Mar 8, 2013 · 6 comments

Comments

@czkody
Copy link

czkody commented Mar 8, 2013

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

}
@dresende
Copy link
Owner

dresende commented Mar 8, 2013

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

@dresende
Copy link
Owner

dresende commented Mar 8, 2013

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

@czkody
Copy link
Author

czkody commented Mar 10, 2013

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.

@dresende
Copy link
Owner

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

dresende added a commit that referenced this issue Mar 11, 2013
This is required when using the countAccessor from hasMany associations
to be able to JOIN tables
@dresende
Copy link
Owner

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

@czkody
Copy link
Author

czkody commented Mar 11, 2013

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

@czkody czkody closed this as completed Mar 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants