【问题标题】:Cakephp HABTM many to many relationCakephp HABTM 多对多关系
【发布时间】:2014-09-11 06:02:00
【问题描述】:

您好,我在 cakephp 中设置简单的 HABTM 时遇到问题。

请告诉我有什么问题吗?我密切关注文档。

我的控制器(它是一个 REST 服务)

class UsersController extends AppController {

    public $components = array('RequestHandler');

    public function index() {
        $this->set(array(
            'users' => $this->User->find('all'),
            '_serialize' => array('users')
        ));
    }
}

我的 User.php 模型:

class User extends AppModel {

    public $hasAndBelongsToMany = array(
        'Sport' => array(
            'className' => 'Sport',
            'joinTable' => 'sports_users',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'sport_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'with' => ''
        )
    );
}

我的 Sport.php 模型

class Sport extends AppModel{

    public $hasAndBelongsToMany = array(
        'User' => array(
            'className' => 'User',
            'joinTable' => 'sports_users',
            'foreignKey' => 'sport_id',
            'associationForeignKey' => 'user_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'with' => ''
        )
    );
} 

这就是我的数据库的样子:

用户表:

运动桌:

最后是sports_users 表:

最后这是我得到的错误:

Fatal error: Call to a member function schema() on a non-object in C:\wamp\www\SportsAround\lib\Cake\Model\Model.php on line 3627

全图:

那么我哪里弄错了?谢谢

【问题讨论】:

  • 有人吗?我还没解决
  • 不完全清楚,你得到的错误是当你执行$this->User->find('all')时,对吧?将 recursive 设置为 -1 并重试,如果是同样的错误,则问题不在于 habtm,而在于用户模型本身(在这种情况下,发布所有用户模型以查看发生了什么)
  • 您好,感谢您的回答。上传的代码是我的完整代码。如何将递归设置为-1?是的,当我调用 find('all') 时问题就来了
  • 这实际上是我的应用程序中的所有代码。加上配置文件中的一些行。
  • 试试class User extends AppModel {public $hasAndBelongsToMany = array('Sport');}class Sport extends AppModel{ public $hasAndBelongsToMany = array('User');},在$this->set 之前放上$users = $this->User->find('all', array('recursive'=>-1)); pr($users);,看看你有什么

标签: php cakephp many-to-many has-and-belongs-to-many


【解决方案1】:

我通过改变解决了它

public $hasAndBelongsToMany = array(
    'Sport' => array(
        'className' => 'Sport',
        'joinTable' => 'sports_users',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'sport_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'with' => ''
    )
);

public $hasAndBelongsToMany = array('Sport');

public $hasAndBelongsToMany = array(
    'User' => array(
        'className' => 'User',
        'joinTable' => 'sports_users',
        'foreignKey' => 'sport_id',
        'associationForeignKey' => 'user_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'with' => ''
    )
);

public $hasAndBelongsToMany = array('User');

但我仍然不明白为什么解决了它。

【讨论】:

  • 如果您遵循 CakePhp 命名约定,您不必指定任何选项,只需像在此处那样指明关联。您在不需要的地方应用的错误设置可能会导致问题。
猜你喜欢
  • 2011-07-12
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多