DAO-DTO-PHP-LARAVEL

Objectives: DAO VS DTO

DTO and DAO Notes

DTO and DAO Notes (English)

1. DTO (Data Transfer Object)

Definition: DTO is a class used to carry data between layers of an application (e.g., Controller → Service → DAO). It does not contain business logic, only getters, setters, and optional data transformations.

Advantages:

  • Reduces dependency on Request or database models.
  • Makes validation and testing easier.
  • Protects layers from direct data manipulation.

1.1 Example: Bill DTO

class BillReqDto {
    private $attributes = [];
    private $billId;
    private $spId;       // Service provider ID
    private $subSpId;    // Sub service provider ID
    private $ccyId;      // Currency ID
    private $totalBillAmount;
    private $billAmt;
    private $billExprDay;
    private $billExprDt;

    public function setAttributes(array $data) { ... }
    public function getAttributes() { return $this->attributes; }
    public function setBillId($billId) { $this->billId = $billId; }
    public function getBillId() { return $this->billId; }
    public function setSpId($spId) { $this->spId = $spId; }
    public function getSpId() { return $this->spId; }
    public function setSubSpId($subSpId) { $this->subSpId = $subSpId; }
    public function getSubSpId() { return $this->subSpId; }
    public function setCcyId($ccyId) { $this->ccyId = $ccyId; }
    public function getCcyId() { return $this->ccyId; }
    public function setTotalBillAmount($amount) { $this->totalBillAmount = $amount; }
    public function getTotalBillAmount() { return $this->totalBillAmount; }
    public function setBillAmt($amt) { $this->billAmt = $amt; }
    public function getBillAmt() { return $this->billAmt; }
    public function setBillExprDt($date) { $this->billExprDt = $date; }
    public function getBillExprDt() { return $this->billExprDt; }
}

Real-life context: A school generates a student bill. DTO carries data like tuition fees, lab fees, etc., from the Controller to the DAO layer.

1.2 Example: Bill Item DTO

class BillItemDto {
    private $attributes = [];
    private $description;
    private $amount;

    public function setAttributes(array $data) {
        $this->attributes = $data;
        $this->description = $data['description'] ?? '';
        $this->amount = $data['amount'] ?? 0;
    }

    public function getAttributes() { return $this->attributes; }
    public function getAmount() { return $this->amount; }
}

Example: A teacher enters multiple items like tuition, library fee, and lab fee.


2. DAO (Data Access Object)

Definition: DAO handles all database interactions. It provides methods like create(), update(), getById(), delete().

Advantages:

  • Separates database operations from business logic.
  • Easier to test (mock the DAO).
  • Reduces repeated SQL queries.

2.1 Example: Bill DAO

class BillDao {
    public function createBill(BillReqDto $dto) {
        try {
            $bill = new Bill(); // Eloquent model
            $bill->sp_id = $dto->getSpId();
            $bill->sub_sp_id = $dto->getSubSpId();
            $bill->ccy_id = $dto->getCcyId();
            $bill->bill_amt = $dto->getBillAmt();
            $bill->total_bill_amt = $dto->getTotalBillAmount();
            $bill->bill_expr_dt = $dto->getBillExprDt();
            $bill->save();

            $dto->setBillId($bill->id);
            return $bill;
        } catch (\Exception $e) {
            Log::error("BillDaoException: " . $e->getMessage());
            return null;
        }
    }

    public function getBillByBillId($billId) {
        return Bill::find($billId);
    }
}

Real-life example: School saves a student bill into the bills table.

2.2 Example: Bill Item DAO

class BillItemDao {
    public function createBillItem(BillReqDto $dto) {
        foreach ($dto->getAttributes()['billItem'] as $itemData) {
            $billItem = new BillItem();
            $billItem->bill_id = $dto->getBillId();
            $billItem->description = $itemData['description'];
            $billItem->amount = $itemData['amount'];
            $billItem->save();
        }
        return true;
    }
}

Example: Teacher enters items for a student's bill; DAO saves each item in bill_items table linked to bill_id.


3. Controller → DTO → DAO Flow

[Request] --> [Controller] --> [BillReqDto & BillItemDto] --> [BillDao & BillItemDao] --> [Database]
  • Controller handles request, validation, and response.
  • DTO carries and structures the data.
  • DAO handles database persistence.

4. Validation Example

$validator = Validator::make($billReqDto->getAttributes(), [
    'spCode' => 'required|exists:service_providers,code',
    'billAmt' => 'required|numeric|min:1',
    'ccy' => 'required|exists:currencies,code',
]);

Ensures:

  • Service provider exists.
  • Bill amount is numeric and positive.
  • Currency is valid and active.

5. Real-life Scenarios

  • School: DTO = student bill; DAO = save bill and items.
  • Water/Electricity: DTO = customer invoice; DAO = save invoice, check exchange rate.
  • Shop: DTO = order receipt; DAO = save order, order items, payment details.

Reference Book: N/A

Author name: MWALA_LEARN Work email: biasharabora12@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::