Doctrine2 cannot make '2' one-to-many relations - doctrine-orm

I'm trying to make 3 entities (Item, Agree, Disagree) with following relations.
Item one-to-many Agree
Item one-to-many Disagree
But only one relation (declared later) out of two has made.
Here're my .yml files.
Entities\Item:
type: entity
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
oneToMany:
agrees:
targetEntity: Agree
mappedBy: items
oneToMany:
disagrees:
targetEntity: Disagree
mappedBy: items
Entities\Agree:
type: entity
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
manyToOne:
items:
targetEntity: Item
inversedBy: agrees
Entities\Disagree:
type: entity
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
manyToOne:
items:
targetEntity: Item
inversedBy: disagrees
And the code below is the Item.php auto-generated by Doctrine2. As you can see, it doesn't contains 'Agree' at all.
namespace Entities;
class Item {
private $id;
private $disagrees;
public function __construct() {
$this->disagrees = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId() {
return $this->id;
}
public function addDisagrees(\Entities\Disagree $disagrees) {
$this->disagrees[] = $disagrees;
}
public function getDisagrees() {
return $this->disagrees;
}
}
If I switches the declaration order ('Disagree' first, followed by'Agree', like the below), Item.php has only 'Agree'-related code in this time.
Entities\Item:
type: entity
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
oneToMany:
disagrees:
targetEntity: Disagree
mappedBy: items
oneToMany:
agrees:
targetEntity: Agree
mappedBy: items
What's wrong with my code? Any comments will be helpful.
Item, Agree and Disagree are just samples to show this problem. In real project, Agree and Disagrees are totally different entity. So, don't suggest me to merge them into unified entity. :)

You were close, you just need to put all same association mapping types under the same type declaration :)
oneToMany:
agrees:
targetEntity: Agree
mappedBy: items
disagrees:
targetEntity: Disagree
mappedBy: items

Related

Not being able to edit my Many-To-Many entity in AWS AppSync

When creating a Many-to-Many relationship using GraphQL for the schema, I have been unable to alter the entity created. The #manyToMany annotation enables the syntax to produce a table named "OrderUsers". But the problem lies in my inability to alter the "OrderUsers" entity. After different attempts at changing the table, I have been unable to figure out how to change it. The only attributes that are taken into the "OrderUsers" table are the id from both the User as well as Order table, which also creates only two GSIs. Also, this is using the v2 docs.
type User #model {
id: ID!
fName: String!
lName: String!
phoneNumber: String!
email: String!
DOB: String!
orders: [Order] #manyToMany(relationName: "OrderUsers")
ordersByDate: [Order] #hasMany(indexName: "byOrderByDate", fields: ["id"])
ordersByStatusDate: [Order] #hasMany(indexName: "byOrderStatusByDate", fields: ["id"])
}
type Order #model {
id: ID!
userId: ID! #index(name: "byOrderStatusByDate", sortKeyFields: ["status", "date"]) #index(name: "byOrderByDate", sortKeyFields: ["date"])
status: String!
amount: Int
date: String!
users: [User] #manyToMany(relationName: "OrderUsers") # Test to see if I can tinker w/ the 'OrderUsers' tbl
productId: ID! #index(name: "byProductOrder", sortKeyFields: ["id"])
clubId: ID! #index(name: "byClub", sortKeyFields: ["id"])
}
After reviewing the AWS Amplify as well as AppSync docs, attempting to add GSIs to the 'OrderUsers' table, switch the partition key used from the 'User' entity to a composite partition key for the 'OrderUsers' table, and adding other attributes to the 'OrderUsers' table, everything gets wiped as I try to push/pull it. Since the syntax isn't stored in the schema.graphql file, I have been stuck. My expectation was that it would save into the schema and update the resolvers used for the mutation(s) & queries, but that has not been the case.
Any and all help would be appreciated,
Thank you & Happy holidays

How to add a unique constraints through Loopback 4 Model?

I have a Department model in my project. The properties are id, name. name of course should be unique. However I can't seem to look where the setting is so I can put something like
#model()
export class Department extends Entity {
#property({
type: 'number',
id: true,
generated: true,
})
id?: number;
#property({
type: 'string',
required: true,
unique: true // ---> This is what I want. But how?
})
name: string;
constructor(data?: Partial<Department>) {
super(data);
}
}
I tried digging to Model documentation, it seems that there is something I can do with the #model decorator. However I found no documentation about it.
Also, I want to do this in PostgreSQL as a datasource. Any clue? Any advice would be appreciated.
#property({
type: 'string',
required: true,
index: {
unique: true,
}
})
name: string;
Using index object.
Migrate Database - Modify or alter table
run command : npm run build
migrate database : npm run migrate

