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

'beforeFetch' in Model?

Hi all,

There's Phalcon\MVC\Model::afterFetch(), but there's no ::beforeFetch(), which I need to inject ACL checks.

Is there a trick I could use to always make a call to ACL before model is loaded?

Important - I don't want to overload ::find/findFirst.

Thanks!

You could use the (partially undocumented) onConstruct() method on the model(s)

beforeFetch() doesn't really make sense, as the object doesn't exist until it is fetched.

I believe you could put your ACL logic in afterFetch(), just null out the data if the ACL check fails.



51.3k

@quasipickle

as the object doesn't exist until it is fetched.

That's exactly the point. I want prevent it from being loaded, saving a DB trip. Why do you think that does not make sense?

Just semantics. You are asking an object to do work before it exists.

It sounds like determining whether or not the model should be loaded or not depends on information that is known outside the model - not information intrinsic to the model. Consequently I would think that type of logic shouldn't sit in the model anyway.