【问题标题】:Model Association Not Correct模型关联不正确
【发布时间】:2014-02-20 19:49:16
【问题描述】:

我用 CakePHP 几个月大。这是我第一次尝试 CakePhp 协会。我假设我几乎遵循了所有说明,但我的模型似乎仍然无法正常工作。

这是简单的“用户”和“个人资料”模型。表结构:用户:
- id(主键)
- 名称

简介:
- id(主键)
- 角色
- user_id(参考密钥)

型号:用户:

class UserModel extends AppModel{
    var $name = 'User';
    public $hasOne = array(
        'Profile'
    );

}

简介:

class ProfileModel extends AppModel{
    var $name = 'Profile';
    public $belongsTo = array('User'); } 

控制器:用户:

lass UsersController extends AppController{
    var $name = 'Users';
    public $scaffold;

    function index(){
        $user = $this->User->find('all'); //HERE I expect to get User and Profiles data
        pr($user);
    }

} 

个人资料:

 class ProfilesController extends AppController{
    var $name = 'Profiles';
    public $scaffold;

    function index(){
        $profile = $this->Profile->find('all');
        pr($profile);
    }

} 

如果我运行用户:/localhost/test_php_apps/users/ 我得到:

Array (
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 1
                    [name] => rohini
                )

        )

) 

我想知道为什么没有显示“个人资料”数据。我在表中手动添加了记录。

如果我在 UsersController 中尝试:$user = $this->User->Profile->find('all'); 我会收到以下错误:

在非对象上调用成员函数 find()

我的猜测是设置关联有问题。但不确定是什么搞砸了。

我知道这是一个非常基本的问题,但即使阅读了 10 到 15 个相关案例,我似乎也找不到答案。

任何帮助将不胜感激。

【问题讨论】:

  • 我没有看到任何可控制的操作在这里发生。你$recursive的价值是什么?
  • 我已经设置好了:class AppModel extends Model { public $actAs = 'Containable'; }
  • 不要使用 Containable。这很简单。当您收到“在非对象上调用成员函数 find()”时,表示您的模型未关联。可容纳用于其他目的

标签: cakephp


【解决方案1】:

帮我一个忙,改变

class UserModel extends AppModel

class User extends AppModel

class ProfileModel extends AppModel

class Profile extends AppModel

看看在 UsersController 中执行 $user = $this->User->Profile->find('all'); 是否有帮助。

还有,是

public $actsAs = array('Containable');

不是

public $actAs = 'Containable';

不是actAs。看看是否有帮助。如果没有,了解您的蛋糕版本会很有帮助。

请参阅here 了解可包含性和this 了解命名模型约定。

【讨论】:

  • 我按照你说的做了,但是没有用。我的蛋糕版本是 2.4。
  • 是否至少将错误从Call to a member function find() on a non-object 更改为其他内容?
  • 如果您设置了可包含,则将'contain' => array('Profile') 选项添加到您的find() 调用中
  • 没有错误保持不变。我确实将其更改为: $user = $this->User->Profile->find('all',array('contain' => array('Profile') )); //不确定这个方法是否正确。
  • 在没有配置文件的情况下尝试它,但包含它:$user = $this->User->find('all',array('contain' => array('Profile') ));
【解决方案2】:

如果你烤这个东西会让你的生活更轻松。

型号:用户:

    class User extends AppModel{
        var $name = 'User';
        public $hasMany = array(
            'Profile'=>array(
                'className' => 'Profile',
                'foreignKey' => 'user_id',)
        );    
}

简介:

class Profile extends AppModel{
    var $name = 'Profile';
            public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
        )
    );

}

控制器:用户:

    class UsersController extends AppController{
        var $name = 'User';        

        public function index(){
            $users = $this->User->find('all'); 
            var_dump($users);
        }

} 

控制器配置文件:

    class ProfilesController extends AppController{
            var $name = 'Profile';        

            public function index(){
                $user_id = 2;
                $users = $this->Profile->find('all', array('conditions'=>array('Profile.user_id'=>$user_id))); 
                var_dump($users);
            }

    } 

【讨论】:

  • 我试过这个,但仍然没有运气。我希望 sql 查询使用一些连接。目前查询看起来像:
    SELECT Profile.id, Profile.role, Profile.user_id FROM test.profiles AS @9876543433@ WHERE @@9876 987654335@ = 1
  • 只是想添加我的sql查询,不知道哪里错了,CREATE TABLE users (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(60)); CREATE TABLE 配置文件(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,角色 varchar(60),user_id INT,CONSTRAINT fk_usr_profile FOREIGN KEY(user_id) REFERENCES test.users(id));
  • 我明白了.. 您的数据库配置不正确。或者您在控制器中的保存语句不正确。如果你有它,让我为你检查一下
  • 我使用的是 WAMP - MySql,我可以手动插入数据,所以没有问题。即使我知道数据库存在一些问题,也无法在我在本地主机上工作时向您展示。任何关于数据库步骤的输入都会非常有帮助。
  • 如果使用 $this->User->query("SELECT users.* , profiles.* from users INNER JOIN profiles ON users.id = profiles.user_id");然后它给了我预期的结果:Array ( [0] => Array ( [users] => Array ( [id] => 1 [name] => david ) [profiles] => Array ( [id] => 1 [角色] => 管理员 [user_id] => 1 ) ) )
【解决方案3】:

在您的用户模型中添加以下行

public $useTable = 'YOUR USERTABLENAME';

改变

public $hasOne = array(
        'Profile'
    );

public $hasOne = 'Profile';

在您的配置文件模型中添加相同的内容。

public $useTable = 'YOUR PROFILETABLENAME';

改变

public $belongsTo = array('User'); 

public $belongsTo = array('User' => array('className' => 'User',
                                          'foreignKey' => 'user_id'));

在你的用户控制器中添加这个

public $uses = array('User','Profile');

通常,当您尝试查询时,它现在应该可以工作了

$u = $this->User->find('all',array('contain' => array('Profile'));

$u = $this->User->find('all',array('recursive' => 2));

但如果你只写它也应该工作:

$u $this->User->find('all');

问候

【讨论】:

  • 没用,不知道什么时候真的出错了。这很简单,但不幸的是不适合我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多