Welcome Guest, Not a member yet? Register   Sign In
Models & Relational Tables
#2

While it's not 100% pretty, I'll add new methods to the model for each situation, something like:

Code:
class Booking extends Model {
    public function getTicketsForBooking(int $bookingID)
    {
        return $this->db->table('tickets')->join('booking_ticket', 'ticket_id = tickets.id')->where('booking_id', $bookingID)->get();
    }
}

Or something similar. There's usually some sanity checks in there to make sure that $bookingID is a valid booking, or it throws an exception, etc.

it's not super elegant, but it is straight-forward, easy to follow and understand. Nowadays I'll typically have an actual Repository-type class that holds an instance of the model, and has custom methods for most of the situations I need, which is why it's written like it is, but I've done it straight inside the model class itself many times.
Reply


Messages In This Thread
Models & Relational Tables - by EpicKris - 03-13-2017, 02:39 AM
RE: Models & Relational Tables - by kilishan - 03-13-2017, 01:11 PM
RE: Models & Relational Tables - by EpicKris - 03-13-2017, 02:35 PM
RE: Models & Relational Tables - by kilishan - 03-16-2017, 09:00 AM
RE: Models & Relational Tables - by SakhR - 11-06-2018, 11:02 AM



Theme © iAndrew 2016 - Forum software by © MyBB