Doctrine mapping file error Foreign Key Silex - file

I'm currently learning Silex and I have problems understanding
how to manage foreign keys with Doctrine.
I have an error with the mapping file: Invalid mapping file.
Also I found that link, that helped me: link
Here is my database:
Database
And here is the mapping file for the Post
BLOG\Models\Post:
type: entity
table: posts
id:
id:
type: integer
generator:
strategy: auto
fields:
title:
type: string
length: 255
column: title
nullable: false
date:
type: datetime
column: date
nullable: false
content:
type: text
length: 65535
column: content
nullable: false
manyToOne:
user:
targetEntity: User
mappedBy: Use_id

I found the answer thanks to this link, I had to change my code for the manyToOne part
manyToOne:
user:
targetEntity: User
joinColumn:
name: Use_id
referencedColumnName: id

Related

Storing messages in new conversation collections; MongoDB

I'm a student working on a chat application for my internship, where I use socket.io.
Right now I am busy thinking of a good way to store the messages send in conversations.
As of now I do the following:
For each conversation between one user and another user, a new collection is made.
On every message sent, the message is stored in the according conversation collection in a single document.
The collections:
Where the document looks as follows:
Now I wonder if there is a good argument to be made to have just one collection "conversations", and store all the messages in multiple documents, where each conversation is a new document.
Creating a new collection for every message is very bad idea instead of that you use a simple schema as given below to store your messages
const conversation_schema = new Schema({
from: {
type: ObjectID,
ref: 'User'
},
to: {
type: ObjectID,
ref: 'User'
},
messageBody: { // body of the message(text body/ image blob/ video blob)
type: String,
},
messageType: { // type of the message(text, mp3, mp4, etc...)
type: String,
},
read: { // to boolean flag to mark whether the to user has read the message
type: Boolean,
default: false,
},
createdAt: { // when was this message goit created
type: Date,
default: new Date(),
},
});
you can fetch the conversation between the two users using the following query
conversations.find({
$or: [
{from: 'user1', TO: 'user2},
{from: 'user2', TO: 'user1},
],
}).populate({ path: 'to', model: User })
.populate({ path: 'from', model: User })
.sort({ createdAt: -1 })

Posting to mongoDb with ObjectId Many to one relationship

Mongoose/MongoDB Question
I have an Owners model containing basic profile data.
I have a secondary model: OwnersImages
e.g
{
owner: {
type: Schema.Types.ObjectId,
ref: 'Owners'
},
name: String,
imageUrl: String,
},
);
From the client I want to post the imageUrl and the name to the OwnersImages table.
e.g
let values = {
owner: this.state.user._id,
name: this.state.field,
imageUrl: this.state.url
}
axios.post(`${serverPath}/api/addFieldImage`, values)
However Im unsure how best to go about this, link it etc.
I can do a GET request on the Owners table to get the Owner data, but then posting this as part of the values to OwnerImages doesn't successfully link the two tables.
Do i need to just store a string reference to the Owner id in OwnerImages or is there a smarter way of doing this?
Or should I just post the string of the user Id to mongoose and then do a map to the Owner table from within there?
Tried to explain this best way I could but the eyes are tired so please ask if any confusion!
Many thanks
Without seeing your exact setup, I think you could modify this to fit your needs:
// In the Schema/Model files
const ownersSchema = Schema({
// other fields above...
images: [{ type: Schema.Types.ObjectId, ref: 'OwnersImages' }]
});
const ownersImagesSchema = Schema({
// other fields above...
owner: { type: Schema.Types.ObjectId, ref: 'Owners' },
});
// in the route-handler
Owners.findById(req.body.owner, async (err, owner) => {
const ownersImage = new OwnersImages(req.body);
owner.images.push(ownersImage._id);
await ownersImage.save();
await owner.save();
});
As a side-note, I think the Models generally have singular names, so Owner and OwnerImage. The collection will then automatically take on the plural form. Just food for thought.
When you want to load these, you can link them with populate(). Consider loading all of the OwnersImages associated with an Owners in some route-handler where the /:id param is the Owners id:
Owners
.findOne({ _id: req.params.id })
.populate('images')
.exec(function (err, images) {
if (err) return handleError(err);
// do something with the images...
});

sequelize model versioning and optimistic

I am wondering if there was an easy way to add versioning to model for easy optimistic concurrency. I was curious if anyone here has integrated that into their project with sequelize and got it to work seamless, without having to manually add the version to the where of every update ect.
I started with something like this
export const User = sequelize.define('user', {
id: {type: Sequelize.STRING, primaryKey: true},
name: {type: Sequelize.STRING, allowNull: false}
}, {
underscored: true,
tableName: 'r_users',
version: true // <- here
});
but the version doesn't change when updating the record or migration
The migration version can be found in SequelizeMeta but to select from it you need to add quotes 'SequelizeMeta' or change the name of the table to sequelize_meta by add
"migrationStorageTableName": "sequelize_meta",
to the config

Dynamic end-points

I have a UI component that generates a mongo schema like this
{
content: String,
date: { type: Date, default: Date.now },
author: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}
the idea is take this schema and generate the end-point to get the info, the question is where do you recomend storage the schema in mongoDB or in the files somehow run programmatically
yo angular-fullstack:endpoint mySchema
Thank You

Doctrine/Symfony losing M:N relationships on save

I have what I assume is a configuration problem with my Doctrine schema.yml, but I can't see to strike the right answer here.
I have two tables, BetaMeeting and ProjectTester, that form a many-to-many relationship through BetaMeetingAttendee. Everything works fine, and I can edit a beta meeting for example to include several project testers, and the relationships are all saved correctly. However, when I edit a project tester that already has existing relationships with a beta meeting(s), upon save the M:N relationships are lost. Using Symfony 1.4.13 and the admin generator, and Doctrine 1.2, and the edit page for a project tester makes no mention of the many-to-many relationships, no hidden fields, etc. Could this be the reason, the data's not there so Doctrine removes it? I didn't think it would be necessary to include it.
My schema.yml is as follows, with irrelevant details removed.
BetaMeeting:
connection: doctrine
tableName: BetaMeeting
columns:
id: { type: integer(4), primary: true, autoincrement: true }
project_id: { type: integer(4) }
date: { type: date }
relations:
Project:
local: project_id
foreign: id
foreignAlias: BetaMeetings
ProjectTester:
class: ProjectTester
refClass: BetaMeetingAttendee
foreignAlias: BetaMeetings
BetaMeetingAttendee:
connection: doctrine
tableName: BetaMeetingAttendee
columns:
beta_meeting_id: { type: integer(4), primary: true, autoincrement: false }
project_tester_id: { type: integer(4), primary: true, autoincrement: false }
relations:
BetaMeeting:
foreignAlias: BetaMeetingAttendees
ProjectTester:
foreignAlias: BetaMeetingAttendees
ProjectTester:
connection: doctrine
tableName: ProjectTester
columns:
id: { type: integer(4), primary: true, autoincrement: true }
tester_id: { type: integer(4) }
project_id: { type: integer(4) }
relations:
Tester:
local: tester_id
foreign: id
foreignAlias: Projects
Project:
local: project_id
foreign: id
foreignAlias: ProjectTesters
Any clue as to why the relationships get cleared out after an edit which is concerned only with the immediate attributes of the ProjectTester object?
If you have a field defined in the Form but you excluded it from the generator.yml it's like submitting an empty field and therefore it clears the relations.
You have to unset that field in the Form.class so the field retains the current values.
public function configure()
{
unset($this['beta_meeting_list']); // or the correct value
}

Resources