Restkit: Post an array of id's instead of the complete objects - json

I have a case where I need to post and array of numbers to the server side. E.g:
I have an order that have a list of products with one to many relation ships.
The question is how to post only array of id's for current order:
Now I have such result:
{"orders":[{"id":0,"Products":[{"productId":6},{"productId":7},{"productId":5},{"productId":3}]},{"id":1,.....]}
But I want to make it
{"orders":[{"id":0,"Products":[6,7,5,3]},{"id":1,.....]}
Any ideas ?
Update 1
I think I have found a solution
I have add method that fetch id's of my order products it's look like this
-(NSArray *)productsIds {
return [self.products.array valueForKeyPath:ProductAttributes.productId];
}
and add such a thing to my mapping code part.
[orderSerializationMapping addAttributeMappingsFromDictionary:#{#"productsIds" : #"Products"}];
And now result is
{"orders":[{"id":5,"Products":[17,13,16,11,12,19]},......]

You don't need add a method to fetch the id's. Setup your mapping like this:
[orderSerializationMapping addAttributeMappingsFromDictionary:#{#"products.productID": #"Products"}];

Related

Rails, MySql, JSON column which stores array of UUIDs - Need to do exact match

I have a model called lists, which has a column called item_ids. item_ids is a JSON column (MySQL) and the column contains array of UUIDs, each referring to one item.
Now when someone creates a new list, I need to search whether there is an existing list with same set of UUIDs, and I want to do this search using query itself for faster response. Also use ActiveRecord querying as much as possible.
How do i achieve this?
item_ids = ["11E85378-CFE8-39F8-89DC-7086913CFD4B", "11E85354-304C-0664-9E81-0A281BE2CA42"]
v = List.new(item_ids: item_ids)
v.save!
Now, how do I check whether a list exists which has item ids exactly matches with that mentioned in query ? Following wont work.
list_count = List.where(item_ids: item_ids).count
Edit 1
List.where("JSON_CONTAINS(item_ids, ?) ", item_ids.to_json).count
This statement works, but it counts even if only one of the item matches. Looking for exact number of items.
Edit 2
List.where("JSON_CONTAINS( item_ids, ?) and JSON_LENGTH(item_ids) = ?", item_ids.to_json, item_ids.size).count
Looks like this is working
You can implement a has many relation between lists and items and then access like this.
List.includes(:item).where('items.id in (?)',item_ids)
To implement has_many relation:
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

Get array of friend names with friend id's from MongoDB

I'm making MEAN app for the first time. I managed to insert users into MongoDB. Each user looks like:
{
"name":"My Name",
"country":"Italy",
"friends":"5922c66b4e708e27a02b7937"
}
My first question is, how to manualy insert more than one friend (how to separate them via Postman)?
Then I have html page to show those informations about user, so I display his friends that way:
<td>{{user.userFriends}}</td>
When I will have more than one firend in database, how to display number of friends instead of their IDs?
Thanks for help!
EDIT: Solved the first part with
{
"friends":["5922c66b4e708e27a02b7937","5922c66b4e708e27a02b7938"]
}
To get Mongo to calculate the number of friends a user has, you can use the $size operator on the friends array. You need to use the aggregation pipeline to do this though. Otherwise, you can just calculate the length of the array yourself. For more details on the $size operator and the aggregation pipeline, see here: https://docs.mongodb.com/manual/reference/operator/aggregation/size/

cakephp retrive data from one table excluding the associated tables

I am struggling with a basic problem. i am using cake php 2.5. i try to apply the find query in the company model and receiving all the data from companies and with its associations, but i only want to receive the data from company table and want to exclude the data from rest of relationships, can anyone help me with this. below are my queries.
$this->loadModel('Company');
$fields=array('id','name','logo','status');
$conditions=array('status'=>1);
$search_companies = $this->Company->find('first',
compact(array('conditions'=>$conditions,'fields'=>$fields)));
print_r($search_companies);die();
echo json_encode($search_companies);die();
With out seeing your data output, I am just going to take a stab at the problem.
Inside your $search_companies variable you are getting a multidimensional array probably with the other values of the other tables.
Why not just select the one array:
$wantedData = $search_companies['Company'];
// The key Company (which is the model) should be the data you are wanting.
Try setting model's recursive value to -1
$this->Company->recursive = -1;
$search_companies = $this->Company->find('first',
compact(array('conditions'=>$conditions,'fields'=>$fields)));
With this you will not fire the joins queries and therefore you only retrieve model's information.
Cakephp provide this functionality that we can unblind few/all associations on a any model. the keyword unbindModel is used for this purpose. inside the unblindModel you can define the association type and model(s) name that you want to unblind for that specific association.
$this->CurrentModelName->unbindModel(array('AssociationName' => array('ModelName_Youwwant_unblind')));

Laravel 4 Eager Loading filtering and selecting only matching results

I'm trying to output the filter results with only matching elements.
I have two tables (in the real project, which will be 5), let's say companies and projects. A company may have more than one project or may not have any.
These are the relations:
/app/models/Company.php
<?php
class Company extends Eloquent {
public function projects() {
return $this->hasMany('Project','companyID');
}
protected $table = 'companies';
}
/app/models/Project.php
<?php
class Project extends Eloquent {
public function companies() {
return $this->belongsTo('Company','companyID');
}
}
What I want to do is, I want to get results of them both but only with matching parameters.
I've tried this:
return Company::with(array('projects'=>function($query){
$query->where('id',99); //project's id is 99
}))->get();
This is the output JSON
If I change the value from 99 to 1 (there is a result with products.id of 1), it changes into this:
I only want to get the second result from the second JSON i've posted.
As you can see in the second JSON (I'm using this parser to check), all companies are loaded regardless of the project, and only the rows matched have the object projects.
There will be more 'with's and I don't know how to filter only matching elements.
I also tried having() inside closure, but it's still same:
$query->having('projects.id','=',99);
Is there a way to filter only matching results (without using a loop) which the output will only include the results having the matched projects object?
Edit: Actually, 5 tables will be filtered.
companies, projects, works, users and user_works
Let's say;
"Companies" have many projects.
"Projects" have many works
"Works" have many users, also "Users" may have more than one work (pivot table user_works).
All relations are set correctly from models.
I want to do a global searching to these.
Something like: "Bring me the user id 1's works which has company id of 5 and project id of 4", but none of the fields are mandatory.
So these are also valid for searching: "Bring me everyone's works on project id of 2", or "bring me id 2's works", or "bring me all the works starting from today", "bring me the id 1's works on project 2", "Bring me this year's works done of company id 1".
Thanks in advance
Using whereHas() is the solution on my case. It filters relation and affects the results returned in the main query.
If you have the id of the project, would it make more sense to go that route? $project = Project::find(99); and then the company variables would be accessible with $project->companies->name;
It would make more sense to rename the companies() function to company() because a project will only ever belong to one.

JSONpath fields with same name

I'm trying to get the user id's from twitter response (asking for several users)
https://dev.twitter.com/docs/api/1/get/users/lookup
But if i try
$.[*].id OR $..id
all fields with id (even nested fields) are retrieved.
How can i get the "id" field placed at first level?
I believe you would only get the first-level id's with the following query $[*].id. I believe this is what you were asking for; let me know if I misunderstood you.