Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a model link to a View in the database instead of a table in CakePHP?

I was wondering if it was possible to create a view in a database then be able to link a Model to it?

like image 576
Jenski Avatar asked Jun 04 '09 09:06

Jenski


People also ask

What is ORM in CakePHP?

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.

How can I get data from database in CakePHP?

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.

What is CakePHP model?

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.


2 Answers

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.

like image 99
icc97 Avatar answered Oct 14 '22 18:10

icc97


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.

like image 28
brettkelly Avatar answered Oct 14 '22 19:10

brettkelly