Link Doctrine Entity when I already have a dataBase - database

I'm working with an existing dataBase (from my company) and they want to build an intranet with symfony 4.
I created my doctrine files and the doctrine database.
And after that i used some insert command for work with all the data from the existing dataBase in the doctrine database and in my symfony project.
For the moment everything work very well !
But I want to add a relationship mapping (one-to-many, many-to-one, many-to-many) in my doctrine model now.
And in all the exemple that i found they link the model with each other inside fixture or with some new data to insert in dataBase
So I added a One-To-One unidirectionnal relationship in my Bodyshops.orm.yml link to the BodyshopsEmail.orm.yml and after used the
doctrine:schema:update --force
With success, I want to use my Bodyshops Model in a Controller and I got a
Missing value for primary key senderEmail on App\Entity\BodyshopsEmail
I know that the question was put
Missing value for primary key id Doctrine Symfony2
Doctrine - "Missing value for primary key"
But I guess that the error is here because I got two ID in my bodyshopsEmail id(same as bodyshops.id) and senderMail(Set foreach row in bodyshopsEmail)
I want to know if i have to make a script that will bind my actual data relationship mapping for avoid the error or if the error doesn't come from the linking but from the orm.yml for exemple
Hope that I am clear enough , thank's for reading :p my files are below
(EN)
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html
(FR)
https://www.jdecool.fr/blog/2017/09/20/tutorial-jobeet-symfony-4-partie-3a-le-modele-de-donnees.html
https://openclassrooms.com/courses/developpez-votre-site-web-avec-le-framework-symfony2/les-relations-entre-entites-avec-doctrine2#=
Bodyshops.orm.yml
App\Entity\Bodyshops:
type: entity
id:
id: #B
type: string
length: 2
fields:
accessFrom:
type: string
length: 5
nullable: false
region:
type: string
length: 8
nullable: false
name:
type: string
length: 32
nullable: false
nameAlternative:
type: string
length: 32
nullable: false
nameCode:
type: string
length: 32
nullable: false
phone:
type: string
length: 16
nullable: true
fax:
type: string
length: 16
nullable: true
website:
type: string
length: 64
nullable: true
address1:
type: string
length: 64
nullable: false
address2:
type: string
length: 64
nullable: true
address3:
type: string
length: 64
nullable: true
zip:
type: string
length: 6
nullable: false
city:
type: string
length: 32
nullable: false
country:
type: string
length: 32
nullable: false
countryCode:
type: string
length: 4
nullable: false
colorBackground:
type: string
length: 8
nullable: false
colorText:
type: string
length: 8
nullable: true
workshopSlots:
type: integer
length: 4
nullable: false
departements:
type: string
length: 32
nullable: false
ipList:
type: json_array
nullable: true
identRepa:
type: string
length: 32
nullable: true
statutJurid:
type: string
length: 2
nullable: true
capital:
type: string
length: 16
nullable: true
natInscript:
type: string
length: 64
nullable: true
rcsRdm:
type: string
length: 10
nullable: true
gerant:
type: string
length: 1
nullable: true
codeApe:
type: string
length: 4
nullable: true
idIntracomm:
type: string
length: 13
nullable: true
oneToOne:
email:
targetEntity: BodyshopsEmail
joinColumn:
name: id
referencedColumnName: id
BodyshopsEmail.orm.yml
App\Entity\BodyshopsEmail:
type: entity
id:
id:
type: string
length: 2
nullable: false
senderEmail:
type: string
length: 64
nullable: false
fields:
senderName:
type: string
length: 64
nullable: false
replyTo:
type: string
length: 64
nullable: true
smtpHost:
type: string
length: 32
nullable: false
smtpPort:
type: string
length: 6
nullable: false
smtpLogin:
type: string
length: 64
nullable: false
smtpAuth:
type: string
length: 32
nullable: false
smtpSecurity:
type: string
length: 32
nullable: true
popHost:
type: string
length: 32
nullable: true
popPort:
type: string
length: 6
nullable: true
imapHost:
type: string
length: 32
nullable: false
imapPort:
type: string
length: 6
nullable: false
receiveLogin:
type: string
length: 64
nullable: false
receiveAuth:
type: string
length: 32
nullable: false
receiveSecurity:
type: string
length: 32
nullable: false
globalPassword:
type: string
length: 64
nullable: false
signatureFile:
type: string
length: 256
nullable: true
I don't think that's usefull to show you the model, but be sure that every value as a getter and setter (even the $email define by the one-to-one)

