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

Commit ebbfd6e4 by Rafael Martins

Adding support for filter through hasMany connector

1 parent 3e29e69f
......@@ -65,15 +65,15 @@ module.exports = (function() {
dataType = dataType.replace(/BIGINT/, 'INTEGER')
}
// SQLite thinks that certain modifiers should come before the length declaration,
// whereas other dialects want them after, see http://www.sqlite.org/lang_createtable.html.
// SQLite thinks that certain modifiers should come before the length declaration,
// whereas other dialects want them after, see http://www.sqlite.org/lang_createtable.html.
// Start by finding the index of the last of the modifiers
['UNSIGNED', 'BINARY', 'ZEROFILL'].forEach(function (modifier) {
var tmpIndex = dataType.indexOf(modifier)
if (tmpIndex > modifierLastIndex) {
modifierLastIndex = tmpIndex + modifier.length
modifierLastIndex = tmpIndex + modifier.length
}
})
if (modifierLastIndex) {
......@@ -345,6 +345,7 @@ module.exports = (function() {
, attributePart = associationParts.pop()
, self = this
return associationParts.every(function (attribute) {
var association = self.findAssociation(attribute, dao);
if (!association) return false;
......@@ -382,6 +383,14 @@ module.exports = (function() {
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName)
joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.identifier)
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 {
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName)
joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.source.autoIncrementField)
......
......@@ -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!