CakePHP HABTM表格提交 [英] CakePHP HABTM Form Submission

查看:83
本文介绍了CakePHP HABTM表格提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,问题标签,具有HABTM关系。添加问题时,我希望能够为问题指定标记(这只是第一个标记,稍后可以添加更多标记)。标签从它们的表中拉出。如何配置我的应用程序,以便在添加问题并指定标记时,连接反映在连接表( questions_tags )?

I have two tables, questions and tags, that have a HABTM relationship. When adding a question, I want to be able to specify a tag for the question (this would just be the first tag, with ability to add more tags later). The tags are pulled from their table. How can I configure my application so that when a question is added and a tag is specified, the join is reflected in the join table (questions_tags)?

这是我的问题添加操作代码:

Here is my question add action code :

function add() {
    $tags = $this->Question->Tag->find('all');
    $this->set('tags',$tags);

    if (!empty($this->data)) {
        $this->Question->create();
        if ($this->Question->save($this->data)) {
            $this->Session->setFlash(__('The question has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The question could not be saved. Please, try again.', true));
        }
    }
    $users = $this->Question->User->find('list');
    $tags = $this->Question->Tag->find('list');
    $this->set(compact('users', 'tags'));
}

这里是我的问题添加视图代码:

and here is my question add view code:

<?php 
    echo $this->Form->create('Question');
    echo $this->Form->input('user_id',array('type' => 'hidden', 'value' => $this->Session->read('Auth.User.id')));
    echo $this->Form->input('title');
    echo $this->Form->input('details',array('type' => 'textarea'));
    echo $this->Form->input('tag_id');
    echo $this->Form->end(__('Submit', true));
?>


推荐答案

首先确保您的模型设置正确办法。事实上,用户最初只是为你的问题添加一个标签不会改变你应该在问题模型和标签模型之间有一个HABTM关系的事实(因为你希望以后可能添加更多标签)。

First make sure that your models are set up the right way. The fact that a user initially just adds one tag to your question does not change the fact that you should have a HABTM relation between the Question model and the Tag model (because you want the possibility add more tags later).

如果您的 $ this-> data 数组是根据以下模式构建的:

If your $this->data array is build according to the following schema:

$this->data = array(
  'Question' => array(
    'name' => 'Trick question'
  ),
  'Tag' => array(
    'Tag' => array(1,2,3)
  )
);

然后a $ this-> Question-> save / code>将保存问题数据以及相关的标记数据(在这种情况下为问题'Trick Question',标记为id 1,2和3)。

Then a $this->Question->save() will save the Question data as well as the related Tag data (in this case Question 'Trick Question' with Tags with the id 1, 2 and 3).

也许退后一步,烘焙你的模型,视图和控制器这两个模型(再次),看看蛋糕做出了什么。如果我是正确的,你只需要一个 $ this-> Form-> input('Tag')正确的数据会自动填充选项参数,结果为 $ this-> Question-> Tag-> find list'))。

Maybe take one step back and bake your Models, Views and Controllers for these two models (again) and see what Cake makes out of it. If I'm correct you'll just need a $this->Form->input('Tag') somewhere in your form (and if that not fills in the right data automatically you want to fill the options parameter with the result of $this->Question->Tag->find('list')).

这篇关于CakePHP HABTM表格提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