简体   繁体   English

模型是否可以链接到数据库中的View而不是CakePHP中的表?

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

我想知道是否可以在数据库中创建视图然后能够将模型链接到它?

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. 因此,您可以将所有复杂的连接组合到一个数据库查询中,然后将其很好地插入到蛋糕的分页/排序世界中,就像一个魅力一样。

Yes, it's possible. 是的,这是可能的。 And very useful, sometimes 而且非常有用,有时候

不可能insert到视图中(我知道,但你知道他们对假设的看法),所以如果您只是想从视图中读取数据并使用与实际表关联的模型进行插入(s),然后它应该工作。

If you use the property var $useTable in the Model you should be able to use a view. 如果在模型中使用属性var $ useTable,则应该能够使用视图。 I have not tried it but I'm pretty sure it would work, but of course you cannot insert into it. 我没有尝试过,但我很确定它会起作用,但当然你不能插入它。

In Postgresql it is possible to make insert-able view and non-insert-able table. 在Postgresql中,可以创建可插入视图和不可插入表。 The only real difference is that table contains it's data, and view selects it. 唯一真正的区别是表包含它的数据,视图选择它。 However, you can even make table to select part of it's apparent data from other tables. 但是,您甚至可以创建表以从其他表中选择部分表观数据。 That said - it seem to me that cake has troubles with treating views just as tables - but that's an arbitrary decision, not a need. 这就是说 - 在我看来,蛋糕在处理视图方面遇到了麻烦 - 但这是一个武断的决定,而不是需要。

我不熟悉CakePHP,但通常这样的框架应该将表和视图视为相同,所以我不明白为什么这不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM