如何为嵌套关系构建适当的CakePHP查询

时间:2015-08-17 13:06:09

标签: php mysql cakephp cakephp-2.0

我是CakePHP的新手,我正在尝试为结果构建复杂的查询。它杀了我。也许some1可以帮助我。 我使用蛋糕2.7

我有两个表有3个关系(多对多)。 NeighbourhoodPolygon。 案例看起来像Neighbourhood有很多Neighbourhoods,它也属于许多Neighbourhoods。此外,Neighbourhood有许多PolygonsPolygon属于mnay Neighbourhoods

Neighbourhood表包含2个字段namezip。我从用户那里得到的是zip code

现在我想要的是: 我想从Polygons及其Neighbourhood获取所有NeighboursNeighbourhood.zip = defined by user

我该怎么做?我应该编写自定义查询或将proccess分成更小的步骤吗?我整天都在和这个人打架。

以下是模型关系的外观:

class Neighbourhood extends AppModel
{
    var $hasAndBelongsToMany = array(
        'Neighbourhoods' => array(
            'className' => 'Neighbourhood',
            'joinTable' => 'neighbourhoods_neighbours',
            'foreignKey' => 'neighbourhood_id',
            'associationForeignKey' => 'neighbour_id',
            'unique' => false
        ),
        'Polygon' => array(
            'className' => 'Polygon',
            'joinTable' => 'neighbourhoods_polygons',
            'foreignKey' => 'neighbourhood_id',
            'associationForeignKey' => 'polygon_id',
            'unique' => false
        ),
    );
}


class Polygon extends AppModel
{
    var $hasAndBelongsToMany = array(
        'Neighbours' => array(
            'className' => 'Neighbourhood',
            'joinTable' => 'neighbourhoods_polygons',
            'foreignKey' => 'polygon_id',
            'associationForeignKey' => 'neighbourhood_id',
            'unique' => false,
        )
    );
}

0 个答案:

没有答案