We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model columns as private or public properties

Regarding https://github.com/phalcon/cphalcon/issues/14538 I've had the same issue that a database table column name was "source". By default I'm using protected properties a column map and public getter and setter methods to write my models, like:

<?php
namespace App\Models\Prod;

class OrdersLifecycle extends Base {
    protected $id;
    protected $orderId;
    protected $orderState;
    protected $changedDate;

    public function initialize() {
        $this->setSource('orders_lifecycle');
        $this->belongsTo('orderId', 'App\Models\Prod\Orders', 'id', [ 'alias' => 'Order' ]);
    }

    public function columnMap() {
        return [
            'id' => 'id',
            'order_id' => 'orderId',
            'order_state' => 'orderState',
            'changed_date' => 'changedDate',
        ];
    }

    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getOrderId() {
        return $this->orderId;
    }

    public function setOrderId($orderId) {
        $this->orderId = $orderId;
    }

    public function getOrderState() {
        return $this->orderState;
    }

    public function setOrderState($orderState) {
        $this->orderState = $orderState;
    }

    public function getChangedDate() {
        return $this->changedDate;
    }

    public function setChangedDate($changedDate) {
        $this->changedDate = $changedDate;
    }
}

Is this the way to go or should I use public properties like "niden" suggest in Github issue?

It doesn't really matter which option you choose.

I prefer getters/setters despite the function call overhead, because you have more flexibility.

If you want to have a reserved word for a column name (like source), you can do either of these:

class MyModel extends \Phalcon\Mvc\Model
{
    protected $source;

    public function setMySource($value)
    {
        $this->source = $value;
    }

    public function getMySource()
    {
        return $this->source;
    }
}
class MyModel extends \Phalcon\Mvc\Model
{
    public $mySource;

    public function columnMap() {
        return [
            'source' => 'mySource',
        ];
    }
}

Or simply don't use any column name that will be mapped to a reserved method...