Models & Relational Tables - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Models & Relational Tables (/showthread.php?tid=67594) |
Models & Relational Tables - EpicKris - 03-13-2017 I have 3 database tables: Code: CREATE TABLE booking But I only want 2 models: PHP Code: class Booking extends Model And 2 domain objects: PHP Code: class Booking How should I handle the `booking_ticket relational table and the domain objects `Booking->tickets` and `Ticket->booking`? I've got a half-baked solution that I'm really not happy with and would like to see what others solutions are? RE: Models & Relational Tables - kilishan - 03-13-2017 While it's not 100% pretty, I'll add new methods to the model for each situation, something like: Code: class Booking extends Model { 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. RE: Models & Relational Tables - EpicKris - 03-13-2017 (03-13-2017, 01:11 PM)kilishan Wrote: While it's not 100% pretty, I'll add new methods to the model for each situation, something like: Could you explain more about your repository-type classes? To use my domain models in my models I've created a basic data mapper (to map arrays to objects) and extended the core model's class to array method. RE: Models & Relational Tables - kilishan - 03-16-2017 (03-13-2017, 02:35 PM)EpicKris Wrote: Could you explain more about your repository-type classes? Sure. Basically, I create a new class to act as a Repository and is a layer that separates me from the model. That way if I ever need to change the model, or the type of database it's using, etc, I can do that at the Repository level and not impact the rest of the app. With a fluent interface it might look something like: Code: use App\Models\BookingModel; The other thing nice is that this allows method names that read well in the controllers. Additionally, this can be much easier to test in many cases. In CI4, I'd create a service for this in Config\Services.php Code: public static function BookingRepo() Hope that makes sense. It's by no means the only way to do it, but even at the current company I work at, which is a Laravel shop, we do something very similar, ignoring much of the relationship magic in Eloquent. RE: Models & Relational Tables - SakhR - 11-06-2018 Realy the CI4 gonna need a huge documentation due to the amount of features supported and algorithmes structures thanks Community && Developpers ^^ |