php - 如何在cakephp中连接三个表,其中一个是HABTM表

标签 php mysql cakephp

我有三个表: 商店 可交付成果集 城市

具有以下关系:

//Store 
    public $hasMany = array(
        'DeliverableSet' => array(
            'className' => 'DeliverableSet',
            'foreignKey' => 'store_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    //DeliverableSet
    public $belongsTo = array(
            'Store' => array(
                'className' => 'Store',
                'foreignKey' => 'store_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            )
        );
//DeliverableSet
    public $hasAndBelongsToMany = array(
            'City' => array(
                'className' => 'City',
                'joinTable' => 'cities_deliverable_sets',
                'foreignKey' => 'deliverable_set_id',
                'associationForeignKey' => 'city_id',
                'unique' => 'keepExisting',
                'conditions' => '',
                'fields' => '',
                'order' => '',
                'limit' => '',
                'offset' => '',
                'finderQuery' => '',
            )
        );

现在,我想通过 deliveradble_set 获取分配给商店的所有城市的列表。

我试过这段代码,但它不起作用。

    $data = $this->DeliverableSet->Store->find('all', array(
    'joins' => array(
        array(
            'table' => 'deliverable_sets',
            'alias' => 'DeliverableSetJoin',
            'type' => 'INNER',
            'conditions' => array(
                'DeliverableSetJoin.store_id = Store.id'
            )
        ),

         array(
            'table' => 'cities_deliverable_sets',
            'alias' => 'CityDeliverableSetJoin',
            'type' => 'INNER',
            'conditions' => array(
                'CityDeliverableSetJoin.deliverable_set_id = DeliverableSetJoin.id'
            ),
            array(
            'table' => 'cities',
            'alias' => 'CityJoin',
            'type' => 'INNER',
            'conditions' => array(
                'CityDeliverableSetJoin.city_id = CityJoin.id'
            )
        )
    ),
    'conditions' => array(
        'Store.id' => 11
    ),
    'fields' => array('DeliverableSetJoin.id', 'Store.id','CityJoin.id'),
    'order' => 'Store.created DESC'
)));
debug($data);exit;

基本上我想在 cakephp 的 find 方法中运行这个 sql :

    $set = $this->DeliverableSet->query('select cities_deliverable_sets.city_id  from stores
inner join deliverable_sets on deliverable_sets.store_id = stores.id
inner join cities_deliverable_sets on cities_deliverable_sets.deliverable_set_id = deliverable_sets.id
WHERE stores.id = 11
order by stores.id asc');

最佳答案

现在可以了:

    $this->DeliverableSet->Store = -1;
           $data = $this->DeliverableSet->Store->find('all', array(
    'joins' => array(
        array(
            'table' => 'deliverable_sets',
            'alias' => 'DeliverableSetJoin',
            'type' => 'INNER',
            'conditions' =>'DeliverableSetJoin.store_id = Store.id'

        ),

         array(
            'table' => 'cities_deliverable_sets',
            'alias' => 'CityDeliverableSetJoin',
            'type' => 'INNER',
            'conditions' =>'CityDeliverableSetJoin.deliverable_set_id = DeliverableSetJoin.id'
            ),
            array(
            'table' => 'cities',
            'alias' => 'CityJoin',
            'type' => 'INNER',
            'conditions' =>'CityDeliverableSetJoin.city_id = CityJoin.id'
        )
    ),
    'conditions' => array(
        'Store.id' => 18
    ),
    'fields' => array('DeliverableSetJoin.id', 'Store.id','CityJoin.id'),
    'order' => 'Store.created DESC'
));

结果如下:

'SELECT `DeliverableSetJoin`.`id`, `Store`.`id`, `CityJoin`.`id` FROM `directorylisting`.`stores` AS `Store` 
INNER JOIN `directorylisting`.`deliverable_sets` AS `DeliverableSetJoin` ON (`DeliverableSetJoin`.`store_id` = `Store`.`id`) 
INNER JOIN `directorylisting`.`cities_deliverable_sets` AS `CityDeliverableSetJoin` ON (`CityDeliverableSetJoin`.`deliverable_set_id` = `DeliverableSetJoin`.`id`) INNER JOIN `directorylisting`.`cities` AS `CityJoin` ON (`CityDeliverableSetJoin`.`city_id` = `CityJoin`.`id`)  WHERE `Store`.`id` = 18   ORDER BY `Store`.`created` DESC'

关于php - 如何在cakephp中连接三个表,其中一个是HABTM表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32676541/

相关文章:

MySQL CONCAT '*' 符号向数据库 toast

php - cakephp 中的自定义查询

jquery - 下拉菜单无法正常工作

authentication - CakePHP 3 : Accessing current user in model

php - 将文本区域中的行保存到数据库

php - 执行 Linux shell 脚本时出错

MySQL 存储过程错误意外字符 ":"

php - 在 JavaScript 中使用 smarty 数组

php - 在 Laravel 5 中获取数据库条目时的编码问题

mysql - 在 LiipFunctionalTest 包中使用 YEAR、MONTH、DAY mysql 函数时出错