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

(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;
use App\Models\TicketModel;
use CodeIgniter\Database\ConnectionInterface;

class BookingRepository {
    
    protected $bookings;
    protected $tickets;
    protected $db;

    public function __construct(BookingModel $bookings, TicketModel $tickets, ConnectionInterface $db)
    {
        $this->bookings = $bookings;
        $this->tickets  = $tickets;
        $this->db       = $db;
    }

    // Basic CRUD functions for a Fluent interface to create/update the objects.
    public function find(int $id) { }
    public function findMany(array $ids) { }
    public function findAll(int $perPage=0) { }
    public function setUserID(int $id) { }
    public function setTickets(array $tickets) { }
    public function withTicket(Ticket $ticket) { }
    public function create();
    public function update(int $bookingID, array $params=[]);
    public function delete(int $bookingID);

    // Custom, task-specific methods...
    public function getTicketsForBooking(int $bookingID)
    {
        return $this->tickets->where('booking_id', $bookingID)->get();
    }

    public function addTicketToBooking(Ticket $ticket, int $bookingID)
    {
        $db = Config\Database::connect();

        return $db->table('bookings_tickets')->insert([
            'booking_id' => $bookingID,
            'ticket_id' => $ticketID
        ]);
    }

    public function cancelBooking(int $bookingID)
    {
        ...
    }
}

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()
{
    return new App\Repositories\BookingRepository(
        new App\Models\BookingModel(),
        new App\Models\TicketModel(),
        Config\Database::connect()
    );    
}


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.
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