Auto increment the field based on another field in Laravel model - php

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.

Related

Laravel sync a Pivot table with 3 models relationships Many to Many and One to Many

Information:
A sound company has many employees.
The employee has many positions in the company.
The company has many events. And for every event, they need a crew.
A crew is composed of many employees holding certain positions. An employee can hold multiple positions in a crew.
For example:
The list of employees is:
Employee 1 is a Driver, Sound Engineer and Stage Hand
Employee 2 is a Driver, Sound Engineer
Employee 3 is a Sound Engineer and Stage Hand
Employee 4 is a Stage Hand
Employee 5 is a Stage Hand
The event is called: Event 1
The crew is:
For the position of Sound engineer:
Employee 1
Employee 2
For the position of Stage Hand:
Employee 3
Employee 4
Employee 5
For the position of Driver:
Employee 2
Employee 1
Problem:
I believe this is done by using a pivot table that holds the event_id, employee_id and position_id
But when I follow this approach, I get stuck on feeding the data and the methods to use to create new data.
Is there a different approach?
you need two pivot tables. One for employee and position which hold employee id and position id. Another one between employee and event which holds event id and employee id. I think this is a better way to handle this. You can use the attach and detach method handling pivot table in laravel.

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.

PHP: iterate through array and assign a customer to a user, in turn

Purpose
Every time an operator manually adds a Customer to the program, he/she assigns an area to the customer as well.
There are many users and every one has one or more areas assigned.
Once the operator enters a customer and assigns an area to it, a user should be assigned to that customer, but not always the same user, it has to be a different one every time (based on a area_customer table) and this has to be in turn (1,2,3,1,2,3,1,2...).
So if 3 users are assigned to an area, the created customer is first assigned to the first user, later on a new customer is entered and is assigned to the second user, a third customer is entered and is assigned to the third user. At this point all starts again. If a fourth customer is created, the first user is assigned to this, an so forth.
Details
I have 4 MySQL tables:
Users
Areas
Area_Users
Customers
Users table has a unique id user_id and other fields for each user.
Areas table has a unique id area_id and a name field.
Area_Users table has the area_id and user_id only, this is where the assignment of areas has been made, so every user has assigned to himself one or more areas.
Customer table has a unique id customer_id, has an area_id (that relates to the Areas table) and has a user_assigned_id (related to Users table)
What I need to accomplish:
Every time a customer is added to the program, the responsible manually inserts all of the data on that customer, INCLUDING THE AREA the customer is assigned to.
At this point a row is inserted in the MySQL table Customer, but the user_assigned_id should be added programmatically (and not manually by the person).
The choice of which user_id to place in the user_assigned_id needs to be calculated from the Areas table. It should be a different person every time in turn.
This is the Areas table:
| area_id | name |
-------------------------
| 1 | Z1 |
| 2 | Z2 |
So let's say this is the Area_Users table:
| area_id | user_id |
-------------------------
| 1 | 4 |
| 1 | 6 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 2 | 5 |
| 2 | 6 |
So, the area #1 is assigned to user 4 and 6, while area #2 is assigned to 1,2,3,5 and 6. (Note: User 6 has 2 area assigned)
At this point the person creates a record for area #2, this has to be assigned to user 1, the next record that will be created goes to user #2, than #3 then #5 and then #6. Than again from #1.
Same for area #1, the first goes to user #4, then #6 then #4, then #6, and so forth...
My idea is to first find the Area data:
//Codeigniter
$areas_data = $this->db->get_where('areas', ['area_id' => 1]);
Then find how many users are in those area:
$tot_users_area = $areas_data->num_rows();
Then I can iterate through them, but how do I know what was the last assignment? And, in the case of Area #1, after user #4 comes #6 (or could be #124) so how do I go onto the next one?
What you are actually looking for is ROUND ROBIN METHOD. You have to circle the users while assigning a customer.
WHAT YOU CAN DO
Whenever you are assinging the customer, you have to make another insert in a temporary table .
This temporary table tracks which was the last user (userid) that was assigned and the area of that customer, and when you will be assinging the customer once again you will first consult this temporary table to check which user was last assigned and just increment that userid and assign the customer.
If I understand correctly, and the customer_id field in the Customers table is auto-incremented, you'd need to check the Customers table for the "last assignment", as you put it.
After you get all the $areas_data:
$areas_data = $this->db->get_where('areas', ['area_id' => 1]);
Loop through each (user_id, area) pair in the Customers table and see what exists. When you find a pair that does not exist, add it:
foreach($areasdata as $key => $areaDatum) {
$result = $this->db->get_where('customers', array('area_id' => $areaDatum['area_id'], 'user_assigned_id' => $areaDatum['user_id']));
if(!result) {
$data = array('area_id' => $areaDatum['area_id'], 'user_assigned_id' = > $areaDatum['user_id']);
$this->db->insert('customers', $data);
}
}
It's just a simple demonstration with arrays without using codeigniter and database..
<?php
$customers=[1,2,3,4,5,6,7];
$users=[1,2,3,4];
$usercustomers = [];
$userslen=count($users);
foreach($customers as $customer)
{
if($customer > $userslen){
$usercustomers = array('user' => $customer%$userslen,
'customer' => $customer);
var_dump($usercustomers);
echo "<br>";
}
foreach($users as $user)
{
if($customer%$user==0 && $customer==$user){
$usercustomers = array('user' => $user,
'customer' => $customer);
var_dump($usercustomers);
echo "<br>";
}
}
}
?>
At this point you may consider using an ORM package(Which provides reusuble solutions for situations like that)..Because there is a many to one relationship between users and customers..Using an ORM there is an easy solution.. http://www.krueckeberg.org/notes/relationships.html.. Also search the keywords "on cascade delete"and "on cascade insert"..It will be more easy using an ORM and cascading.."There is no need to reinvent the wheel"..
There is a many to many relationship between areas and users..Changing your database design may help you,and strongly recomended..In your current design what if you need to get all areas?There is not a primary key in the table areas..Also what if you need to get track assignments using date?In this solution you can add a date field into AssignedInto table and keep track assignemts etc.AssignedInto table provides the relation between the Users and Areas..
https://en.wikipedia.org/wiki/Many-to-many_(data_model)
http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561
An ORM package called doctrine..
http://www.doctrine-project.org/

The schema for MySQL lookup table

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

Storing related data from another table

I have a forum website that I'm building myself. I'm planing to add group section into my website.
Users will be able to create groups. And manage by themselves. and then the creator should add moderators to the group.
How should I store this moderators? Should I create new table like this:
group_moderators
ID - GroupID - UserID
or should I directly insert into group table
ID - GroupName - Moderaters
1 - Tech - 5, 7, 9 (These are User IDs) then I can separate them with PHP.
It depends on your plan:
if you are planning to have multiple moderators per group then you
have to create a new table for moderators
if you are planning for only 1 moderator for each group then you can add a new column to the group table
UPDATE
Multiple IDs in 1 field is not a good idea at all, it will cause a lot of headache if you want to select, update, join, delete moderators.
First option because it'll be easier to delete or update the mods and it''ll be eaiser to update the table if you intend to give different powers to different mods in future ..
For Ex
ID - GroupID - UserID - Power
1 14 1 Mod
2 14 3 Super-Mod

Categories