Symfony Doctrine mapping inside one Entity - php

I had one class User which is also an Entity type. I need to store information about which user can send a message to another one. My conceptual solution is:
- to make a database table with two columns, except unique id of course (id_sender, id_recipient)
- the next step should be to map my class User to that table (maybe using #joinTable annotation)
What is a good practice for thinks like that? I know I should use the doctrine (as a higher abstraction layer). Thanks for your time!

Related

Save data in related tables Symfony 4

First of all, I apologize for my little knowledge of the sector, but I am new to Symfony and PHP.
I am doing a new project using Symfony 4 and the following problem arises.
I have two tables in my application, one for Contracting and another for Alerts.
They are related to each other through an intermediate table, since a contract can have several alerts at the same time.
I leave an image so you can see the relationship.
I already have all the entities created with their Setters and Getters and all the forms made and working, to add a new alert and a new contract and all the additional functionality.
My purpose is to program a function, so that in the intermediate table are stored the IDs of contracts and alerts and are related to each other so that later I can show a list of contracts with their respective alerts.
I can not create the logic to collect the contract ID and the ID of the alert and save it in the table that relates them and then shows it.
I do not know if I've explained myself correctly or if I say some nonsense, excuse me.
First of all, please read the Doctrine2 Many-to-Many docs carefully.
After proper relations configuration creating relations with Doctrine will be as easy as:
$contact->addAllert($alert);
With proper flushing if you don't have cascade: persist configured.
You don't have to take care about the joining table - Doctrine2 will take care of it for you.

Symfony and MySQL/Doctrine: manage a todo lists with todos that may relate to many different entities: tables or fields or many-to-any?

Say I have to manage some lists with some todos in each.
But each todo can be related to otherr entities: say a user (call him), or a document (say edit it), or any other entity managed by my app.
I see only two solutions to manage these relationships:
Use a separate table for each relation type (and so I will came up with tables like todo_users, todo_documents and so on)
Use dedicated fields in the todos table (and so I will have fields like too_id, todo_list, relates_to_user, relates_to_document, ecc.)
QUESTION #1: the two solutions may have different performances on very large datasets? Or are they equivalent?
QUESTION #2: Is there a better approach I can follow?
Doctrine doesn't support it, but is there a way to build an todo-to-any relationship?
Try to store things in #2 fashion will result in a mess soon or later. Some of the problems you can face (I'm sure that will be others that now I can't see or remember of)
Everytime you need to add a relationship with a new entity, you need to add a column: only one column at a time is "significant" as, I imagine, only one todo_list can belong to other entity.
(direct consequence of point above) You need to control "programmatically" that only one column at a time has true value and that at least one column has true value (that can become pretty difficult to guarantee if you "bypass" your application API and go direct onto db (relates_to_* fields here are treated like booleans to indicate foreign key "membership"; if they are just foreign keys, you need to guarantee the same constraints but instead of true value, substitute not null)
If relates_to_* are booleans you need to store an extra field with foreign key: this is not possibile in doctrine (and in sql in general) as you can't declare a foreign key referencing more than one table
You have an amount of useless "noise data" for each record: if this application runs for ten users this would not be a problem at all, otherwise it will be for sure.
I suggest to take a look at doctrine inheritance mappings: this is the solution to your problem and is handled "under the hood" directly from doctrine itself.

symfony3 strange proxy behaviour

I'm working with Symfony3, and at one point I need to get the class name from an entity object.
I have two tables: role and tier. Both have only two fields: id and name. If I do a query to get these entities, one comes back as a regular entity (as is expected), but the other comes back as a proxy, and I can't understand why.
Tier is a foreign key and belongs to another table, but in this instance I'm only getting the tiers, so I don't see why this should matter.
Can anyone explain how or if there is anything I can do in this scenario?
Its normal that you have proxy. Doctrine use the lazy loading to load entities. I advice you to read this answer:
stackoverflow.com/a/17787070/2377164
Anyway you should be able to use your "proxy" as an entity. Doctrine will load the data you need when you'll try to get some properties

Doctrine - Make Multiple Entities From One Table

I am currently working on a huge refactoring project. We have taken over a classic PHP/MySQL project, where most code is procedural, duplicated, and there is very little hint of an architecture.
I am planning on using Doctrine to handle our Data Access, and have all of my tables mapped to entities. However, our MySQL tables are largely messed up.
The table I am currently working with has over 40 columns, and is not normalized by any means. A quick example of what we have:
Brand
id
name
poNumber
orderConfirmationEmail <---- these should go into a BrandConfirmations entity
shippingConfirmationEmail <-----
bill_address <---- these should go into a BrandAddress entity
bill_address2 <-----
city <------
.
.
.
Ideally, what I would like to have is for Doctrine to pull out the fields that reference different Entities, and actually put them into those Entities. So for instance id, name, and poNumber would get pulled out into a Brand entity. orderConfirmationEmail and shippingConfirmationEmail would get pulled out into a BrandNotification entity. Next, bill_address, and the rest of the address fields would get pulled out into a BrandBillAddress entity. Is there a way to configure Doctrine to split the table into these models for me, or do I have to custom write code myself that would do that?
If I do have to write the code to split this table myself, do you have any resources or advice that tackle a similar issue? I haven't been able to find many yet.
The latest version of Doctrine 2 supports what they call embeddables: http://doctrine-orm.readthedocs.org/en/latest/tutorials/embeddables.html. It may solve some of your problems. However, it requires D2.5+. Currently, S2 uses Doctrine 2.4. You could experiment with using the very latest doctrine.
What you can do is make your domain models (entities) act as though you had value objects. So $brand->getOrderConfirmation() would actually return an order confirmation object. You have to do some messing around to keep everything mapped to one table and you might be limited on some of your queries but it's not that hard. The advantage is that the rest of your new applications deals with proper normalized objects. It's only the internal persistence code that needs to get messy.
There are quite a few links on this approach. Here is one: http://russellscottwalker.blogspot.com/2013/11/entities-vs-value-objects-and-doctrine-2.html
Your best bet of course is to refactor your database schema. I like to do kind of a raw dump of the original database into a yaml file with the desired object nesting. I then load the yaml file into the new schema. If you are really lucky then you might even be able to create new views for your existing application which will allow it to keep working in parallel with your new application.

Versioning entity relations with PHP in a PostgreSQL database

I'm in the process of designing a system that stores entities and their relations over time.
Each entity has properties, each property should be versioned, so when a property of the entity changes, a new history state gets added. The complexity comes with the fact that I also need to version the relations between the separate entites. For example: when entity A moves from parent X to parent Y, the relation of both entities also gets a new history state.
I'm looking for advice on how to design this on a lower level - are there any design patterns available for this sort of thing, or any other best practices/proven methods?
I'm building this in PHP with a PostgreSQL database, optionally using Doctrine as the ORM/DBAL.
I would recommend looking into the table_log extension (https://github.com/psoo/table_log) for this. It does require compiling but it has the advantage of allowing you to restore tables to a previous state, audit changes, etc.
One important detail here is that the history changes are stored in other tables, not your main tables, so you think of it as current data plus external audit trails. If you need to merge them for a query, you could create a view.

Categories