• 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply


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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.