从一个控制器编辑2个控制器(正确)

时间:2014-03-25 12:11:22

标签: cakephp cakephp-2.3 cakephp-appmodel

我有小问题,所以我需要一些帮助。也许它很容易,但我只需要推动......所以我正在使用一个控制器

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->save($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);

        //$options1 = array('conditions' => array('Typology.id' => $id));
        $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'));
}

因此,您从联系人控制器中看到我想要访问我要编辑的联系人及其存储在 contact_picture 表中的图片。联系人本身就像一个图标或一个头像,并在联系人图片存储在画廊。所以这里的问题是我得到了它应该的所有数据,但是联系人的图像(化身或图标)会显示出来,路径被正确地重新检索但它只是显示图像。

所以我要问的是,如果有另一种方式或简单的方法,甚至更好的方式来做到这一点,我真的会适应它......真的。

提前致谢!

编辑:查看部分:

<?php echo $this->Form->create('TypologyPicture', array('type'=>'file')); ?>
        <legend><?php echo __('Edit Typology Picture'); ?></legend>
    <?php
    $dir = "/img/uploads/typology/images/"; 
        echo $this->Form->input('id'); ?>

<?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 of the update -->
<?php echo $this->Form->input('pic_path', array('label'=>'Picture','type'=>'file'));
        echo $this->Form->input('hiddenimage', array('type'=>'hidden','value'=> $this->Form->value('pic_path') )); 
        $Image = $this->Form->value( 'pic_path');       
            if(empty($Image) || $Image==NULL)
                    {$Image = "/img/uploads/noimg.jpg";}
                else {$Image = $dir . $Image;   }
        echo $this->Html->image($Image,array('align'=>'absbottom','style'=>'max-height:100px'));
    ?>
<?php echo $this->Form->end(__('Submit')); ?>

因此,当我对图像进行回声时,它会正确显示...如果我像普通编辑一样删除类型模型部分,它会显示正常......

1 个答案:

答案 0 :(得分:0)

尝试从$ dir和$ Image中删除img部分;

$dir = "/img/uploads/typology/images/"; 

应该是

$dir = "uploads/typology/images/"; 

{$Image = "/img/uploads/noimg.jpg";}

应该是

{$Image = "uploads/noimg.jpg";}
相关问题