CakePHP 中的递归分页不起作用
如果这个问题在其他地方被问过并得到回答,我深表歉意,但我已经看过但未能找到它。
我有三个模型: Manager
HABTM Tenant
我还有一个 ManagersTenant
模型将两者联系在一起。
我正在尝试使用分页在 ManagersTenant
控制器中递归显示来自 Manager
和 Tenant
的字段。这是我的代码:
$this->ManagersTenant->recursive = 2;
$this->set('managersTenants', $this->paginate('ManagersTenant', array(),array('recursive'=>2)));
这仅显示 ManagersTenant
中的字段 (id
, tenant_id< /code>、
manager_id
),但不会从关联的 Manager
和 Tenant
模型中检索数据。
我还在做一个 debug($this->ManagersTenant->find('all'));
,它完美地执行递归并显示正确的数组。
我做错了什么?我需要对我的模型做一些特别的事情吗?
非常感谢任何帮助。
//编辑:
我想要做的是显示 Tenant_id
或 Manager_id
与登录用户的 ID 匹配的所有匹配项。例如,如果登录的Manager
在Tenant
模型上执行index
函数,我想对所有Tenants
code> 将针对 Tenant_id
显示,其中 Manager_id
(在 ManagersTenant
模型中)== $this->Auth->;用户('id')
。我的印象是,为了做到这一点,我必须使用 HABTM 表。但如果我可以在没有连接表的情况下执行Manager
HABTM Tenant
,我完全赞成尝试。
I apologize if this question has been asked and answered elsewhere but I have looked and have not been able to find it.
I have three models:Manager
HABTM Tenant
I also have a ManagersTenant
model to tie the two together.
I'm trying to use pagination to recursively display fields from Manager
and Tenant
in the ManagersTenant
controller. Here is my code:
$this->ManagersTenant->recursive = 2;
$this->set('managersTenants', $this->paginate('ManagersTenant',array(),array('recursive'=>2)));
This displays only the fields in ManagersTenant
(id
, tenant_id
, manager_id
) but does not retrieve data from the associated Manager
and Tenant
models.
I am also doing a debug($this->ManagersTenant->find('all'));
which performs the recursion perfectly and displays the right arrays.
What am I doing wrong? Do I need to do anything special with my model(s)?
Any help is much appreciated.
//edit:
What I'm trying to do is display all matches where Tenant_id
or Manager_id
matches the logged-in user's id. For example, if a logged-in Manager
performs the index
function on the Tenant
model, I would like for all Tenants
to be displayed for Tenant_id
where Manager_id
(in the ManagersTenant
model) == $this->Auth->User('id')
. I was under the impression that in order to do this, I had to utilize a HABTM table. But if it is possible for me to do Manager
HABTM Tenant
without a joining table, I am all for trying it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能不需要定义 ManagersTenant 模型。您应该在管理器和租户模型中定义 HABTM ,并使用这些模型供您查询。
如果您确实需要 ManagersTenant 模型,您应该使用 加入模型 关系:
You probably don't need to define the ManagersTenant model. You should define HABTM in the Manager and in the Tenant model, and use those models for your query.
If you really need a ManagersTenant model you should use the join model relationship: