Sailsjs Mysql ORM multiple query on the same table

2019-03-01 12:40发布

Im using sails v 0.10.5 and latest sails-mysql

I have a Restaurant filtering system

Venue.find().populate('comments', {
        deleted: false
    }).where({
            restaurant_services: {contains: '"delivery":1'},
            restaurant_services: {contains: '"takeout":1'},
            restaurant_specialties: {contains: '"breakfast":1'}
    })

Now the problem is when I get the data from the client, I do not know how many items the user has selected for restaurat_services, so obviously I have to create a dynamic JSON object for the .where() function

The problem is though, I cannot do this

var searchObj = {};
searchObj['restaurant_specialties'] = {contains: '"breakfast":1'}; 

searchObj['restaurant_specialties'] = {contains: '"breakfast":1'};

So as you may see the previous setting of the value gets replaced the second time,

Any help would be greatly appreciated from the Smart people out therehere but does not work

Model.find({
  name: { 'contains' : ['Walter', 'Skyler'] }
});

2条回答
来,给爷笑一个
2楼-- · 2019-03-01 12:57

In order to do this based on how waterline works you need different fields to search on.

You can do this by created aliased attributes in your model.

venue.js
module.exports.attributes = {
    restaurant_services:'string',
    restaurant_services_1: {type:'string',columnName: 'restaurant_services'}
    restaurant_services_2: {type:'string',columnName: 'restaurant_services'}
    restaurant_services_3: {type:'string',columnName: 'restaurant_services'}
    restaurant_services_4: {type:'string',columnName: 'restaurant_services'}
    restaurant_services_5: {type:'string',columnName: 'restaurant_services'}
}

Then you can do

Venue.find().populate('comments', {
        deleted: false
    }).where({
            restaurant_services: {contains: '"delivery":1'},
            restaurant_services_1: {contains: '"takeout":1'},
            restaurant_specialties: {contains: '"breakfast":1'}
    })

Its hacky, but it works

查看更多
叼着烟拽天下
3楼-- · 2019-03-01 13:14
Venue.find().populate('comments', {
    deleted: false
}).where({
        restaurant_services: {
            contains: '"delivery":1',
            contains: '"takeout":1',
        },
        restaurant_specialties: {contains: '"breakfast":1'}
})

that you need

Venue.find({
  where:{
    restaurant_services: {
      contains: '"delivery":1',
      contains: '"takeout":1',
    },
    restaurant_specialties: {
      contains: '"breakfast":"1"'
    }
  }
}).populate('comments', {
    deleted: false
}).exec(console.log);
查看更多
登录 后发表回答