这是跨多个 HABTM 模型进行查询的一种非常糟糕的方法吗?

发布于 2024-11-04 23:26:17 字数 2204 浏览 0 评论 0原文

我正在尝试跨两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2024-11-11 23:26:17

看来您应该使用 Containable 行为: http://book.cakephp.org/view/1323 /可容纳

$contain = array(
    'Manager' => array(
        'Property'
    )
);
$conditions = array('Tenant.id' => $this->Auth->user('id'));
$tenants = $this->Tenant->find('first', array('conditions'=>$conditions, 'contain'=>$contain));

Seems like you should be using the Containable behavior: http://book.cakephp.org/view/1323/Containable

$contain = array(
    'Manager' => array(
        'Property'
    )
);
$conditions = array('Tenant.id' => $this->Auth->user('id'));
$tenants = $this->Tenant->find('first', array('conditions'=>$conditions, 'contain'=>$contain));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文