CakePhp中的Mysql连接表查询

时间:2017-03-15 11:25:05

标签: php mysql cakephp

你好,下面是我的表格和他们之间的关系

GigPost

enter image description here

位置

enter image description here

GigPost类别

enter image description here

我在这样的GigPost表中查询

public function getAllGigPosts(){
        $this->Behaviors->attach('Containable');
        return $this->find('all', array(

            'contain' => array(
              'User',  'GigPostAndCategory.Category','Location'

            ),
            'order' => 'GigPost.gig_post_id DESC',

            'recursive' => 0
        ));
    }

}

这是打印出来的结果,如果我运行以上查询

Array
(
    [0] => Array
        (
            [GigPost] => Array
                (
                    [gig_post_id] => 49
                    [user_id] => 31
                    [description] => gig post description
                    [deadline] => 2017-03-14 17:56:37
                    [pay] => 100
                    [type_of_payment] => 1
                )

            [User] => Array
                (
                    [user_id] => 31
                    [email] => demo@demo.com
                    [active] => 1
                )

            [UserInfo] => Array
                (
                    [user_id] => 31
                    [first_name] => john
                    [last_name] => doe
                    [phone_no] => 2147483
                    [about] =>hello I am john doe
                 [profile_img] => app/webroot/uploads/31/58b66b8605412test.png
                    [registration_date] => 2017-02-22 03:40:51
                    [device_token] => asdsadas
                    [available_for_gigs] => 1
                )

            [Location] => Array
                (
                    [gig_post_id] => 49
                    [lat] => 28.6619
                    [long] => 77.2274
                    [city] => Delhi
                    [state] =>  India
                    [country] =>  India
                    [location_string] => Delhi, India
                )

            [GigPostAndCategory] => Array
                (
                    [0] => Array
                        (
                            [gigpost_category_id] => 61
                            [gig_post_id] => 49
                            [cat_id] => 7
                            [Category] => Array
                                (
                                    [cat_name] => clothes
                                )

                        )

                    [1] => Array
                        (
                            [gigpost_category_id] => 62
                            [gig_post_id] => 49
                            [cat_id] => 1
                            [Category] => Array
                                (
                                    [cat_name] => delivery
                                )

                        )

                )

        )

现在我想获得距离100英里的那些演出帖子。我有一个SQL查询,找到两个纬度/长点之间的距离,这是

SELECT 
  *, 
   ( 3959 * acos( cos( radians(42.290763) ) * cos( radians( location.lat ) ) 
   * cos( radians(location.long) - radians(-71.35368)) + sin(radians(42.290763)) 
   * sin( radians(location.lat)))) AS distance 
FROM location 
WHERE active = 1 
HAVING distance < 100 
ORDER BY distance;

问题是我想在上面的查询中合并此查询,以便它也将获取所有这些相关的表数据。我不知道如何在我上面写的Cakephp查询中执行此查询。

1 个答案:

答案 0 :(得分:0)

请关注以下两个表的加入

$this->yourtablename->find("all", array(
  'joins' => array(
    array(
              'table' => 'anotherTableName',
              'alias' => 'aliasname',
              'conditions' => array("WHERE contition")
            ),),
    "fields" => array($fields),
    "conditions" => array($commonContion),
    "group"=>"ifyou have any group bya",
    )
  );