If the user requirement mock-up to something like that :
1-Company (id -name -etc)
and each Company has (Iron,Cement) entered in specific (month-year) .
The Iron has local sales and the Cement has Exporting.
Each Company has (Start balance) for (Iron,Cement) entered only once .
How to model something like that ?
Assuming I understood your domain one could do something like this:
Company(id, name, ...) // PK = id
Material(id, name) // PK = id
Entry(idCompany, idMaterial, month-year, production, localSales, exporting) // PK = idCompany, idMaterial, month-year
Initial(idCompany, idMaterial, date, balanceStart) PK = idCompany, idMaterial, date
If you create your tables this way you can use one table to store data for both cement and iron. You can then write a custom VIEW to display only relevant columns for each material.
Related
we have a problem to query our database in a meant-to-be fashion:
Tables:
employees <1-n> employee_card_validity <n-1> card <1-n> stamptimes
id id id id
employee_id no card_id
card_id timestamp
valid_from
valid_to
Employee is mapped onto Card via the EmployeeCardValidity Pivot which has additional attributes.
We reuse cards which means that a card has multiple entries in the pivot table. Which card is right is determined by valid_from/valid_to. These attributes are constrained not to overlap. Like that there's always a unique relationship from employee to stamptimes where an Employee can have multiple cards and a card can belong to multiple Employees over time.
Where we fail is to define a custom relationship from Employee to Stamptimes which regards which Stamptimes belong to an Employee. That means when I fetch a Stamptime its timestamp is distinctly assigned to a Card because it's inside its valid_from and valid_to.
But I cannot define an appropriate relation that gives me all Stamptimes for a given Employee. The only thing I have so far is to define a static field in Employee and use that to limit the relationship to only fetch Stamptimes of the given time.
public static $date = '';
public function cardsX() {
return $this->belongsToMany('App\Models\Tempos\Card', 'employee_card_validity',
'employee_id', 'card_id')
->wherePivot('valid_from', '>', self::$date);
}
Then I would say in the Controller:
\App\Models\Tempos\Employee::$date = '2020-01-20 00:00:00';
$ags = DepartmentGroup::with(['departments.employees.cardsX.stamptimes'])
But I cannot do that dynamically depending on the actual query result as you could with sql:
SELECT ecv.card_id, employee_id, valid_from, valid_to, s.timestamp
FROM staff.employee_card_validity ecv
join staff.stamptimes s on s.card_id = ecv.card_id
and s.stamptimes between valid_from and coalesce(valid_to , 'infinity'::timestamp)
where employee_id = ?
So my question is: is that database desing unusual or is an ORM mapper just not capable of describing such relationships. Do I have to fall back to QueryBuilder/SQL in such cases?
Do you suit your database model towards ORM or the other way?
You can try:
DB::query()->selectRaw('*')->from('employee_card_validity')
->join('stamptimes', function($join) {
return $join->on('employee_card_validity.card_id', '=', 'stamptimes.card_id')
->whereRaw('stamptimes.timestamp between employee_card_validity.valid_from and employee_card_validity.valid_to');
})->where('employee_id', ?)->get();
If your Laravel is x > 5.5, you can initiate Model extends the Pivot class I believe, so:
EmployeeCardValidity::join('stamptimes', function($join) {
return $join->on('employee_card_validity.card_id', '=', 'stamptimes.card_id')
->whereRaw('stamptimes.timestamp between employee_card_validity.valid_from and employee_card_validity.valid_to');
})->where('employee_id', ?)->get();
But code above is only translating your sql query, I believe I can write better if I know exactly your use cases.
How can i create and migrate lots of tables in laravel with a variable in their name?
I need to create a table "time-{{user_id}}" every time a new user is added to a table called "users".
How can i realize this and how does the Migrationfile/Controller/whatever has to look like?
Thanks!
"Now i need a table for each user where i can store something like a CV.. Date begin, date end, City etc"
Sounds like you need another table with a 1:1 relationship to the users table
Your new table will look something like
tblFurtherUserInfo
FurtherUserInfoId
CV
DateBegin
DateEnd
City
You should add a new row to this table and set FurtherUserInfoID to be the same as the userID in your user table. You should do this whenever you add a record to your user table
EDIT
Now I know a user can have more than one CV, you need a new table as follows
tblCV
CVId
UserId
CV
DateBegin
DateEnd
City
This is a 1 user : many CVs relationship
I'm designing the database for a solution. I'm facing the following scenario:
The user can add a product. This product will belong to a specific operation: "SELL", "BUY", etc.
Another user can mark the product as interested. So, I'll have a table to generate the users which are interested in something.
I'm struggling to decide which approach to go:
I can create one table for each operation, something like "ProductSell", "ProductBuy", etc. The same for interested users ("InterestedProductSell", "InterestedProductBuy", etc).
```
User ProductSell ProductBuy InterestBuy InterestSell
____________ ___________ __________ ___________ ____________
Id Id Id ProductId (ProductBuy PK) ProductId (ProductSell PK)
Name Title Title UserId UserId
Username UserId UserId Date Date
```
I can create one table for all operations, with a column named "Operation". Same for interested users.
```
User Operation Product Interest
____________ _________ ___________ __________
Id Id Id ProductId (ProductBuy or ProductSell PK)
Name Name (Buy, sell, etc) Title UserId
Username UserId Date
Operation
```
Can you give me your opinions about these two approach, or even a third approach that I didn't realize? Things like performance, optimization, maintenance, coding... I need another options other than my sight about this.
If it's matter, I'm working with SQL Server.
your 2nd approach of having a separate column for Operation looks good
user Table
uid
name
product Table
pid
name
userproduct Table
uid
pid
operation
time
Ok, I am total newbie so bear with me.
Trying to implement an ordering system and wish
to save the orders to the database with LINQ to Entities. I can do it now
but for each new object that is saved to the orders table
a new row is inserted, with new OrderNo for each ProductID where as I obviously
should be able to have multiple ProductID's for each OrderNo.
Everything is very simplified as I am just testing.
I have an orders table with columns as such:
OrderNo PK, Identity specification
Line int PL
ProductID int
and a products table
ProductID int PK
An order entity object is instantiated and its properties
are populated with data from a form which is posted to an action method.
It is then saved to the orders table with the following code:
(DropDownList1Value) has value of an existing ProductID and "DropDownList1Value" is the id of the DropDownList element in view.
[HttpPost]
public ActionResult OrderProcessor()
{
int productId = int.Parse(Request.Form["DropDownList1Value"]);
var order = new Order();
order.ProductID = productId;
context.Orders.AddObject(order);
context.SaveChanges();
return View(order);
}
So the records that are inserted look as such:
Sorry, couldn't line up the values under their respective column name in this editor.
OrderNo Line ProductID
101 0 3
102 0 5
103 0 2
Where as I want something like this:
OrderNo Line ProductID
101 1 3
101 2 5
101 3 2
102 1 2
So I wish to know how can I modify the orders table so it
can have multiple records with same "OrderNo" and just increment for "Line" for diff ProductID's and how do I go about inserting such records with LinQ to Entities where
I will obviously have many ProductId from multiple DropDowLists
and they will all be for the one order.
Currently I have foreign key dependency on ProductID in Products table,
so no record in the Orders table can have ProductID which does not exists in the Products table.
I need to make the table depend on the whole key that is OrderNo + Line
and have the "Line" auto increment.
Or is there a much better way of implementing of what I am after here?
Thanks.
Let me first explain briefly what I understood.
There is an invoice, which contains several products for one order number.
and this is how your invoice looks like:
Order Number: 101
------------------
Sl. Products
1 3
2 5
3 2
Before answering I want to point out that you are taking OrderId from a form (That is from client side) This is a wrong and INSECURE approach. Let the order id be AutoGenerated by database.
I would suggest to tweak your database design a little.
Here is a solution that will work.
Note: I am consedering your database support Auto-Increment, for MS SQL replace it with IDENTITY, for Oracle you need to create a sequence.
Product (
id INT PK AUTO-INCREMENT
);
Order (
id INT PK AUTO-INCREMENT
user-id INT FK # user who purchased
### and other common details Like date of purchase etc.
);
Order-Detail (
id INT PK AUTO-INCREMENT
order-id INT FK # Common order id
pdt-id INT FK # product which was purchased.
);
When you make a purchase:
1. Insert a row in order table
2. Fetch the last inserted id
3. Insert order-id from last step and products which are purchased in Order-Detail table,
Fetch all the orders made by a user:
1. Read from order table.
Fetch all products purchased for an order:
1. Fetch details from Order-Detail
Note: You will get List of products purchased, Use Order-detail.id as "Line"
EDIT:
Thanx to HLGEM's comment
If you think price of a product may change then instead of updating the price add a new row to the table (and flag the old table so that it wont be visible, you can also have a column in new table pointing to old table), thus old purchase will point to old product and new orders will point to updated (new) row.
There is one more approach this problem:
store the current cost of product in order-detail table.
If you are facing difficulty understanding above solution here is another and simpler one.
In Order table, Make a composite primary key including OrderNo and Line.
Whenever inserting into database you will need to generate line number in your code, which you can do by runnign a loop over array of propduct being purchased.
I think it would be better to split your current Order table into two separate tables:
Order table
(PK, Identity specification) OrderId
Perhaps other fields like Invoice address, Delivery address, etc.
OrderLine table
(PK, Identity specification) OrderLineId
(FK to Order table) OrderId
(FK to Product table) ProductId
For both tables you have an Entity in your class model: class Order and class OrderLine and a one-to-many relationship between them, so Order has a collection of OrderLines.
Creating an order with all order lines would then look like this:
var order = new Order();
foreach (var item in collection)
{
var orderLine = new OrderLine()
// Get productId from your DropDownLists
orderLine.ProductId = productId;
order.OrderLines.Add(orderLine);
}
context.Orders.AddObject(order);
context.SaveChanges();
Edit
The MVC MusicStore Tutorial might also help for the first steps to create an order processing system with ASP.NET MVC and Entity Framework. It contains classes for orders and order details (among others) and explains their relationships.
Web app is being written in classic ASP with a MSSQL backend. On this particular page, the admin can select 1 or any/all of the employees to assign the project to. I'm trying to figure out a simple way to store the employee IDs of the people assigned to it in one column.
The list of employees is generated from another table and can be dynamic (firing or hiring) so I want the program to be flexible enough to change based on these table changes.
Basically need to know how to assign multiple people to a project that can later be called up on a differen page or from a different query.
Sorry for the n00bish question, but thanks!
Don't store multiple ID's in one column! Create another table with the primary key of your existing table and a single ID that you want to store. You can then insert multiple rows into this new table, creating a 1:m (one to many) relationship. For example, let's look at an order table:
order:
order_id
order_date
and I have a product table...
product:
product_id
product_name
Now, you could go down the road of adding a column to order that let you list the products in the order, but that would be bad form. What you want instead is something like..
order_item:
order_item_id
order_id
product_id
quantity
unit_price
You can then perform a join to get all of the products for a particular order...
select
product.*
from orders
inner join order_item on order_item.order_id = order.order_id
inner join product on product.product_id = order_item.product_id
where orders.order_id = 5
Here's an example order_id of 5, and this will get all of the products in that order.
You need to create another table that stores these values such as. So this new table would store one row for each ID, and then link back to the original record with the original records ID.