Can I have camel case as default when using postgres in loopback?

If you don't specify anything all the fields are created as lower case in the database when using postgres. Is it possible to change the default behavior to use the exact name of the fields in the model? That makes it easier to write custom queries.
As it is now I have to configure the property on each field to say that they should be camel case, and that is quite error prone since that is something that is easy to forget.
If that isn't possible, is it possible to use the functionality in the repository that does the mapping from all lowercase to the fields in the models in an easy manner somehow?
Not sure if this helps, but you can use name property
example:
export class User extends .... { #property({
type: 'number',
id: true, }) id?: number;
#property({
type: 'string',
required: true,
name: 'first_name',
})
firstName: string;
#property({
type: 'string',
name: 'last_name',
})
lastName: string;

DynamoDB AppSync field resolvers timing out

So I have the schema below. If I try to query data off this schema AppSync will time out saying 'NetworkError when attempting to fetch resource.'
type Model {
PartitionKey: ID!
SortKey: ID!
Name: String
Version: Int
FBX: String
# ms since epoch
CreatedAt: AWSTimestamp
Description: String
Tags: [Tag]
}
type ImageSet {
PartitionKey: ID!
SortKey: ID!
Name: String
CreatedAt: AWSTimestamp
Description: String
Tags: [String]
}
Now, if I change 'Name' in the model to 'ModelName' then queries on that will work. If I change 'Name' in ImageSet to 'SetName' then queries on that will work.
What is going on with this? What is wrong with the 'Name' field name? 'Description' and 'CreatedAt' do not have this issue.
Edit
Actually I am encountering this happening with other fields in the
schema as well. Please help.
I do have resolvers attached to specific fields. Removing them does
solve the problem. Am I not supposed to attach revolvers to specific
fields or is something else wrong?
-
Edit 2
This really does seem to only occur if the name of a field is shared
between different schema objects, is that not allowed!?

polymorphic hasMany and belongsTo relationships in ember-data rev 12

I am having trouble implementing what I understand to be a polymorphic relationship using ember-data rev12.
I have the following models:
App.Project = DS.Model.extend
lists: DS.hasMany('App.List', { polymorphic: true })
App.Proposal = DS.Model.extend
lists: DS.hasMany('App.List', { polymorphic: true })
App.Employee = DS.Model.extend
lists: DS.hasMany('App.List', { polymorphic: true })
App.List = DS.Model.extend
name: DS.attr('string')
#project: DS.belongsTo('App.Project', { polymorphic: true })
And I am trying to create a new list from the project router like so.
App.ProjectRoute = Ember.Route.extend
events:
newList: (project) ->
lists = project.get('lists')
list = App.List.createRecord(name: 'list1')
lists.pushObject(list)
#store.commit()
But the request to the server is setting the polymorphic keys incorrectly.
I was expecting the payload to look like:
{ list: { name: list1, listable_type: project, listable_id: 100 } }
But got:
{ list: { name: list1, project_type: project, project_id: 100 } }
What am I missing? Is there a way to define the polymorphic type or key?.
Here is my temporary hack
https://gist.github.com/arenoir/5409904
First thing, based on the current models you have, you don't need to use polymorphic associations. And you may not want to set the polymorphic option on both end of the relationship.
If you want to have your payload to contain listable, you just need to rename your attribute:
App.List = DS.Model.extend
name: DS.attr('string')
listable: DS.belongsTo('App.Project', { polymorphic: true })
UPDATE
Based on my understanding of your classes, it is a List that can belong to different types. So you should define your models like this:
App.Listable = DS.Model.extend
lists: DS.hasMany('App.List')
App.Project = App.Listable.extend
App.Proposal = App.Listable.extend
App.Employee = App.Listable.extend
App.List = DS.Model.extend
name: DS.attr('string')
listable: DS.belongsTo('App.Listable', { polymorphic: true })
and the payload will look like this:
{list: {name: "list1", listable_type: 'project', listable_id: 100}}
I don't know if you also want a list to be added to several listables at the same time. Based on the names of your models, I'm tempted to believe that it's not what you want: a list should contain different objects (project, proposal, employee). and not a project can have multiple lists.