在Cakephp连接查询中返回的字段为空

时间:2015-07-30 07:01:46

标签: php mysql cakephp

我尝试使用多个连接查询,但很少有字段返回空。我使用了以下查询。当我在MySql中使用类似的查询时,我得到了我需要的结果,但这在cakephp查询中不起作用。

$selQry2= $this->CustomerProfile->find('all',array(
                              'fields'=>array('CustomerProfile.firstname as cust_firstname,CustomerProfile.lastname as cust_lastname,
                                               CustomerAddress.address1 as cust_address1,CustomerAddress.address2 as cust_address2,CustomerAddress.phone1 as cust_phone,
                                               CustomerAddress.email as cust_email,CustomerAddress.city as cust_city,CustomerAddress.state as cust_state,
                                               CustomerAddress.zipcode as cust_zipcode,AddedbyCustomer.'. configure::read('log_usr_fld').',
                                               UsersProfileBackend.firstname as sal_firstname,UsersProfileBackend.lastname as sal_lastname,
                                               UsersProfileBackend.phone as sal_phone,UsersProfileBackend.email as sal_email'),
                                'conditions'=>array('CustomerProfile.customerid'=>$customer_id),
                                'joins'     =>array(array (
                                                    'table'=>'customer_address',
                                                    'alias'=> 'CustomerAddress',
                                                    'type' => 'left',
                                                    'conditions' => array('CustomerProfile.customerid = CustomerAddress.customerid')
                                                    ),
                                                array(
                                                    'table'=>'addedby_customer',
                                                    'alias'=> 'AddedbyCustomer',
                                                    'type' => 'left',
                                                    'conditions' => array('CustomerProfile.customerid = AddedbyCustomer.customerid')
                                                    ),
                                                array(
                                                    'table'=>'users_profile_backend',
                                                    'alias'=> 'UsersProfileBackend',
                                                    'type' => 'left',
                                                    'conditions' => array('AddedbyCustomer.'. configure::read('log_usr_fld') => 'UsersProfileBackend.user_id' )
                                                    ),
                                    ),
                                'limit'     =>  1
                        ));

我获得上述查询的结果是:

Array
(
    [0] => Array
        (
            [cust_firstname] => betty
            [cust_lastname] => haycraft
            [cust_address1] => 
            [cust_address2] => 
            [cust_phone] => 2702302676
            [cust_email] => testsasdf@test.com
            [cust_city] => 
            [cust_state] => 
            [cust_zipcode] => 
            [allocatedtoprimaryid] => 239
            [sal_firstname] => 
            [sal_lastname] => 
            [sal_phone] => 
            [sal_email] => 
        )

)

查询的预期结果是:

Array
(
    [0] => Array
        (
            [cust_firstname] => betty
            [cust_lastname] => haycraft
            [cust_address1] => 
            [cust_address2] => 
            [cust_phone] => 2702302676
            [cust_email] => testsasdf@test.com
            [cust_city] => 
            [cust_state] => 
            [cust_zipcode] => 
            [sal_firstname] => Cl
            [sal_lastname] => Wilson
            [sal_phone] => 270-737-0005
            [sal_email] => c.l.wilson@kiastoreetown.com
        )

)

Sql查询是:

SELECT a.firstname as cust_firstname,a.lastname as cust_lastname,b.address1 as cust_address1,
                                   b.address2 as cust_address2,b.phone1 as cust_phone,b.email as cust_email,b.city as cust_city,
                                   b.state as cust_state,b.zipcode as cust_zipcode,d.firstname as sal_firstname,d.lastname as sal_lastname,
                                   d.phone as sal_phone,d.email as sal_email 
                                   FROM customer_profile as a left join customer_address as b 
                                   on a.customerid=b.customerid  
                                   left join addedby_customer as c 
                                   on a.customerid=c.customerid  
                                   left join users_profile_backend as d 
                                   on c." . configure::read('log_usr_fld') . "=d.user_id  WHERE a.customerid='$customer_id' LIMIT 1

0 个答案:

没有答案