1

好的,所以对于我在 Cakephp 2.2.3 中的项目,我设置了我的数据库和控制器并烘焙了所有内容。我仔细检查了所有内容,并且一切正常(添加、编辑、删除)。我无法工作的一件事是保存到users表和表之间的中间websites表。中间表是user_websites。该user_websites表只有 3 个字段:iduser_idwebsite_id

当我以注册用户身份登录并进入网站添加页面时,我可以毫无问题地添加网站,但它只保存到websites表格中,表格中没有任何内容user_websites。摆弄 add 函数中的WebsitesController我可以让它保存website_iduser_websites表中,但同样,对于user_id. 显然,我需要发生的是,当添加一个新网站时,它应该将该记录保存到websites表中,并user_websites使用当前登录用户的 ID 和website_id刚刚保存的 ID 将记录添加到表中。任何帮助,将不胜感激。谢谢!

这是我的代码:

用户模型

public $hasMany = array(
    'UserWebsite' => array(
    'className' => 'UserWebsite',
    'foreignKey' => 'user_id',
    'dependent' => false,
    'conditions' => '',
    'fields' => '',
    'order' => '',
    'limit' => '',
    'offset' => '',
    'exclusive' => '',
    'finderQuery' => '',
    'counterQuery' => ''
    )
);

网站模型

public $hasMany = array(
    'UserWebsite' => array(
        'className' => 'UserWebsite',
        'foreignKey' => 'user_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

public $hasAndBelongsToMany = array(
    'User' => array(
        'className' => 'User',
        'joinTable' => 'users_websites',
        'foreignKey' => 'website_id',
        'associationForeignKey' => 'user_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

用户网站模型

public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Website' => array(
        'className' => 'Website',
        'foreignKey' => 'website_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

网站控制器

public function add() {
    if ($this->request->is('post')) {

        $this->Website->create();
        if ($this->Website->UserWebsite->saveAll($this->request->data, array('deep' => true))) { 
    $log = $this->Website->getDataSource()->getLog(false, false);
    debug($log);
            $this->Session->setFlash(__('The website has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The website could not be saved. Please, try again.'));
        }
    }

}
4

2 回答 2

0

当然,在寻求帮助后不久,我在朋友的帮助下解决了问题。对于其他可能需要答案或可能有更好解决方案的人,我补充说:

我只是将数据的 user_id 设置为在我的 AppController 的 beforeFilter 中设置的当前登录用户的 id。

网站控制器

public function add() {
    if ($this->request->is('post')) {
        $this->Website->create();

        // This is the line I added
        $this->request->data['user_id'] = $this->Auth->user('id');

        if ($this->Website->UserWebsite->saveAll($this->request->data, array('deep' => true))) { 
            $this->Session->setFlash(__('The website has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The website could not be saved. Please, try again.'));
        }
    }

}
于 2012-12-08T04:14:14.133 回答
0

但是,您仍然没有以正确的 CakePHP 方式进行操作。

它目前仅在工作,因为您定义了一个 hasMany,在这种情况下,它不是正确的关系,因为您在这里只需要 HABTM。

将您的表重命名为:

users_websites

这是正确的 CakePHP,正如您所看到的,您实际上也在您的 HABTM 中以这种方式定义了它。

现在您可以删除 hasMany,Cake 应该通过 HABTM 自动保存,而不是像您现在所做的那样通过 hasMany。

你仍然需要像你一样做,因为 user_id 不是通过你的表单/请求定义的,你总是需要像你一样定义它。

于 2012-12-08T13:03:09.403 回答