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

Is there's a way to set collection field to be protected or private

As the doc (https://docs.phalcon.io/en/latest/reference/odm.html) says:

If you’re using PHP 5.4/5.5 is recommended declare each column that makes part of the model in order to save memory and reduce the memory allocation.

With that in mind, i'm explicitly declaring each field on my model, but what i'm only able to declare it as public, if not the field wouldn't be save.

Is there's a away to the save field value and at the same time protecting my field from outside access?

Use case:

// collection
class Posts extends Collection
{
   use Behavior\Sluggable
}

trait Sluggable 
{
  // here's the problem
  public $slug;
}

Thanks



98.9k

Try creating the trait this way:

trait Sluggable 
{  
    protected $slug;

    public function setSlug($slug)
    {
        $this->slug = $slug;
    }

    public function getSlug()
    {
        return $this->slug;
    }

}

Hi, thanks for the comment,

I've just try it (i think i already did that before), but it's not working. Here's some more details:

  • Model are Phalcon\Mvc\Collection
  • I set the slug value on beforeSave method
  • PHP version 5.4.16, Phalcon version 1.2.3

PS: What would happen if the setter(Sluggable::setSlug) are protected/private



98.9k

Values are read/written from/to models using readAttribute/writeAttribute. So it does not matter if they are declared as private/protected/public, Phalcon can always write or read the property.

I'm also have the same problem with Anggia and just trying using writeAttribute. It seems doesn't works on private and protected value