The schema for MySQL lookup table - php

Not sure exactly how I should be setting this up as far as databases go.
In words what I need to do is query one funding source from either my credit cards table or my bank account table. There can only be one funding source between the two tables. So I am thinking do I need a lookup table?
Here is what I am thinking for tables:
table 1
id user_id credit_card_num
1 22 2222222222222222
2 55 3333333333333333
table 2
id user_id bank_account_num
1 22 2323232323232323
2 55 2323232234323423
table 3
id user_id funding_source_id type
1 22 1 1 (or CreditCard table)
2 55 2 2 (or BankAccount table)
So again a user can only have one funding source, which could either live in the credit card table or bank account table. Is this needed?
And for the Laravel crew. If this is correct is there an Eloquent relationship that would work for this? From what I am seeing in the Documentation I don't think so.
Please give me your advise.
Citti

Related

Creating Matching bonus using PHP

How can I create a networking system where
User A registers person 1 and person 2 in a week and he gets a matching bonus and then when person 1 registers another two people User A gets matching bonus from those two people.
How can I structure the database???
I'm finding it difficult to structure the database. Please can anyone help me?
This is a diagram showing what I'm actually talking about
Diagram 1
Diagram 2
Normally I would assign any bonuses the moment a person get registered. The algorithm assigning the bonuses has to deal with the complexities, not the structure of the database.
Using an algorithm for this is also more flexible, because you can change, and test, the algorithm without changing the structure of the database.
A basic setup for the database could be:
Table USERS:
UserID
Name
RegisteredByUserID
1
person A
0
2
person 1
1
3
person 2
1
4
person X
3
5
person Y
3
Table BONUSES:
BonusID
RewardedToUserID
ForRegisteringUserID
Amount
1
1
2
2,500
2
1
3
2,500
3
3
4
2,500
4
3
5
2,500
This is a very crude example. Basically when a new person is registered an algorithm sorts out which bonuses have to be given to which users and these bonuses are stored in a table.
The 'BONUSES' table can then be used to compute the total bonuses given to each user, and the total of bonuses given out for registering an user.

How to update another row's column after update on a column

As the title says I want to automatically update another row's column after an update on a specific column.
I have this table
id username direct_referral indirect_referral total_referral referrer_id paid
---------------------------------------------------------------------------------
1 aj 100 56 156 1 1
2 john 100 40 140 1 1
3 michael 100 0 100 2 1
Now in this table refferer_id = id of referrer. For example aj has referred 'john' so he get 40% of the total_referral of john i.e.(140*40/100 = 56). john has referred michael so his indirect_referral will be 40% of michael i.e. (100*40/100 = 40).
Now I want to automatically increase indirect_referral of referrer by 40% of the total_referral of referral whenever a new user joins through his referral id and paid = 1.
Please tell me how can I do this process automatically and thanks in advance.
Your problem is tricky. But it might be solvable by a recursive SQL statement, also called CTE (common table expression).
There you have one initial query as an anchor member (the row you want to insert) and a recursive query that calls itself again and again.
Look at that link for an deeper explanation: https://www.mysqltutorial.org/mysql-recursive-cte/
Another way of solving your problem is to make a lot of SQL queries, that you manage by your PHP code.

Auto increment the field based on another field in Laravel model

I have implemented a project in Laravel.
The following is my database schema.
A user has many services and a service has many tasks. I have a table
"service_tasks" with task_id (auto increment), service_id and
task_order fields.
Now when a new task is created, the task order should be auto increment according to the service_id field.
So for example like this.
id service_id task_order
1 101 1
2 101 2
3 102 1
4 102 2
5 103 1
I implemented the following solution.
I count the total task for a service first and add plus one for the order field but When two or more user simultaneously add the task for the same service then the order is not working for a service(the same user is logged in more than one devices and working on the same service).
Updated
$task = new ServiceTask();
$task->service_id = 139;
$task->task_order = ServiceTask::where('service_id',139)->count()+1;
$task->save();
Note that I can't change the table structure.

Symfony3 with checkboxes retrieved from database tables

I'm trying to get a good Symfony3 solution for a Form containing checkboxes. The total number of checkboxes are retrieved from one table (the CategoryList table) and in another table I've stored which Categories for a certain domain are selected.
Table: Domain
ID DomainName
1 TestDomain1
2 TestDomain2
3 TestDomain3
Table: CategoryList
ID CategoryName
1 Unknown
2 Cat-1
3 Cat-2
Table: DomainCategory
ID Domain_ID DomainCategoryList_ID
1 1 1
2 1 2
3 1 3
4 2 2
5 3 1
The DomainCategory table contains the relationship between the Domains and the categories to which the Domains belong to.
Domains --> DomainCategory <--> CategoryList
How can I create a Symfony3 form for this?
I tried to use the EntityType::class and passing the CategoryList object to it, then I get indeed all the Categories as checkboxes but I cannot specify which checkbox needs to be checked/ticked depending on the DomainCategory table.
Any suggestions on how to solve this? Or any good reference to an example implementation. I was not able to find it after spending a week solving this problem.

How to Store Multiple Options selected by User in a Table

So I want my users to be able to restrict who may contact them.
There are several factors they should be able to filter, including Age (e.g. Must be between 18 - 29), income (must earn between $25,000 - $60,000), what they're looking for (e.g. Friendship, Hang out, etc.), what drugs they do (Marijuana, Meth, Cocaine, etc), etc.
The problem is, I want them to be able to select and store multiple choices for some of the criteria (e.g. drugs), but I do not know how I should store that in the DB or how I should structure the table to best accomplish that.
For example, how would I store a user's row that for "drugs" chose "Marijuana", "Cocaine", and "Heroin" within this context? Would I simply store those as comma-separated values in the "Drugs" column? Or should I do it in a completely different way?
What would be the best way to do this (considering I will obviously have to retrieve and check this information every time a user wants to contact another user) and why?
No, don't store the values in CSV format in the database. Instead create a join table called user_drug and store one row for each user/drug combination:
user
id name income
1 Foo 10000
2 Bar 20000
3 Baz 30000
drug
id name
1 Marijuana
2 Cocaine
3 Heroin
user_drug
user_id drug_id
1 1
1 2
2 1
2 3
3 3
A DB column (at least theorethically) should NOT hold multiple values. Unfortunately, there are some programmers that store multiple values in a single column (values separated by comma for examples) - those programmers (in most cases) destroy the concept of DB and SQL.
I suggest you to read about Database Normalization to get a start in organizing your tables. And, do your best to achieve the Codd's Third Normal Form
You can try with this:
criterium
------------
user_id type value
1 AGE_MIN 18
1 AGE_MAX 29
1 INCOME_MIN 25000
1 INCOME_MAX 60000
1 DRUGS Marijuana
1 DRUGS Meth

Categories