I was wondering if it was possible to create a view in a database then be able to link a Model to it?
The CakePHP ORM provides a powerful and flexible way to work with relational databases. Using a datamapper pattern the ORM allows you to manipulate data as entities allowing you to create expressive domain layers in your applications.
To view records of database, we first need to get hold of a table using the TableRegistry class. We can fetch the instance out of registry using get() method. The get() method will take the name of the database table as argument. Now, this new instance is used to find records from database using find() method.
Models represent data and are used in CakePHP applications for data access. A model usually represents a database table but can be used to access anything that stores data such as files, LDAP records, iCal events, or rows in a CSV file. A model can be associated with other models.
Yes it's possible.
Create the view as if it were a table and make sure that it adheres to all the cake rules for creating tables. Then you can create a model, controller and view as you would with any other Table. It even comes up in the cake bake app as a normal Table.
One of the nice things I found was when dealing with pagination/sorting across a very normalised database, cake becomes almost impossible to deal with. Thus you can combine all your complex joins into one database query which then plugs nicely into cake's world of pagination/sorting on one table which works like a charm.
It's not possible to insert
into a view (that I know of, but you know what they say about assumptions), so provided you're just wanting to read data from the view and insert using a model(s) associated with the actual table(s), then it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With