1 Reply Latest reply on Jul 28, 2016 6:20 AM by Rens van der Linden

    Get results with a like condition in JavaScript

    Rens van der Linden

      Hi,

       

      I'm trying to get the results of a find query with a like condition in JavaScript. But i don't know how to do this.

       

      I've found this Ruby solution: (Rhomobile | Rhom Ruby API ):

      Product.find(
        :all,
        :conditions => {
        {
         :func => 'LOWER',
         :name => 'description',
         :op => 'LIKE'
        } => query,
        {
         :func => 'LOWER',
         :name => 'title',
         :op => 'LIKE'
        } => query
        },
        :op => 'OR',
        :select => ['title','description']
      

       

      Product.find(
       :all, 
       :conditions => [ 
         "LOWER(description) like ? or LOWER(title) like ?", 
        query, 
        query
       ], 
       :select => ['title','description'] 
      )
      

       

       

      The like query isn't described in the JavaScript guide, so i've tried this: (Rhomobile | Using Rhom in JavaScript )

      var objs = contact.find("all", 
          offset: offset,
          per_page: limit,
          conditions: "LOWER(name) like rens"
      });
      

       

      var objs = contact.find("all", {
          offset: offset,
          per_page: limit,
          op: 'LIKE',
          conditions: "LOWER(name) like rens"
      });
      

       

      var objs = contact.find("all", {
          offset: offset,
          per_page: limit,
          conditions: {
              name: 'rens',
              op: 'LIKE'
          }
      });
      

       

      It doesn't work for me. What is the right way to do this in JavaScript?

       

      Can someone help me? Thanks.

        • Re: Get results with a like condition in JavaScript
          Rens van der Linden

          The solution is to execute a sql query like this:

           

          SELECT L.object AS object, L.VALUE AS column1, R.VALUE AS column1
          FROM object_values L INNER JOIN object_values R ON L.object = R.object
          WHERE L.attrib = column1
          AND R.attrib = column2
          AND column1 LIKE '%test%'
          

           

          The model I used is a PropertyBag. So I can't use "SELECT * FROM tablename". When I switch to a fixedModel this will be possible, but there are a lot of disadvantages. See more info: Rhomobile | Using Rhom in Ruby

           

          Use this to execute the query:

           

          var database = new Rho.Database(Rho.Application.databaseFilePath('user'),'user');
          var result = database.executeSql("here_the_query");
          

           

          And then loop through the results...