Support Topics Documentation Slack YouTube Blog

Create a Relation With User


(Saad Zahoor) #1

I wonder if there is any way to declare the relation between User (that is offered by backendless) table and the other table ? In the doc there is a relation between table to table

Person personObject = // personObject retrieval is out of scope in this example
Address addressObject = // addressObject retrieval is out of scope in this example

ArrayList addressCollection = new ArrayList();
addressCollection.add( addressObject );

Backendless.Data.of( Person.class ).setRelation( personObject, “address”, addressCollection,
new AsyncCallback()
{
@Override
public void handleResponse( Integer response )
{
Log.i( “MYAPP”, “relation has been set”);
}

@Override
public void handleFault( BackendlessFault fault )
{
Log.e( “MYAPP”, "server reported an error - " + fault.getMessage() );
}
} );


(Sergey Kukurudzyak) #2

You can declare relation to users in the same way. Instead of:

Backendless.Data.of( Person.class ).setRelation( personObject, "address", addressCollection,

use something like the following:

Backendless.Data.of( Person.class ).setRelation( personObject, "user", userCollection,

(Saad Zahoor) #3

Thank you for you time. I have attached the image which describe what i wanted to do


(Saad Zahoor) #4

I want to create a relation in this User table that is created by the backendless so it points the user to the UserInformation table which contains the detail information about the user
i.e age passport no visa etc


(Mark Piller) #5

You want the Users table have a relation column which points to the UserInformation table, is that correct?


(Saad Zahoor) #6

Yes, Exactly but i do not know how can i achieve that


(Mark Piller) #8
User userObject = // userObject retrieval is out of scope in this example
UserInformation userInfoObject = // userInfoObject retrieval is out of scope in this example

ArrayList userInfoCollection = new ArrayList();
userInfoCollection.add( userInfoObject );

Backendless.Data.of( BackendlessUser.class ).setRelation( userObject, “userInfo”, userInfoCollection,
new AsyncCallback()
{
@Override
public void handleResponse( Integer response )
{
Log.i( “MYAPP”, “relation has been set”);
}

@Override
public void handleFault( BackendlessFault fault )
{
Log.e( “MYAPP”, "server reported an error - " + fault.getMessage() );
}
} );

(Saad Zahoor) #9

Thank you for your time @mark-piller

Below is my code if you help me the User class that you define in solution, is it from backendless ? How can i access Users table ?

UserInformation userInformation = new UserInformation() ;

ArrayList<UserInformation> userInfoCollection = new ArrayList<UserInformation>();

userInformation.setName(sharedPreferences.getString(“userName”, “xyz”));
userInformation.setPassportNo(sharedPreferences.getString(“passportNo”, “234”));
userInformation.setDateOfBirth(sharedPreferences.getString(“dateOfBirth”, “sdf”));

userInfoCollection.add( userInformation );

Backendless.Data.of( BackendlessUser.class ).setRelation( userObject, “userInfo”, userInfoCollection,
new AsyncCallback()
{
@Override
public void handleResponse( Integer response )
{

}

@Override
public void handleFault( BackendlessFault fault )
{

}
} );


(Mark Piller) #10

Yes, the BackendlessUser class comes from the Backendless SDK for Android/Java.


(Saad Zahoor) #11

I have declare no column like userInfo in the console through CONSOLE
My Dynamic Schema is On

I run the below code but it give me error

Unable to create relation , unknown column name ‘userInfo’. Make sure the column is created first with the ‘column creation API’ or in Backendless Console

1.What is column creation API ?
2.I want to do the job with the API

BackendlessUser user = new BackendlessUser() ;

UserInformation userInformation = new UserInformation() ;
ArrayList<UserInformation> userInfoCollection = new ArrayList<UserInformation>();

userInformation.setName(sharedPreferences.getString(“userName”, “xyz”));
userInformation.setPassportNo(sharedPreferences.getString(“passportNo”, “234”));
userInformation.setDateOfBirth(sharedPreferences.getString(“dateOfBirth”, “sdf”));

userInfoCollection.add( userInformation );

Backendless.Data.of( BackendlessUser.class ).setRelation(user, “userInfo”, userInfoCollection,
new AsyncCallback<Integer>() {
@Override
public void handleResponse(Integer response) {

Toast.makeText(H_UserAgreement_Agreed.this, “Data Saved”, Toast.LENGTH_SHORT).show();
}

@Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(H_UserAgreement_Agreed.this, fault.getMessage(), Toast.LENGTH_SHORT).show();
}
});

So i add this line of code in the code block

user.setPropert("userInfo" , "");

then throw error

:thinking::thinking: in relation case version 3 was better


(Mark Piller) #12

What does the error say?


(Saad Zahoor) #13

Unable to create relation , unknown column name ‘userInfo’. Make sure the column is created first with the ‘column creation API’ or in Backendless Console


(Mark Piller) #14

Well, for one, your code doesn’t follow the rule described in the documentation:

https://backendless.com/docs/android/data_relations_set_add_overview_android.html

As for the column name in the API call, you should read the documentation as well:
https://backendless.com/docs/android/data_add_relation_with_objects_android.html

Regards,
Mark


(Saad Zahoor) #15

Thank you for your time. I will look into the doc one more time