这是跨多个 HABTM 模型进行查询的一种非常糟糕的方法吗?
我正在尝试跨两个 HABTM 表进行查询。我有租赁
经理
租户
房产
。
经理
HABTM 租户
管理器
HABTM 属性
租户
HABTM 租赁
我想要做的是查找
链接到经理
的属性
列表code> 链接到租户
。我已经能够使用下面的代码完成查询,但我只能检索 Property_id
(从 Managers_Property
模型),而不是 Property.name (来自
Property
模型)。
我总有一种挥之不去的感觉,我在这里做了一些非常错误或不必要的事情,但我一直在用头撞墙,却无法弄清楚这一点。
$conditionsSubQuery['`ManagersTenant`.`tenant_id`'] = $this->Auth->User('id');
$dbo = $this->Lease->getDataSource();
$subQuery = $dbo->buildStatement(
// SELECT `ManagersTenant`.`manager_id` FROM `managers_tenants` AS `ManagersTenant` WHERE `ManagersTenant`.`tenant_id` = $this->Auth->User('id')
array(
'fields' => array('`ManagersTenant`.`manager_id`'),
'table' => $dbo->fullTableName($this->Lease->LeasesManager->Manager->ManagersTenant),
//'table' => $dbo->fullTableName($this->Lease->Property->ManagersProperty->Manager->ManagersTenant),
'alias' => 'ManagersTenant',
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => $conditionsSubQuery,
'order' => null,
'group' => null
),
$this->Lease->LeasesManager->Manager->ManagersTenant
//$this->Lease->Property->ManagersProperty->Manager->ManagersTenant
);
$subQuery = ' `ManagersProperty`.`manager_id` IN (' . $subQuery . ') ';
$subQueryExpression = $dbo->expression($subQuery);
$conditions[] = $subQueryExpression;
$properties = $this->Lease->Property->ManagersProperty->find('list', array(
'conditions' => $conditions,
'fields' => array('property_id',),
'recursive' => 3
)
);
非常感谢任何帮助。
I am trying to query across two HABTM tables. I have Leases
Managers
Tenants
Properties
.
Managers
HABTM Tenants
Managers
HABTM Properties
Tenants
HABTM Leases
What I want to do is find
a list of Properties
linked to Managers
linked to Tenant
. I have been able to accomplish the query with the code below BUT I am only able to retrieve Property_id
(from the Managers_Property
model) and not Property.name
(from the Property
model).
I get the nagging feeling I am doing something very wrong or unnecessary here but I've been banging my head against the wall and haven't been able to figure this out.
$conditionsSubQuery['`ManagersTenant`.`tenant_id`'] = $this->Auth->User('id');
$dbo = $this->Lease->getDataSource();
$subQuery = $dbo->buildStatement(
// SELECT `ManagersTenant`.`manager_id` FROM `managers_tenants` AS `ManagersTenant` WHERE `ManagersTenant`.`tenant_id` = $this->Auth->User('id')
array(
'fields' => array('`ManagersTenant`.`manager_id`'),
'table' => $dbo->fullTableName($this->Lease->LeasesManager->Manager->ManagersTenant),
//'table' => $dbo->fullTableName($this->Lease->Property->ManagersProperty->Manager->ManagersTenant),
'alias' => 'ManagersTenant',
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => $conditionsSubQuery,
'order' => null,
'group' => null
),
$this->Lease->LeasesManager->Manager->ManagersTenant
//$this->Lease->Property->ManagersProperty->Manager->ManagersTenant
);
$subQuery = ' `ManagersProperty`.`manager_id` IN (' . $subQuery . ') ';
$subQueryExpression = $dbo->expression($subQuery);
$conditions[] = $subQueryExpression;
$properties = $this->Lease->Property->ManagersProperty->find('list', array(
'conditions' => $conditions,
'fields' => array('property_id',),
'recursive' => 3
)
);
Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您应该使用 Containable 行为: http://book.cakephp.org/view/1323 /可容纳
Seems like you should be using the Containable behavior: http://book.cakephp.org/view/1323/Containable