How to dynamically create databases and tables using Symfony2 and Doctrine? - php

I'm designing a web application that allow user registration. That's pretty simple to persist, since a new user is a User entity class that only represents a new line inside a users table in a database after created.
Those users, after logged in, may create a new Group, also registered as an entity class. This group would also represent just a new line in another table called groups. Everything until here is extremely simple, even for someone like me that only watched a few Lynda video tutorials on Symfony2.
The issue that is preventing me from going further in my development is that the users, weekly, will have to register reports for these groups, that, as the other, will be referencerd as a Report class in the Entity folder. I know that these registers could simply be added to another table reports, with a mapping OneToMany, relating the group to the report with a group_id column or something like that. But, someday, this table may be, I don't know, a million rows long?
So, I was wondering if there is any way to create a new table {groupname}_reports in the database each time a user creates a new group in the web application, in order to make things a bit more organized. Also, how could I send the connection to the correct table using the group name as a parameter?
Thanks in advance!

Related

How do I allow multiple users to use my web app & database?

I am a novice in SQL organization! I essentially have an inventory app written in php with an sql database. If I want to allow multiple users to create inventories independent of one another using my web app, would I just use foreign keys linked to a user table or should I be creating new tables for each user, or another option that I have not considered?
thank you!
I have said before that it's a code smell if you hear the word "per" in the context of of database design.
I'm going to create a table per user.
That could result in a lot of tables. How many users will you have? Hundreds? Thousands? Millions? Can your database handle millions of tables? How can you test this? Does that mean your application must create new tables on the fly as new users register?
In your application code, would you create a separate PHP class for each user? Probably not -- you'd create one class that can be reused for many users. One of the class data members would distinguish the user, so each object instance of that class is for one user. But it's the same class.
Likewise, it's almost always better to create one table, and make sure the table includes a column to identify the user. It's simpler to add new users by INSERTing new rows into the table, instead of requiring new tables to be created.
There are exceptions to every rule, of course. You might have so many users that you must split the database over multiple database servers. Or you might have a very strict privacy requirement and separating the data into multiple tables is a good way to enforce it. But if you're just starting out with a small project, those exception cases probably don't affect you yet.

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.

Multi-database design application using Laravel 5.3 and Codeigniter

Friends, I am building a service application and would like to see a gross suggestion to achieve.
The core of the application is to manage research projects, hence it will have a group of users, who belong to an organization, who login and manage their own projects. Many organizations carry out such projects, which are identical in nature. The project management has identical database tables and schema (across institutions). I have designed a plan as follows:
Database-1: A common database users table (all institutions together) get authenticated by querying this table. This table has a institutional code corresponding to each user as a column.
Database-2 (institutional code as its name): Based on the institutional code, all the project management is done by connecting to this database. Within this database tables will be present.
....
Database-x (institutional code as its name).....
All databases will have identical schema and identical user interface. Institute wise management is easy this way.
Now, using Laravel, i know i can connect to multiple databases. I have done this in codeigniter 3 but trying to migrate to Laravel 5.3 as models architecture is different between Codeigniter 3 and Laravel 5.3.
Any better suggestions. I know my query is not a pure question but this question is about implementation of a many to many relation.
Since all institutions will have same modules and same functionalities why are you creating multiple databases? I have successfully done a similar project as you, a Centralised CMS which integrate all church under one diocese in CodeIgniter.
Just add an institution id in all tables and query your table operation with institution id every time. Adding multiple databases will definitely decrease your project performance and increase complexity. An institution table will track all your institutions.
If you need more details about database structure and implementation please comment below.
I have successfully worked on mutliple database and mutltiple domain application.
In that I have created one table "branches" which stored the multiple branches details
like its database name, domain name.
I have set the record as domain= "branch1", database="db_branch1" in table "branches"
Also set the middleware which check the valid domain name and allow to associated routes for domain "branch1".
Once the main common database authenticate the user creditials it connect to branch database in main connection.
It can not explain in very short method, here I just explain the work flow. If you need more details I will explain it.

Finding all columns with same names across multiple tables with Symfony2/Doctrine

I'm thinking of implementing blameable fields to couple of my tables using https://github.com/stof/StofDoctrineExtensionsBundle bundle to know which user created some of database objects.
What I want to achieve and cannot find right answers to do that is following:
As admin user I would like to be able to transfer all blameable objects from one user to another one. This could happen in cases when I want to delete existing user or just want that those objects belongs to another user.
Theoretically I can connect to MySQL.INFORMATION_SCHEMA to find all tables which have fields "createdBy" and deal with those tables, but I cannot connect to that database using doctrine (or can I???).
Can someone suggest approach of transferring all blameable objects to different user?
P.S. I don't need to set default user or to enable null values for createdBy column like answered here: How to disable Blameable-behaviour programmatically in Symfony2

Extending a Doctrine entity to add extra association mapping in Symfony2

I currently have a User Doctrine entity and model in my Components. I have a User Bundle that does the basics of working with users, CRUD, etc.
I am developing a resource allocation bundle and I want to extend my User entity to add extra associations without changing the original user. Then the ResourceAllocation Bundle will be completely separate from the User Bundle.
I have setup a mapped superclass of BaseUser, which both User and SkilledUser (the one from the Resource Allocation Bundle). This however, tries to create a table for both User and SkilledUser which is undesired.
The user and skilled user could be the same user, so, Single Table Inheritance is not going to work.
Effectively, the end result should be one table with the users in.
We are working in YAML if answers could keep to this method, that would be great.
"Effectively, the end result should be one table with the users in."
The only way to achieve this is with STI (Single Table Inheritance).
"The user and skilled user could be the same user, so, Single Table Inheritance is not going to work."
I don't see how this matters... you can still create an association between User and SkilledUser if that is what you mean here.
See reference: http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html

Categories