【发布时间】:2014-03-22 14:08:23
【问题描述】:
我需要知道添加来自 Features_properties 的蛋糕的方式。所以当我这样做时
其中
FeaturesProperty.feature_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
最重要的是
左外连接
realestate.features_propertiesASFeaturesPropertyON (Property.id=FeaturesProperty.property_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',
)
);
【问题讨论】:
-
@AD7six 感谢您指出这一点
-
您需要知道为您提供所需内容的 sql - features_properties 不在查询中,因为它有很多。请写下您的问题,以便在第一次阅读时清楚 - 不要为阅读过该问题以前版本的人写它。
-
@AD7six 我知道如何实际制作 SQL,只是我正在尝试根据控件进行操作。因为如果我只是编写查询,那对我来说不是问题。
-
@AD7six 我已经在问题文本中添加了我需要的 SQL 查询。
-
@AD7six 我需要添加到 from wold be left outer join realestate.features_properties AS FeaturesProperty ON (Property.id = FeaturesProperty.property_id)
标签: mysql cakephp cakephp-2.4