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

Use database view as SELECT source

Hi, it is possible to use database view as source for SELECT queries? I know that in phalcon Model it is possible to define getSource() as name of table or view, but in case of INSERT this won't work without special view definition ("ON INSERT", etc). So my question is it is possible to use different source on INSERT/DELETE queries and another on SELECT?

Hi! You can rewrite function save or find/findFIrst

You can rewrite function save or find/findFIrst

I was thinking in that way. I found that Phalcon Model have got protected method _preSave. I can in that method set db Source.

I have to check that but in case of INSERT or UPDATE i thing _preSave is invoked. I'm not sure what if I will need to execute DELETE. If I want to select then I thing _preSave is not invoked.

Can someone confirm that?

If you need delete you can rewrite function delete

In any case, you can to implement the interface class Model https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model.html - ie to create your own base model.

If you will don't use your model for relations or somithg what need Phalcon model as parent class - try don't use Phalcon model. Just create class with same interface but not full implement interface:

  • class property,
  • set/get for property,
  • function save
  • function delete
  • several static function findBy..., findWith..., ... - for select

I think rewrite method that not defined in interface is bad idea.

IMHO

sorry for english (