Cakephp参数没有传递

时间:2012-09-25 09:31:43

标签: cakephp

我花了很多时间试图解决这个问题并且没有运气。我尝试过路由但没有成功。我先把逻辑放在代码之后。

  
      
  1. 用户在/ pra / Fields / view / 1中查看fields.template_id = 1
  2. 的字段列表   
  3. 用户点击添加新字段/ pra / Fields / add_new / 1将它们带到一个表单,在表单中输入他们想要的信息   关于他们创造的新领域。
  4.   
  5. 当用户点击添加/提交时,新字段将保存到数据库
  6.   
  7. 用户将被带回/ pra / Fields / view / 1,以便他们可以看到添加到模板中的新字段
  8.   

目前正在进行前3个步骤,但是当重定向到/pra/Fields/view/1时,用户被重定向到/pra/Fields/view/

这是add_new函数的代码

function add_new($id=null){
    //allows users to add another field to an existing template
    $this->set('title_for_layout', 'Create Fields');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.png');
    $this->layout='home_layout';
    $name=$this->Field->field('template_id', array('template_id'=>$id));
    //if the field data saves
    $this->set('name',$name);
    if(($this->Field->save($this->data)))
    {   
        //user is redirected to fields/view/$name
        $this->redirect(array('controller'=>'Fields', 'action'=>'view',$name));
    }
    //sets the $id variable
    $this->set('id',$id);

这是add_new视图

<table id="formatform">
        </br></br>
        <?php
            $options = array('Money'=>'Money','Text'=>'Text','Description'=>'Description');
            ?>
        <?php echo $this->Form->create('Field', array('action'=>'add_new')); ?>
        <?php echo $this->Form->input('id',array('type'=>'hidden')); ?>
                <tr>
                <td></td>
                <td><?php echo $this->Form->input('template_id',array('type'=>'hidden','default' => $id)); ?></td>
                </tr>

                <tr>
                <td align='center'>Field Name:</td>
                <td align='left'><?php echo $this->Form->input('name', array('label'=>false)); ?></td>
                </tr>

                <tr>
                <td align='center'>Default Value:</td>
                <td align='left'><?php echo $this->Form->input('default_value', array('label'=>false)); ?></td>
                </tr>
                <tr>
                <td align='center'>Field Type:</td>
                <td align='left'><?php echo $this->Form->input('field_type', array('label'=>false,'type'=>'select','options'=>$options)); ?></td>
                </tr>

                <tr>
                <td align='center'>Description:</td>
                <td align='left'><?php echo $this->Form->input('description', array('label'=>false)); ?></td>
                <td><?php echo $this->Form->input('active', array('type'=>'hidden','default'=>true)); ?></td>
                </tr>




    <td></td>   <td align = "left"><?php echo $this->Form->end('Submit');?></td>

这是视图功能代码

function view($name){
    //lists information about fields that correspond to the
    //template they have clicked 'view' on
    $this->set('title_for_layout', 'Field Details');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.png');
    $this->layout='home_layout';
    //sets $conditions where template_id=$name and field.active=true
    $conditions=array(
        "AND"=>array(
        'template_id'=> $name,
        'Field.active'=>true));
    //finds all fields where 'conditions'=$conditions
    $fields = $this->Template->Field->find('all',array( 
        'conditions' => $conditions));
    //sets all the variables      
    $this->set('conditions', $conditions);
    $this->set('field', $fields);
    $this->set('field', $this->paginate('Field', $conditions));  


}

2 个答案:

答案 0 :(得分:0)

您可能会遇到问题:

function add_new($id=null){
    //allows users to add another field to an existing template
    $this->set('title_for_layout', 'Create Fields');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.png');
    $this->layout='home_layout';

检查$ name是否为空

$name = $this->Field->field('template_id', array('template_id'=>$id)); 
if(!empty($name)){
    $this->set('name',$name);
    if(($this->Field->save($this->data)))
    {   
        //user is redirected to fields/view/$name
        $this->redirect(array('controller'=>'Fields', 'action'=>'view',$name));
    }
}

你的其余代码

//sets the $id variable
$this->set('id',$id);

答案 1 :(得分:0)

function add_new($id=null){
    //allows users to add another field to an existing template
    $this->set('title_for_layout', 'Create Fields');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.png');
    $this->layout='home_layout';
    $this->set('id',$id);

    if(($this->Field->save($this->data)))
    {

    $id = $this->data['Field']['template_id'];

    $this->set('id',$id);
    $this->redirect(array('controller'=>'Fields', 'action'=>'view',$id));

    }
    }