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

ArrayAccess Phalcon\Mvc\Model (Paginator)?

Hi there,

I'm using a generic Volt template used for paginator model results. Depending if I define columns or not in my find or query-builder, a paginator item will be a Model object (Phalcon\Mvc\Model), or it will be Phalcon\Mvc\Model\Row

(For some background info on this, see: https://forum.phalcon.io/discussion/1297/findfirst-inconsistent-return-objects)

The Row class implements ArrayAccess, Phalcon\Mvc\Model does not. This poses a bit of a problem in my template (where I (currently) expect array access), unless I do some kind of pre-processing, or making sure I always specify the columns in my query.

Wouldn't it be a good idea to add ArrayAccess to models? Or is this not possible, or would it be inefficient somehow?

A solution for me would be to just access the items as an object of course, but ArrayAccess is quite handy in Volt when you're using variable property names, as this won't work out of the box otherwise (see: https://forum.phalcon.io/discussion/1231/volt-access-to-object-property-using-variable)

ps. I'm aware of the toArray() method, but I believe/ already assessed it doesn't quite work the same as the ArrayAccess of the Row class.

Why don't you just always define the columns, then you'll always have a Row?



8.1k

@quasipickle: sure, that's possible :)) Though, this might be something you don't expect, as is also discussed in https://forum.phalcon.io/discussion/1297/findfirst-inconsistent-return-objects

Having ArrayAccess for models would remove some of the 'inconsistency'.