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开发者_C百科
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.
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:
- Manager hasMany ManagersTenant
- Tenant hasMany ManagersTenant
- ManagersTenant belongsTo Manager and Tenant.
精彩评论