Parse iOS SDK: Select columns from compound query - ios

I'm using the Parse iOS SDK to pull data from my Parse backend. What I would like todo is create a compound query, so a query with a subquery as a where condition, but also have it bubble up properties from the subquery.
For example, lets say I have an object called car and I have an object called dealership. Now I want to pull the dealership that a particular car is located at based on some query parameters. This is where the compound query comes in.
I know how to create the compound query to only return cars that belong to a particular dealership, but how would I return the "name" property from the dealership object on my car object?
The idea here is to mimic a left join in SQL to return data from a joined table.
Any ideas how one would do this?

Related

Check if PFObject is in PFRelation

I am building an iOS in Swift using Parse.com as my backend.
I have a table of objects: car, and each car can be owned by multiple users, so I have a Car table with the column owners which is a PRRelation of the _User table.
I am displaying all the cars in a TableView and want to determine (for each object) whether the PFUser.currentUser() is in the Relation of _User objects for each car.
Is there a way of doing this without creating a query which then makes a request to the Parse server? Doing that seems very inefficient to have to check again for each object, and would make a large number of Parse database calls which would make me hit the call limit quite quickly if multiple people are using the app...
So is there a way to simply do something like:
if carObject["owners].contains(PFUser.currentUser()) {
println("the current user is an owner of this car")
}
Might it be possible to run a query of all cars, and then another query of all the cars with a whereKey restriction on the students column and then comparing queries? How could I compare the queries?
Have you created your car class in your app? You can download all your car objects from parse at once, put them in an [Car] and then you'll have all the relational data as well.
I'm not 100% sure but you may need to use parsequery.includeKey("users") when you query parse so it also includes the parse user. User's being an attribute of Car.

Querying for all rows matching multiple tags

I have a object called photo with an attribute called multitag of type array. For ex photo A has tag desk, photo B has tag chair and photo C has tags desk,chair. If user searches for desk, A is returned. If user searches for chair B is returned. If user searches for desk chair C is returned first folllowed by A and B. How do you do that using PFQuery?
I don't think I would do this particular type of data relationships using tags in arrays. You are better off using pointers or relations for this task.
For example:
the Photo class has a column tag which is a ParseRelation column type that points to the Tag class. For each photo in the Photo class you will add as many Tag objects to the Photo.tag column as you wish. This makes it very easy to query for and it is scales better than using arrays.
Here is the Parse guide on using Relations

Model.find_by_sql vs connection.query, which one is better?

In rails-
I need to execute a sql query against the database; the query is not related to any specific Model it can have mix data from multiple tables or from some other table. I have ways to do this=-
first is by executing query agains Model and capture the result like this-
res=User.find_by_sql("select * from customers joins and conditions")
res=User.find_by_sql("select * from [other table] joins and conditions")
Problem with this approach, I am not feeling comfortable with it because in User class objects I am capturing data of other table. like the first query result has data from customer table so in the user object I got customer's attribute. And more interesting problem is- if the resulting query has id attribute then
res.first.id will be id of customer and
if User model has relation with UserRoles model and if I access this relation with res.first.roles then it will fetch roles from UserRole for customer id, which is completely wrong.
And there might be may problems also.
So I think it has lot of chaos.
And good part is we dont not need to deal with connection and result would be an array of objects. so accessing object attributes with res.first.id is easier the hast like row["id"].
and second approach to use ActiveRecord connection and execute the query like
this res = ActiveRecord::Base.connection.query("sql query")
in place of query we can use select_one, select_all and can also make query parameterized.
The problem it has is it returns array of hash, but I need array of objects for easy accessibility in code. So I wrote a class to convert hash to object (I think rail does same thing in background) and is working fine.
So I need some suggestion on both the approach and need to decide which one is better.
First find_by_sql vs select_all
find_by_sql, This method returns an array of objects by initiating them.
users = User.find_by_sql("SELECT * FROM users"); #=> [#, #, #, # ....]
Accessing properties
users[0].name #Getting property in object oriented fashion
select_all, This method returns an array of objects but does not initiate them, and each object represents a row of database.
users = User.connection.select_all("SELECT * FROM users"); #=> #
Accessing properties
users[0]["name"] #Getting property in non-object oriented fashion
Whether find_by_sql is better, because of it is a simple way of custom querying to the database and returns instantiated objects

ListItemRenderer 'data' parameter

I am using Listitemrenderer(Zk UI and Grails) to render a list of books from database table called "book". I have different types of books in the table. the book types are determined by the bookType attribute. The issue I am having is that when I do:
Book bookInstance = (Book) data
it shows null point exception when there are different types of books in the table. But in the case when there are books of a single type, it works fine.
Is there a way to filter the parameter 'data' of Listitemrenderer so that I could get books of a certain type I want? Again, there is an attribute bookType in database table that distinguishes different types of book.
Thank you!!!
you can use Query String with 'where' clause specifying 'book_type'.
this way use can fetch the desired result.

rails query serializable object

How do I query the database to find objects that contain one or more attributes that are stored as serializable?
For example, I have a concert which occurs only in certain cities. I want to make a Concert object with a column called cities and store an array of cities.
If I want to query my database to find all concerts that occur in 1 city (or all concerts that occur in an array of n cities), how do I do this?
The best way to do this isn't to store it in a serialized column, but a separate table called Cities. Then you can do this:
City.find_by_name('Cityname').concerts
One possible way to query would be to use SQL's LIKE condition. This would work for boolean conditions in serialized tables.
For example to find those Users with the 'notification' option on,
users=User.arel_table
User.where(users[:options].matches("%notification: true%"))
As for other type of variables this would not be as feasible.

Resources