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.
Related
I am new to MySQL databases and I'm trying to create a web stock and production database, using PHP.
In this inventory software, I am trying to create a table in which products are created and also their components inserted. But if a product has a different number of components, I wanted to know if there is any way to add more components columns from the management page of the website.
Another thing is that these components are taken from another table, and as long as a new product order is created, the quantity of those used components should be subtracted from the components table. (but this is a major issue, solving the first issue should be enough for now).
Yes, you can add, or remove, columns from a database table at any time.
However, I would not do this. You have to try and design a database that can handle products with a varying number of component. Normally this would be done this way:
Create a table for your products.
Create a table for your components.
Create a linking table product_components, to indicate what components a product consists off.
See: Using linking tables for many to many relationships in MySQL
You need to know about relationships in mysql. There are basically 3 types(some may say 4 though) of relationships available in a relational database like mysql.
Here according to your description you can use
1 to many
many to many
relationship in your database. This may help you-
https://afteracademy.com/blog/what-are-the-different-types-of-relationships-in-dbms
I'm pretty new to Laravel, so I'm struggling with the logic for what is essentially a CMS with multiple content types.
Say I have 3 content types; Food, Books and Cars. Every item in all content types has a name, URL and a couple of other fields.
I can create, update and delete any of these resources with most likely the same code replicated 3 times. The only difference would be with a create or update as the field names would differ between them.
Should I just duplicate these fields/functions for each controller, or create some common ground in one place?
The crossover of fields/functions initially will not be huge, however, it seems inefficient let's say if I had 10 content types and I want to add one field to all of them I have to update code in a large number of places.
If I had a central "Node" that contained the id's and common fields for ALL items in every content type, then have this linked to individual tables for the custom fields, I'm in a much better position when I want to add, update or delete common fields.
I've currently got 3 controllers and have only worked on one so far so I have an index(), show() and edit() function in the controller.
As a test, I created a Node model with php artisan make:model Node -mcr and simply extended the existing Controllers so they were extending NodeController. Which just threw up an error like this;
Declaration of App\Http\Controllers\FoodController::show(App\Food$food) should be compatible with App\Http\Controllers\NodeController::show(App\Node $node)
This is likely not the way to go about it anyway, but I simply do not know the recommended practice for this.
Most appropriate and standard best practice for your problem is,
have a single database table, let's say table name as node, which will contain all the common fields, and have another table as categories and relate it with node table (1-m) to categorize type of node such as car,book,food etc., and make one more table, let's say node_meta which will store all additional attributes depending on the type of node,
(you may have a look on the wordpress CMS database ER Diagram which has similar db design.)
Polymorphic relation is not a good idea for this as stated by another user above, it has some limitation when it comes to querying underlying data, for example you cannot apply whereHas query and still there is no official solution to this problem.
i am struggling with setting up my database and eloquent relationships in a certain scenario.
This certain part of my application will be handling online orders.
basically i want an order to consist of multiple configured items.
i want configured items to consist of a base item (ex. a cheesburger) and also of toppings.
i have gone through several scenarios, but I am trying to make this as simple as possible. here is the quick and dirty story of what I have now.
I want a configured item to consist of three things. 1. the order id of the order it is associated with. 2. the menu item that it relates to (ex. cheeseburger, hotdog ) 3. and the toppings.
I am considering two tables that are full of relatively static information about the menu items and the toppings to be referenced from the configured item table.
I had originally considered creating a new menu item on every configured item, but I like the idea of just being able to look up items/toppings and applying them to a configured item. Im sorry if this is unclear. I am three days into this and my brain is absolutely in pain by now.
here are the relationships i am considering.
configured_item: belongsTo Order; hasOne menu_Item;
Menu_item: belongsToMany configured_item; hasMany toppings;
Toppings: belongsToMany configured_item;
I guess in a way my configured item table is a pivot table of sorts, but then it will need to be referenced by an order as well.
i know questions have been asked about three way relationships, but I cant find any info on tables that are relatively static like i am trying to use.
I finally caved and used two pivot tables. it all works, but i cannot help but feel there is a better way to handle this. It seems a lot of people have similar issues and there is no clear cut solution.
I need to make a lead convert (and relate to) 1 or more opportunity on SugarCRM. With just one lead it's easy, I just have to set the 'opp_id' field - however, with multiple leads I'm not able to do that.
I need at least to create a relationship 1:n between Leads and Opportunities, and to fill that relationship with PHP code (receiving both lead and opportunity ids).
Any ideas?
Thanks.
What I would do is create a one to many relationship between Leads and Opportunities in Studio, what this does is creates a relationship table that will be called something like leads_opportunities (or opportunities_leads depending on what table is the LHS, I think). This will also create a subpanel, the Opportunities can be added to lead via the Select or Create option on the Subpanel. If you want to create multiple relationship straight from the Lead screen, you could add a number of Opportunity relate fields. With the after_save logic hook, you could check for the existence of that specific Lead/Opportunity combo (since there isn't a multi-field index on the relationship tables) and write it if it doesn't exist. It depends more what the use case requirements are, but that should give you an idea of how to handle it a couple different ways...
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.