I am using Backand BaaS for my ionic app. My model consists of two tables:
1. Users
2. Trips
A one-to-many relationship has been established between 'Users' and 'Trips'.
'Users' has a collection field that is a collection of 'Trips' and 'Trips' has a owner field that is an object of 'Users'.
What is the correct way to create an object so that upon creating I am assigning the correct owner ('Users' object) to 'Trips' collection field ?
When POSTing a new Trip use this for the Trip Object
{
tripName:"asdf"
owner:1
}
In the users object it will look like this
{
id:1
fName:"david"
trips:[{__metadata: {id: 1}, tripName: "asdf"}]
}
Related
I have a table category it has a self relation to have belongs_to simply to be able to create subcategories and assign them to previously created categories and now i need to be able to either assign the new category to another (super) category or to assign it to none.
my model is :
public $belongsTo = [
'category' => ['plugin\pdf\models\Category']
];
This is found in the documents under backend -> forms. Look for the relation under widget fields. The option you is called emptyOption and it will be added in your fields.yaml file.
You would add the emptyOption like so to model's fields.yaml.
field:
label: Field Label
nameFrom: name
descriptionFrom: description
span: auto
type: relation
emptyOption: None
I have a Model named Player with 'id' and 'shortname'.
I have another Model named Team with 'id', 'teamname', 'player_1_id', 'player_2_id' and 'player_3_id'.
I am trying to use relations:
// in Model Team
public function players()
{
return $this->hasOne('App\Player', 'id','player_1_id')
->hasOne('App\Player', 'id','player_2_id')
->hasOne('App\Player', 'id','player_3_id');
}
// In controller
$resource = Team::with('players')->get(); doesnt work.
In this case, Which is the best(fastest) way to use eagerloading?
Thanks in advance...
You should read about database normalization. It does not make sense to store player_1_id, player_2_id and player_3_id in teams database. What if team will contain 20 players? You will create another fields?
You should either add team_id into Player model (in case players always belongs to only one team) or create extra table where you will store connections between players and teams.
So I'm working in an AngularJs app that is connected to a Kinvey backend. www.kinvey.com
In Kinvey we can fetch an entity and it's relations like this:
var promise = $kinvey.DataStore.get('events', 'event-id', {
relations : { location: 'locations' }
});
http://devcenter.kinvey.com/angular/guides/datastore#JoiningOperators
Relational Data / Fetching
The above is actually getting an event entity from the events collection and it's also fetching the data of the locations associated to it (which are in a different collection), so far so good we are able to retrieve an entity and it's associated entities ...
Now how could I retrieve the associations of those locations ?
In other words how can I retrieve nested associations ?
Thanks.
Like this:
$kinvey.DataStore.find('collection1', null, {
relations: {
name: "collection2",
"name.name": "collection3"
}
});
https://support.kinvey.com/discussion/201272198/nested-relations
I am trying to create a backbone client side application. I am receiving a list of json objects from the server on startup that will be a list of the possible tables exposed from the server, with their structure. E.g. Customers, Orders, Invoices, Employees
I want to create the models, collections and views dynamically based on the data I receive from the server.
Only when I receive the json on load will I know what the models should be and what the relationships between the models should be.
E.g. Customers structure might be Id, CustomerName, Address, Contact Numbers.
Order Structure might be Id, CustomerId, OrderDate, Amount
etc
By building Models, collections, views, controllers dynamically, I could in theory on startup point at another server who might give me a totally different set of tables e.g. : Movies, Actors etc.. with their structures.
Also, if additional fields are added I don't have to change the client side code again. E.g. Customer table might include a new field called ContactPerson
Please assist me as all the examples I saw on backbone is all based on statically defining the models on the client side up front. So create a model and collections and views for Customers, Orders, Invoices, Employees etc. etc.
Best wishes,
Andy
As already mentioned in the comments, Backbone models are dynamic by nature. So this is perfectly valid for example:
// A example dataset, this could be returned as JSON from the server
var jsonDataA = [
{
name: "Foo",
title: "Bar"
},
{
name: "a",
title: "b"
}
],
// A different example dataset
jsonDataB = [
{
make: "X",
model: "Y"
},
{
make: "Z",
model: "ZZ"
}
],
MyModel = Backbone.Model.extend({
/* Empty Model definition */
}),
MyCollection = Backbone.Collection.extend({
model: MyModel
}),
collection = new MyCollection();
collection.reset(jsonDataA);
console.log(collection.models);
collection.reset(jsonDataB);
console.log(collections.models);
Here I have reused the same Collection and Model definition to store completely different datasets.
One part is the raw data, the other part is its relations. You need to transport the metadata also, which contains the types and their relations. Model attributes will be populated automatically.
From your metadata a simple object can be constructed, where the keys describe one entity, for example:
var entites = {};
entities["Customer"] = Backbone.Model.extend({
/* Model definition based on metadata */
});
var parametersFromServer = {name: "John Doe"};
var customer = new entities["Customer"](parametersFromServer);
For building relations I would recommend using BackboneRelational plugin.
I´d like to create a content type called "contact" that will be share in other content types, like "client" and "prospect". When I create a new client, I´d like to be able to create, in the same form, new contacts for this client, like a field collection. For example:
ADD NEW CLIENT
Name:
Firm:
Email:
Contacts (multiple value)
Name:
Email:
Add new contact
But, in the "Contact" content type I have a lot more fields that I can fill up later:
Name:
Email:
Phone:
Address:
City:
State....
Thanks!
You could use the rules module to setup a rule that whenever a new 'Client' entity is created a new 'Contact' entity is created based on the values in the client contacts fields (name and email).
However I am unsure as to how you could implement an 'add another item' for field groups in a form (which is what I assume is the desired functionality of your 'Add new contact').
You may also want to make sure to include an entity reference field on your contact entity type, so that it can reference the client entity to which it is related. That would give you the option in the future to display a view of contacts related to a client.