Pergunta

I need to know the cakeish way of adding the from Features_properties. so when I do

where FeaturesProperty.feature_id = 3

it won't give an error

The below will generate a MySQL query

$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);

Which can be seen hereunder

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 

What I need is

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 

The most important part is

left outer join realestate.features_properties AS FeaturesProperty ON (Property.id = FeaturesProperty.property_id)

Also since it might help hereunder are the relations from property model

/**
 * 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',
        )
);
Foi útil?

Solução

I found the answer although it might have been a bit dumb I didn't know how to handle joins in cakephp

so now my code is

$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);

the code for the join being

Although this leaves me with multiple rows rather than a distinct 1 row

I have changed my idea of using a join to using an exists as was suggested by AD7six this has solved my problem with multiple rows.

so my code now is

$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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top