How to add new columns if needed - php

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

Related

eloquent relationship and database or three way relationship

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.

How to deal with dynamic columns in MySQL

I have a site that has different type of products with different specifications for each of them.
I want to be able to use only one products table with a set of columns but because columns are different depending on what type, I have to create multiple products table catering for each type. This I think is time consuming and not really effective way to manage as an ongoing solution.
Is there a good way to manage this type of scenario with the database?
I'm using Cakephp as the framework.
Normalise your data structure: for example, have a product_info table (with FK into the products table) that contains columns key and value to express additional information about each product.
Martin Fowler lists three general approaches.
Single table - Putting all the columns in one table and only using the ones you need (this sounds the closest to what you have)
Class table - All classes have their own table storing data specific to that class (with the same primary key in every relevant table)
Concrete table - The same as above, but only concrete classes have tables, not abstract ones.
Single table is the simplest unless you have a good reason not to - just have all possible fields there, and only use the ones you need in each class. You do have the disadvantage of not being able to enforce NOT NULL; if this matters, either make a custom constraint depending on the type of object, or use option 2 or 3.
I wrote about using the EAV model with cake a little while ago, I think this post might be helpful, but slightly outdated.
http://nuts-and-bolts-of-cakephp.com/2010/07/27/keyvalue-tables-and-how-to-use-them-in-cakephp-1-3/
Also, this could be very helpful for your particular question... Please take a look and study some concepts in Magento (a very popular PHP-based ecommerce framework) makes heavy use of EAV schemas and does a nice job of indexing and flattening the data.
You can certainly gain a lot of interesting perspective on EAV implementation. Whether you love it or not is a different story :)

CakePHP/SQL dynamic table creation & relations, I need a better approach

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.

Table Data Gateway, Cascading one-to-many, and Zend

I am new to Zend Framework and OO design patterns.
I have three tables: Owners, Shops and Products. One Owner has many Shops; and each Shop has many Products.
I have extended Zend's DB Table Abstract for each of the three tables, and setup dependent tables and reference maps.
If I want to find which Products are available from a particular Owner, I start with an Owner object and use findDependentRowset to get a rowset of Shops. I then iterate the rowset to find all Products (again using findeDependentRowset).
Is there a shortcut for doing this? How do you go about navigating three such tables using the Table Data and Row Data Gateway patterns? (Or should I be moving to a Data Mapper perhaps?)
Thanks for your thoughts!
I think what you can use is findManyToManyRowset method.
For example to find produces for a given owner you could be able do:
$productsRowset = $ownerRow->findManyToManyRowset('Products_Table_Model','Shops_Table_Model');

display several tables in pagination

I am using cake php and mysql.
I have several tables that all link up to a table called relates. The relates table holds the primary ids for all the other tables. example I have a table called clients its primary key is called id in the relates table its called client_id.
heres my deal i created a crud application in cakephp it works great, but it only displays information via pagination for one table. I would like to use pagination to display several tables at once so i can display my clients table and another tables like clientsphone table ect. I tried a few things out but i am completely lost. In my relates table i created pagination for it and it shows the primary ids when you click the primary id is goes to the proper record and displays the information from that table, but display just numbers wont help anyone if you dont know what it is.
how can i display several tables at once for the relates table ? any advice would be great
This really depends on what you're looking for.
If you'd like to have all the data at hand but not necessarily on screen at once, a great solution would be to use jQuery's UI Tabs. You could throw each table in a separate div and easily toggle them with the tabs.
You could display multiple at once a la a stock trading application. But from a UI standpoint that can get ugly in a hurry.
How about dynamically building one gigantic table via HTML or JSON string then using jQuery's datatables to paginate, sort, filter, and show/hide columns? That probably would give you the best of all worlds while still making it readable to your users. You could also use Datatables and tabs together to get more than one at a time. Datatables is the easiest way I can think of to make a standard table more user friendly quickly.

Categories