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
Related
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.
There is a legacy project, which I would like to refactor. I would like to handle all database related stuff with Doctrine.
Unfortunately the user data is in two tables. There is a x_users and a x_userdata table. Both have a user_id field, but store different type of information. I would like to use all the columns from both tables in my User entity. How is it possible?
I was searching for a solution, but I haven't found any answer yet.
One solution would be to create a User and a UserData entity, and have a one to one relationship. But maybe there is a better solution
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!
I've got a question about CakePHP and databases regarding an application idea I have. I'm still only at the first lines of code, but I'm trying to think out what it will require from CakePHP. One of the things I've realized is the structure of this application will require runtime table creation, which won't work because I need to bake.
Allow me to explain. There's a parent object called the list. The list has many submissions, it also has many comments. Basically users submit an item to the parent list, and they can also comment on the parent list. The things I'm having trouble figuring out is how I'd structure all of this.
My first thought was to have a table containing a row for each list, with it's basic properties. Then two tables per list containing lists submissions and comments. I'm not sure what kind of relationship the comments and submissions table should have to the row in the lists table containing the parent info, foreign key?
The problem with this approach is when a user creates a new list, this would require the creation of two new tables associated with that list. I don't see that this is possible in CakePHP due to needing a bake to generate the cake structure. My other thought was if Cake allowed me to access arbitrary tables without established models, I could access everything, but then I would have to define all the relationships at every point I rely on them.
I'm not really sure how to approach this, any help is appreciated.
You don't need to add new tables every time as you suggested above. You should have 3 tables, lists,list_submissions, and list_comments. When someone adds a list, you add a row to the lists table. list_submissions and list_comments should have a list_id field as a foreign key, so when a new comment or submission is added, just save the id of the list it belongs to in the appropriate table. Your ListSubmission and ListComment should have a 'belongs to' relationship with the List model.
Let's say I have three tables: users, books, and users_books.
In one of my views, I want to display a list of all the books the current user has access to. A user has access to a book if a row matching a user and a book exists in users_books.
There are (at least) two ways I can accomplish this:
In my fetchAll() method in the books model, execute a join of some sort on the users_books table.
In an Acl plugin, first create a resource out of every book. Then, create a role out of every user. Next, allow or deny users access to each resource based on the users_books table. Finally, in the fetchAll() method of the books model, call isAllowed() on each book we find, using the current user as the role.
I see the last option as the best, because then I could use the Acl in other places in my application. That would remove the need to perform duplicate access checks.
What would you suggest?
I'd push it all down into the database:
Doing it in the database through JOINs will be a lot faster than filtering things in your PHP.
Doing it in the database will let you paginate things properly without having to jump through hoops like fetching more data than you need (and then fetching even more if you end up throwing too much out).
I can think of two broad strategies you could employ for managing the ACLs.
You could set up explicit ACLs in the database with a single table sort of like this:
id: The id of the thing (book, picture, ...) in question.
id_type: The type or table that id comes from.
user: The user that can look at the thing.
The (id, id_type) pair give you a pseudo-FK that you can use for sanity checking your database and the id_type can be used to select a class to provide the necessary glue to interact the the type-specific parts of the ACLs and add SQL snippets to queries to properly join the ACL table.
Alternatively, you could use a naming convention to attach an ACL sidecar table to each table than needs an ACL. For table t, you could have a table t_acl with columns like:
id: The id of the thing in t (with a real foreign key for integrity).
user: The user the can look at the thing.
Then, you could have a single ACL class that could adjust your SQL given the base table name.
The main advantage of the first approach is that you have a single ACL store for everything so it is easy to answer questions like "what can user X look at?". The main advantage of the second approach is that you can have real referential integrity and less code (through naming conventions) for gluing it all together.
Hopefully the above will help your thinking.
I would separate out your database access code from your models by creating a finder method in a repository class with an add method like getBooksByUser(User $user) to return a collection of book objects.
Not entirely sure you need ACLs from what you describe. I maybe wrong.