Cakephp HasOne()表关系不起作用

时间:2013-05-30 06:01:46

标签: php database cakephp model

好吧,我没有看到代码和表格有任何问题,但我无法显示我需要的数据。

鉴于表已经创建并填充,这是我得到的:

class Business extends AppModel
    {
        public $name = "Business";
        public $useTable = "businesses";

        public $hasOne = array(
        'user_profiles' => array(
            'className'     => 'profiles',
            'foreignKey'    => false,
            'conditions' => array('Business.business_id = profiles.user_id')  
        ));
    }

我在我的控制器中调试它的结果是:

array(
    (int) 0 => array(
        'Business' => array(
            'business_id' => '1',
            'business_name' => 'sysnet admin',
            'mac1' => '1f',
            'mac2' => 'af',
            'mac3' => '12',
            'mac4' => 'aa',
            'mac5' => '33',
            'mac6' => 'ba',
            'area_id' => '1',
            'latitude' => '',
            'longitude' => null,
            'province_id' => null,
            'max' => '20',
            'upload' => '0',
            'download' => '0',
            'allow_bypass' => '1',
            'bypass_pin' => '0000000',
            'gate_way' => '192.168.88.1',
            'trial' => '0',
            'hasExpired' => '0',
            'isRedirectEnabled' => '0',
            'redirectLink' => 'www.google.com',
            'group_admin_id' => '1',
            'last_access_time' => '2013-05-23 09:15:37'
        )
    ),
    (int) 1 => array(
        'Business' => array(
            'business_id' => '89',
            'business_name' => 'Panthera',
            'mac1' => 'd4',
            'mac2' => 'ca',
            'mac3' => '6d',
            'mac4' => '65',
            'mac5' => '60',
            'mac6' => '8b',
            'area_id' => '1',
            'latitude' => '14.5580',
            'longitude' => '121.0183',
            'province_id' => '1',
            'max' => '20',
            'upload' => '0',
            'download' => '0',
            'allow_bypass' => '1',
            'bypass_pin' => 'X7veJ2p',
            'gate_way' => '172.16.88.1',
            'trial' => '0',
            'hasExpired' => '0',
            'isRedirectEnabled' => '0',


            'redirectLink' => 'www.google.com',
                'group_admin_id' => '2',
                'last_access_time' => '2013-05-23 09:15:52'
            )
        ),
....
}

似乎是什么问题?我还需要显示用户配置文件及其匹配的业务记录。请帮忙。我用google搜索答案,但我编码相同的行,所以我不知道问题所在的位置。

1 个答案:

答案 0 :(得分:4)

您不是根据Cake约定创建关系。

    public $hasOne = array(
    'user_profiles' => array(
        'className'     => 'profiles',
        'foreignKey'    => false,
        'conditions' => array('Business.business_id = profiles.user_id')  
    ));

VS

    public $hasOne = array(
    'UserProfile' => array(
        'className'     => 'Profile',
        'foreignKey'    => false,
        'conditions' => array('Business.business_id = Profile.user_id')  
    ));