You would have no schema changes if you use column already present in your database and model.
For example if you have a table cart with a column customer_id in your existing database
You could do
/**
* One Cart has One Customer.
* #OneToOne(targetEntity="Customer", inversedBy="cart")
* #JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customer;
You can check using php bin/console doctrine:schema:update --dump-sql to see if there is any sql request needed to do the changes

Related

MongoDB - How can I define a field as unique and nullable in MongoDB?

i am writing in typescript,
i am trying this in member model:
mail: {
type: String,
unique: true,
nullable: true,
default: null,
}

How to convert MongooseSchema to Angular .ts file

I have this mongoose.Schema
const Recept = mongoose.model("Recept", new mongoose.Schema({
receptName: {
type: String,
required: true,
unique: true,
trim: true,
maxlength: 200
},
ingredient: [
{
amount: {type: Number, min: 1, required: true},
unit: {type: String, required: true, minlength: 2},
ingredientname: {type: String, required: true, trim: true, maxlength: 200}
}
],
})
);
for my Angular app i made this food.ts file
export food{
id!:number;
receptName!:string;
ingredient!:string[];
cookTime!:string;
}
===
My question is: how can i get this() part of the mongoose schema
=== ()this part
{
amount: {type: Number, min: 1, required: true},
unit: {type: String, required: true, minlength: 2},
ingredientname: {type: String, required: true, trim: true, maxlength: 200}
}
=====
into / related to the food.ts ingredient!:string[];?
I just can't figure out how to get the array (amount: type:number, unit: type: string, ingredientname:type:string ) into ingredient[]
Is there somewhere a manual on this subject?
I looked into the Mongoose Documentation, but can't really find it.
Thanks
LS,
I am sorry that I was not able to reply earlier - to clarify the problem.
I found the solution to my problem.
My problem was how to (re)write the mongoose. schema to this food.ts file.
The food.ts file is a model for the data I want to get from the back-end.
In the file food.ts, the ingredient is an array that consists of 3 objects: amount/unit/ingredient name.
So i came up with this solution for my food.ts file:
export food{
id!:number;
receptName!:string;
ingredient!:{amount:number, unit: string, ingredientname: string}[];
cookTime!:string;
}
It works for me! My problem is solved.

MongoDB - 3 types of roles

I have a user model here that currently only have 2 possible roles. One is admin and one is regular user.
const userSchema = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
isAdmin: {
type: Boolean,
required: true,
default: false
}
}, { timestamps: true
})
As you can see, if the isAdmin was set to true, it will automatically become an admin otherwise its a regular user.
But things change when I added a new role like: isOwner which is I added a new field again:
const userSchema = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
isAdmin: {
type: Boolean,
required: true,
default: false
},
isOwner: {
type: Boolean,
required: true,
default: false
},, { timestamps: true
})
As you can see I added a new field isOwner at the top.
My question is given these three roles: isAdmin, isOwner and regular user, am I doing the right way to do this and manage 3 different roles? or is there a better way to do this?
Note: The way this works is that admin has the overall access to all of the job post of the owner while the regular user can only comment on the job post posted by the owner (who posted the job)
In future you may have more roles, It is best to use Array field for user roles.
array reference document
const userSchema = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
isAdmin: {
type: Boolean,
required: true,
default: false
},
roles: {
type:[String],
required: true,
default:["regular"]
}
},
{ timestamps: true
})

mongodb User with subdocument vs document with UserId

I am using meanjs and I would like to store some user data in a one to many relationship. My case is similar to the articles example but the articles will only ever be accessed through the user. I want the route to be something like
Users/:userId/articles
or
Users/me/articles
Question 1
Should I just stick with the articles model as it is or should I make articles a subdocument of user. e.g.
var UserSchema = new Schema({
firstName: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your first name']
},
lastName: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your last name']
},
displayName: {
type: String,
trim: true
},
email: {
type: String,
trim: true,
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your email'],
match: [/.+\#.+\..+/, 'Please fill a valid email address']
},
username: {
type: String,
unique: 'testing error message',
required: 'Please fill in a username',
trim: true
},
articles: [articleModel.schema],
password: {
type: String,
default: '',
validate: [validateLocalStrategyPassword, 'Password should be longer']
},
salt: {
type: String
},
provider: {
type: String,
required: 'Provider is required'
},
providerData: {},
additionalProvidersData: {},
roles: {
type: [{
type: String,
enum: ['user', 'store', 'admin']
}],
default: ['user']
},
updated: {
type: Date
},
created: {
type: Date,
default: Date.now
},
/* For reset password */
resetPasswordToken: {
type: String
},
resetPasswordExpires: {
type: Date
}
});
Question 2 if I make it a subdocument can I still use the $resource function or do I have to make custom functions?
The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.

Error with doctrine command line tool

Update
I have now go back to a previous build of doctrine and now the error is:
Invalid schema element named "Roles" at path "RoleResource->columns->relations"
this is whit the same yaml file (see it below)
I have a problem with the doctrine command line tool. When I give the command "build-all-reload", I get te following error:
SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table. Failing Query: "CREATE TABLE resource (id BIGINT AUTO_INCREMENT, name VARCHAR(20), INDEX role_id_idx (role_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB". Failing Query: CREATE TABLE resource (id BIGINT AUTO_INCREMENT, name VARCHAR(20), INDEX role_id_idx (role_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB
My yaml file looks like this:
detect_relations: true
options:
type: INNODB
collate: utf8_general_ci
charset: utf8
Log:
columns:
id:
type: integer
primary: true
autoincrement: true
priority: tinyint
priorityName: string(10)
title: string(250)
message: text
actAs:
Timestampable:
created:
type: timestamp
format: Y-m-d H:i:s
updated:
disabled: true
User:
columns:
id:
type: integer
primary: true
autoincrement: true
username: string(50)
password: string(40)
actAs:
Timestampable:
created:
type: timestamp
format: Y-m-d H:i:s
updated:
type: timestamp
format: Y-m-d H:i:s
Role:
columns:
id:
type: integer
primary: true
autoincrement: true
name: string(20)
attributes:
export: all
validate: true
RoleResource:
columns:
role_id:
type: integer
primary: true
resource_id:
type: integer
primary: true
relations:
Role:
foreignAlias: RoleResource
Resource:
foreignAlias: RoleResource
Resource:
columns:
id:
type: integer
primary: true
autoincrement: true
name: string(20)
relations:
Roles:
foreignAlias: Resources
class: Role
refClass: RoleResource
Menu:
columns:
id:
type: integer
primary: true
autoincrement: true
label: string(20)
Artical:
columns:
id:
type: integer
primary: true
autoincrement: true
title: string
content: longtext
css: longtext
js: longtext
I don't know how to solve this problem.
Can someone please help me?
I downgraded Doctrine.
And there are to spaces to much in the yaml file add the line:
relations:
from the component "RoleResource:"
Thank you for all the help Tom,
Ivo Trompert

Resources