如何在查询Cakephp中包含另一个表

时间:2014-03-22 14:08:24

标签: mysql cakephp cakephp-2.4

我需要知道从Features_properties添加的蛋糕方式。所以当我做的时候

  

其中FeaturesPropertyfeature_id = 3

它不会出错

以下将生成MySQL查询

$this->Paginator->settings = array('conditions' => array($conditionID, $categoryID, $localityID, $serviceTypeID,
'Property.price BETWEEN ' . $params['priceFrom'] . ' AND ' . params['priceTo'], 
$bedrooms,$bathrooms,'FeaturesProperty.feature_id = 3','Property.viewable = 1'),
'contain' => array('Image' => array('conditions' => array('main_image' => true)),
'ServiceType','Category','FeaturesProperty', 'Condition', 'Locality',),
'limit' => 15);

可以在下面看到

SELECT `Property`.`id`, `Property`.`address1`, `Property`.`address2`,
`Property`.`area`, `Property`.`category_id`, `Property`.`condition_id`, 
`Property`.`contact_detail_id`, `Property`.`country_id`, `Property`.`description`, 
`Property`.`length`, `Property`.`main_image_id`, `Property`.`price`, 
`Property`.`service_type_id`, `Property`.`locality_id`, `Property`.`viewable`,
`Property`.`width`, `Property`.`bathroom_qty`, `Property`.`bedroom_qty`,
`Category`.`id`,  `Category`.`category`, `Condition`.`id`, `Condition`.`condition`, 
`ServiceType`.`id`, `ServiceType`.`service_type`, `Locality`.`id`, 
`Locality`.`locality`, `Locality`.`region_id`, (SELECT concat(`Locality`.`locality`," 
- ", region) FROM regions where id = `Locality`.`region_id`) AS `Locality__fullname` 
FROM `realestate`.`properties` AS `Property` LEFT JOIN `realestate`.`categories` AS 
`Category` ON (`Property`.`category_id` = `Category`.`id`) LEFT JOIN 
`realestate`.`conditions` AS `Condition` ON (`Property`.`condition_id` = 
`Condition`.`id`) LEFT JOIN `realestate`.`service_types` AS `ServiceType` ON 
(`Property`.`service_type_id` = `ServiceType`.`id`) LEFT JOIN 
`realestate`.`localities` AS `Locality` ON (`Property`.`locality_id` = 
`Locality`.`id`) 
WHERE `Property`.`price` BETWEEN 0 AND 99999999 AND 
`FeaturesProperty`.`feature_id` = 3 AND `Property`.`viewable` = 1 LIMIT 15 

我需要的是

SELECT `Property`.`id`, `Property`.`address1`, `Property`.`address2`,
`Property`.`area`, `Property`.`category_id`, `Property`.`condition_id`, 
`Property`.`contact_detail_id`, `Property`.`country_id`, `Property`.`description`, 
`Property`.`length`, `Property`.`main_image_id`, `Property`.`price`, 
`Property`.`service_type_id`, `Property`.`locality_id`, `Property`.`viewable`,
`Property`.`width`, `Property`.`bathroom_qty`, `Property`.`bedroom_qty`,
`Category`.`id`,  `Category`.`category`, `Condition`.`id`, `Condition`.`condition`, 
`ServiceType`.`id`, `ServiceType`.`service_type`, `Locality`.`id`, 
`Locality`.`locality`, `Locality`.`region_id`, (SELECT concat(`Locality`.`locality`," 
- ", region) FROM regions where id = `Locality`.`region_id`) AS `Locality__fullname` 
FROM `realestate`.`properties` AS `Property` LEFT JOIN `realestate`.`categories` AS 
`Category` ON (`Property`.`category_id` = `Category`.`id`) LEFT JOIN 
`realestate`.`conditions` AS `Condition` ON (`Property`.`condition_id` = 
`Condition`.`id`) LEFT JOIN `realestate`.`service_types` AS `ServiceType` ON 
(`Property`.`service_type_id` = `ServiceType`.`id`) LEFT JOIN 
`realestate`.`localities` AS `Locality` ON (`Property`.`locality_id` = 
`Locality`.`id`) left outer join `realestate`.`features_properties` AS `FeaturesProperty` ON 
(`Property`.`id` = `FeaturesProperty`.`property_id`)
WHERE `Property`.`price` BETWEEN 0 AND 99999999 AND 
`FeaturesProperty`.`feature_id` = 3 AND `Property`.`viewable` = 1 LIMIT 15 

最重要的部分是

  

左外联接realestatefeatures_properties AS FeaturesProperty ON       (Propertyid = FeaturesPropertyproperty_id

另外,因为它可能有助于下面是属性模型的关系

/**
 * belongsTo associations
 */
public $belongsTo = array(
    'Category' => array(
        'className' => 'Category',
        'foreignKey' => 'category_id'
    ),
    'Condition' => array(
        'className' => 'Condition',
        'foreignKey' => 'condition_id'
    ),
    'ContactDetail' => array(
        'className' => 'ContactDetail',
        'foreignKey' => 'contact_detail_id'
    ),
    'ServiceType' => array(
        'className' => 'ServiceType',
        'foreignKey' => 'service_type_id'
    ),
    'Country' => array(
        'className' => 'Country',
        'foreignKey' => 'country_id'
    ),
    'Locality' => array(
        'className' => 'Locality',
        'foreignKey' => 'locality_id'
    )
);

/**
 * hasMany associations
 */
public $hasMany = array(
    'Image' => array(
        'className' => 'Image',
        'foreignKey' => 'property_id',
        'dependent' => false,
    ),
     'DetailsProperty' => array(
        'className' => 'DetailsProperty',
        'foreignKey' => 'property_id'
    ),
     'FeaturesProperty' => array(
        'className' => 'FeaturesProperty',
        'foreignKey' => 'property_id'
    )
);

public $hasAndBelongsToMany = array(
    'Detail' =>
        array(
            'className' => 'Detail',
            'joinTable' => 'details_properties',
            'foreignKey' => 'property_id',
            'associationForeignKey' => 'detail_id',
        ),
    'Feature' =>
        array(
            'className' => 'Feature',
            'joinTable' => 'features_properties',
            'foreignKey' => 'property_id',
            'associationForeignKey' => 'feature_id',
        )
);

1 个答案:

答案 0 :(得分:1)

我找到了答案虽然可能有点愚蠢但我不知道如何处理cakephp中的连接

现在我的代码是

$this->Paginator->settings = array('conditions' => array($conditionID, $categoryID, $localityID, $serviceTypeID,
                    'Property.price BETWEEN ' . $params['priceFrom'] . ' AND ' . $params['priceTo'], $bedrooms, $bathrooms, 'FeaturesProperty.feature_id = 2', 'Property.viewable = 1'),
                'contain' => array('Image' => array('conditions' => array('main_image' => true)),
                    'ServiceType', 'Category', 'FeaturesProperty', 'Condition', 'Locality',),
                'joins' => array(
                    array(
                        'table' => 'features_properties',
                        'type' => 'left outer',
                        'alias' => 'FeaturesProperty',
                        'conditions' => array('property.id = FeaturesProperty.property_id')
                    )
                ),
                'limit' => 15);

加入的代码是

虽然这给我留下了多行,而不是一行不同的

我已经改变了使用连接来使用存在的想法,如AD7six所建议的,这解决了我的多行问题。

所以我的代码现在是

$this->Paginator->settings = array('conditions' => array($conditionID, $categoryID, $localityID, $serviceTypeID,
                        'Property.price BETWEEN ' . $params['priceFrom'] . ' AND ' . $params['priceTo'], $bedrooms, $bathrooms, '(exists (select 1 from features_properties where features_properties.feature_id = 2))', 'Property.viewable = 1'),
                    'contain' => array('Image' => array('conditions' => array('main_image' => true)),
                        'ServiceType', 'Category', 'FeaturesProperty', 'Condition', 'Locality',),
                    'joins' => array(
                        array(
                            'table' => 'features_properties',
                            'type' => 'left outer',
                            'alias' => 'FeaturesProperty',
                            'conditions' => array('property.id = FeaturesProperty.property_id')
                        )
                ),
                'limit' => 15);