Just trying to specify some tables structures and models for Doctrine in a YAML file. I'm going through the documentation on this page: http://www.doctrine-project.org/projects/orm/1.2/docs/manual/yaml-schema-files/en
I haven't quite got a grasp on what each line in the relations section is doing.
Here's some sample YAML from that page:
User:
columns:
username:
type: string(255)
password:
type: string(255)
contact_id:
type: integer
relations:
Contact:
class: Contact
local: contact_id
foreign: id
foreignAlias: User
foreignType: one
type: one
Specifically, relations, in order:
Contact is.. I am guessing the name of the other corresponding table pertaining to this relationship?
class: contact is.. what exactly? The name of the model that will be created from this YAML?
local: contact_id is the local key, I understand this.
foreign: id is the field name of the foreign key, I understand this
foreignAlias: User what is this line doing?
foreignType: one
type: one: I am guessing these two lines together specify the type of relationship, eg, one-to-one?
Thanks for any and all help.
Contact is.. I am guessing the name of the other corresponding table pertaining to this >relationship?
Nope. It is the Name of the object you later use when accessing contact information of a user.
$user->Contact->email
This can be different from the Class-Name
class: contact is.. what exactly? The name of the model that will be created from this >YAML?
That is the class you are referencing. Needs to be the Classname in the YAML (i.e. Contact)
local: contact_id is the local key, I understand this.
foreign: id is the field name of the foreign key, I understand this
foreignAlias: User what is this line doing?
This is the name you can access the user information from a contact object.
$contact->User->username
foreignType: one
type: one: I am guessing these two lines together specify the type of relationship, eg, >one-to-one?
Jepp, you are right here.
Related
Hopefully, you can help me with the following doubts that I have.
So I have an entity called User that has some columns like Nationality, Gender, Professional Status. And I have an entity for each column referred.
I'm looking for the best way to define this.
Should I apply a ManyToMany relationship between those tables? Since Users can have the same Nationality and Gender?
When I try to implement this, Spring is creating a third table automatically and I don't understand the need.
Having the following shouldn't be enough?:
Table User:
id,
name,
id_nationality.
id_gender,
id_professional_status
Table Nationality:
id,
nationality
(etc)
Why Spring is creating another table called user_nationality?
Thanks in advance.
Whenever we are using the ManyToMany relationship in that case a separate table will be created containing both primary keys of the tables involved in the new relationship.
Do all types need a non-null ID? I am creating a schema and can't think of a type that wouldn't need an ID and yet most tutorials I see do not include ID.
I'm using AWS Appsync and Amplify.
A snippet of my GraphQL schema look like this:
type Recipe
#model
#auth(rules: [{allow: owner}])
{
id: ID!
title: String!
key: String!
courses: [Course!]!
}
type Course
#model
#auth(rules: [{allow: owner}])
{
id: ID!
name: String!
}
On amplify push it creates the DynamoDB tables Recipe and Course
After reading many tutorials I still don't get it how to add a recipe in GraphiQL.
How can i insert a new Recipe that has a reference to a course and avoid duplicates in the Courses table?
To create multiples Recipe referencing the same Course without duplicates in the Course table, you need to design a many-to-many relationship.
So far the relationship you have designed is not enough for AppSync to understand, you are missing #connection attributes. You can read this answer on github to have an explanation of how to design this many-to-many relation in AppSync
After designing the relation, you will use a mutation to insert data, and it's likely that AppSync will generate the mutation code for you (if not, use amplify codegen in the console). You will then be able to create data.
Since you use DynamoDB with multiple tables (default mode for amplify / AppSync), you will have to either :
Call multiple mutations in a row
Use a custom resolver, as described in this SO answer
I try to save Customer and Contact entities in one form.
My Customer Entity fields:
Id
Name
LastName
ContactId
<- This have relation OneToOne with Id field in Contact Entity.
My Contact Entity fields:
Id
Phone
Address
I try do one form and save this entities to DB.
How to do that?
Thanks for help ;)
If your Entities are well done, you just have to set your Contact to your Customer and save the Customer.
Can you include your annotations (or yml/xml) so we can better understand how you've implemented the OneToOne association?
Attempting to link Categories to Websites, using WebsiteCategory as the refClass.
WebsiteCategory has a column rank, which indicates the order in which the categories should be retrieved when you call $website->getCategories()
I'm stumped, didn't think this would be so difficult. Can anyone help?
Was not able to get doctrine to order the relationship natively (like Damien was suggesting) instead, added a getCategories() function to the model which runs the proper query and returns the result set.
This is not supported for Many to Many relationships, the bug report is linked below with a possible patch, although some comments indicate it was not working. As already noted by OP, I think the solution is to override the getCategories() function.
http://www.doctrine-project.org/jira/browse/DC-651
You can add an orderBy param in your schema.yml:
Gallery:
columns:
title: string(255)
relations:
Images:
local: id
foreign: gallery_id
foreignAlias: Gallery
type: many
orderBy: position DESC
You can put multi fields with a comma.