使用一个视图和控制器cakephp编辑2个模型

时间:2014-04-11 15:22:06

标签: cakephp cakephp-2.3

我有2个型号。类型学和类型学图片。和2个控制器类型和类型图片。我想从一个控制器编辑typology和typologyPcture。

<?php echo $this->Form->create('TypologyPicture', array('type'=>'file')); ?>
<legend><?php echo __('Edit Typology Picture'); ?></legend>
<?php echo $this->Form->input('id'); ?>
<!-- Here is the first part for the update Typolohgy -->
<?php echo $this->Form->input('Typology.item_id',array('empty'=>true)); ?>
<?php echo $this->Form->input('Typology.title'); ?>
<?php echo $this->Form->input('Typology.description');?>
<?php echo $this->Form->input('Typology.thumbnail',array('type'=>'file')); ?>
<?php echo $this->Form->input('Typology.typology_category_id',array('empty'=>true)); ?>
<?php echo $this->Form->input('Typology.typology_condition_id',array('empty'=>true)); ?>
<?php echo $this->Form->input('Typology.price',array('placeholder'=>'Price')); ?>
<!-- Here is the second part for the update Typpology Picture -->
<?php echo $this->Form->input('pic_path', array('label'=>'Picture','type'=>'file'));  ?>
<?php echo $this->Form->end(__('Submit')); ?>

这是观点。其中包含类型学的字段(item_id | title | description | thumbsnial | typology_category_id | typology_condition_id) 和typologypictures的字段(pic_path)

控制器(位于TypologyPicturesController)是:

public function edit($id = null) {
    if (!$this->TypologyPicture->exists($id)) {
        throw new NotFoundException(__('Invalid typology picture'));
    }
    if ($this->request->is(array('post', 'put'))) {
            if(empty($this->data['TypologyPicture']['pic_path']['name'])){
                unset($this->request->data['TypologyPicture']['pic_path']);
              }
        if ($this->TypologyPicture->saveAll($this->request->data)) {
            $this->Session->setFlash(__('The typology picture has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The typology picture could not be saved. Please, try again.'));
        }
    } else {
        $options = array('conditions' => array('TypologyPicture.' . $this->TypologyPicture->primaryKey => $id));
        $this->request->data = $this->TypologyPicture->find('first', $options);

        $opt =  array('conditions' => array('Typology.id' => $this->request->data['TypologyPicture']['typology_id']));
        $this->request->data = $this->Typology->find('first', $opt);

    }


    if ( AuthComponent::user('role')==='admin' ||AuthComponent::user('role')==='superadmin' ){ //if the user is admin or superadmin, show all on dropdown
            $items = $this->Typology->TypologyItem->find('list');
        } else {// else if the user is author, show only item created by him.
            $items = $this->Typology->TypologyItem->find('list', array('conditions' => array('TypologyItem.user_id' => AuthComponent::user('id'))));            
        }
    $typologyCategories = $this->Typology->TypologyCategory->find('list');
    $typologyConditions = $this->Typology->TypologyCondition->find('list');
    $users = $this->Typology->TypologyUser->find('list');
    $this->set(compact('items', 'typologyCategories', 'typologyConditions', 'users'));

    if ( AuthComponent::user('role')==='admin' ||AuthComponent::user('role')==='superadmin' ){
        $typologies = $this->TypologyPicture->ItemTypologyPicture->find('list');
    } else {
        $typologies = $this->TypologyPicture->ItemTypologyPicture->find('list', array('conditions' => array('ItemTypologyPicture.user_id' => AuthComponent::user('id'))));
        }
    $this->set(compact('typologies'));
}

当我尝试编辑时,它打开表单,字段被正确填充,但是当我按下保存时,在typologyPicture中插入了新行,并且没有对类型学进行任何更改。

所以我想要的是当用户按提交时只更新相应的行而不是插入新的行......

0 个答案:

没有答案