不要怂,就是干,撸起袖子干!

Commit ebbfd6e4 by Rafael Martins

Adding support for filter through hasMany connector

1 parent 3e29e69f
...@@ -345,6 +345,7 @@ module.exports = (function() { ...@@ -345,6 +345,7 @@ module.exports = (function() {
, attributePart = associationParts.pop() , attributePart = associationParts.pop()
, self = this , self = this
return associationParts.every(function (attribute) { return associationParts.every(function (attribute) {
var association = self.findAssociation(attribute, dao); var association = self.findAssociation(attribute, dao);
if (!association) return false; if (!association) return false;
...@@ -382,6 +383,14 @@ module.exports = (function() { ...@@ -382,6 +383,14 @@ module.exports = (function() {
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName) joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName)
joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.identifier) joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.identifier)
joins += ' = ' + self.quoteIdentifiers(association.target.tableName + '.' + association.target.autoIncrementField) joins += ' = ' + self.quoteIdentifiers(association.target.tableName + '.' + association.target.autoIncrementField)
} else if (association.connectorDAO){
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.connectorDAO.tableName)
joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.source.autoIncrementField)
joins += ' = ' + self.quoteIdentifiers(association.connectorDAO.tableName + '.' + association.identifier)
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName)
joins += ' ON ' + self.quoteIdentifiers(association.connectorDAO.tableName + '.' + association.foreignIdentifier)
joins += ' = ' + self.quoteIdentifiers(association.target.tableName + '.' + association.target.autoIncrementField)
} else { } else {
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName) joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName)
joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.source.autoIncrementField) joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.source.autoIncrementField)
......
...@@ -137,4 +137,58 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() { ...@@ -137,4 +137,58 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
}); });
}) })
}) })
it('can filter through hasMany connector', function(done) {
var User = this.sequelize.define('User', {username: DataTypes.STRING })
, Project = this.sequelize.define('Project', { title: DataTypes.STRING })
Project.hasMany(User);
User.hasMany(Project)
this.sequelize.sync({ force: true }).success(function() {
User.bulkCreate([{
id: 101,
username: 'leia'
}, {
id: 102,
username: 'vader'
}]).success(function() {
Project.bulkCreate([{
id: 201,
title: 'republic'
},{
id: 202,
title: 'empire'
}]).success(function() {
User.find(101).success(function(user){
Project.find(201).success(function(project){
user.setProjects([project]).success(function(){
User.find(102).success(function(user){
Project.find(202).success(function(project){
user.setProjects([project]).success(function(){
User.findAll({
where: {
'projects.title': 'republic'
}
}).success(function(users){
try{
expect(users.length).to.be.equal(1);
expect(users[0].username).to.be.equal('leia');
done();
}catch(e){
done(e);
}
})
});
});
});
});
});
});
});
});
})
})
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!