Query Matching hasMany field in CakePHP - php

I can't find a working query that match my need.
I simply have a Bookings model and a Bookingsstatus :
Bookings hasMany Bookingsstatus
In that way, I can keep all changes to the status and when I want the status of a Booking, I just have to take the last associated entry.
BUT,
is there a query to get all Bookings currently matching a specific status ? (not a booking that has at some point the specific status)
I tried with matching but it's not working ... Does anyone has a lead ?
Thanks

Related

The problem to get the % of all the attendance for each student

My aim is to get the presentage of the students with other attributes too, based on present or absent. I have search everything but i dont know how to implement it in Laravel, Im confuse i dont know why.
I first tired to get the total count but nothing worked. please help me to get this done in laravel query builder. Thanks
DB::table('attendances')->
select(
'courses.name as course_name',
'sections.name as section_name',
'students.name as student_name',
'students.rollnumber as student_rollnumber',
'attendances.is_present as is_present',
'attendances.date as date',
DB::raw('COUNT(attendances.id) as tcount')
)
->join('courses','courses.id','attendances.course_id')
->join('sections','sections.id','attendances.section_id')
->join('students','students.id','attendances.student_id')
->where('attendances.student_id',$id)
->where('attendances.course_id',$course_id)
->where('attendances.section_id',$section_id)
->groupBy('attendances.id')
->get();
My expect result, i will get all other info with a attribute names as percentile. Hint(I have an attribute in attendences table call is_presnet with value 0 or 1)
Well as I understand you need to get the % attendance of each student for the given course.
In your SQL query group by attendances.id is not needed since each attendance id is unique. If you remove following two lines:
DB::raw('COUNT(attendances.id) as tcount')
and
->groupBy('attendances.id')
It should return all the attendances of the student in the given course. You can loop over the result data using Php. Within the loop you can check how many present rows are present. If you divide the total number of presents by the total number of attendances, you will get the % attendance of the student for the given course

Wanting to update with two conditions both with multiple values

I am building a status system that shows satus of units, if you press a button the status of the task will update if your name is like one of the roles for that task. however i don't want the status to downgrade, it needs to display the latest status. So what i wanted to do is like this
update status Set status=cleared where jobtask1=name or jobtask2=name AND status=in_progress OR status=in_wait
UPDATE data
SET status='cleared' WHERE person1='$name' OR person2='$name' OR person3='$name' OR person4='$name'
AND status='in_progress' OR status='preparation' OR status='in_wait' OR
status=''
So it results in checking if one of the persons is the person updating the status or if the status is like one of the specified statusses. I want it to check if the person is like the person updating the status, if so then check if the status is like one of the specified statusses and if both are correct update the status.
you could try to use SWITCH CASE logic
UPDATE data
SET status =
CASE
WHEN (person1='$name' OR person2='$name' OR person3='$name' OR person4='$name') THEN 'cleared'
WHEN (<second_condition>) THEN 'in_progress'
...
ELSE ''
END
You should also consider redesigning the DB according to normalization principles.
Thanks Salman and nacho for the solution, I just needed to add parenthesis.

Laravel 5.1 / Eloquent: How do I use a 3rd table to select from the second table

I have three tables: users, purchase_orders and approvals.
One purchase_order has to be approved by multiple users.
When a new purchase_order gets created, I also create 3 pending approvals belonging to that PO.
The approvals table has a field allowed_user_type that determines who can approve it.
I can't figure out, what is the Eloquent way of selecting the pending purchase orders that can be approved by a specific user, as these are determined from the approvals table.
So far I can pull the pending approvals from the approvals table for a user with the following in the User model.
public function approvals_pending()
{
return $this->hasMany('App\Approval', 'allowed_user_type', 'user_type')
->where('approved', '=', 0);
}
The question is, how do I combine this with a theoretical filter?
I mean ideally, I would love to write:
return $this->hasMany('App\PO')->whereIn('id', '=', $this->approvals_pending()->get()->po_id);
Or something like that...
Any ideas would be greatly appreciated.
OK, for anyone interested I found a solution:
It's very close to what I thought I would have to write.
The lists method basically creates a single array out of the selected field, so it can be plugged-in directly to a whereIn method like so:
return \App\PO::whereIn('id', $this->approvals_pending()->lists('po_id'));
I don't know if this is the most Eloquent way of doing this but it does work.

Mysql - to create a new table or not to create

I've the following model Order.
Any order can have such status as new, in work, being delivered, on storage, executed, cancelled.
I found the following code in the model:
As for now every order has number what identifies its status.
If I started the project from scratch I would rather create a separate table with the name let's say order_status and insert primary keys from it into Order table.
What approach is more preferred and why?
Thanks
you can take a column named as status and set the number what ever it is like 1,2,3,4,5,this approach is more convenient than creating a new relation ship table because it uses extra join to retrieve status,it will be useful if order have multiple status at the same time otherwise you can update status of that order in same table.

Yii - Include field from another model

my question consists of two parts:
1, I'm developing small ticketing software for technical support. I have model and views for Workorder (type of ticket). I would like to include another model's field/input into this model. This model is called WorkorderJournalUpdate. It's basicly just table with 3 rows: *id, workorder_id, text*. In this case, "workorder_id" is in HAS_MANY relation with "workorder_id" in Workorder model/table.
The reason why I want to include this field/model is to create unique journal record after each Workorder update, so it looks like this:
http://luzer.eu/pu_files/27844_RelationsTC.jpg
//Edit: It would be great, if I could add new journal entry on each page update and as well read all previous records.
2, this part of my question is simple. I would like to make rule in model to make certain
fields required, but only if other field is filled in. For example:
Order ID:
Ordered by:
If Order ID is not filled in, Ordered by should not be required. If Ordered by is filled in and Order ID is not, Order ID should be required. Is this possible with "required on" rule?
This is all, hopefully, somebody will be able to help me. Thank you in advance!
May be this like can help you
http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/

Categories