5

我正在使用(或尝试)来自 Jose Gonzalez 的上传插件:https ://github.com/josegonzalez/upload ,我想将我的图像记录存储在一个单独的表中。我遵循了 github 上的自述文件,但它没有指出如何在添加/编辑视图和控制器中启用此功能。这是我到目前为止所做的:

应用程序/模型/Image.php:

class Image extends AppModel {

    public $actsAs = array(
      'Upload.Upload' => array(
        'image' => array(
          'thumbnailSizes' => array('thumb' => '20x20')
        ),
      ),
    );

    public $belongsTo = array(
      'Profession' => array(
        'className' => 'Profession',
        'foreignKey' => 'foreign_key'
      )
    );

}

应用程序/模型/专业.php:

class Profession extends AppModel {

    public $hasMany = array(
      'Image' => array(
        'className' => 'Image',
        'foreignKey' => 'foreign_key',
        'conditions' => array(
          'Image.model' => 'Profession'
        )
      )
    );

}

app/View/Professions/add.php(相关部分):

$this->Form->input('Image.image', array('type' => 'file', 'label' => ''));

应用程序/控制器/ProfessionsController.php:

public function add() {
  if ($this->request->is('post')) {
    if ($this->Profession->saveAll($this->request->data)) {
      $this->redirect(array('action' => 'index'));
    } else {
      $this->Session->setFlash(__('Error'));
    }
  }
}

文件没有上传,我images表中的记录如下:

id | model | foreign_key | name     | image | dir  | type      | size | active
---+-------+-------------+----------+-------+------+-----------+------+--------
 1 |       |           1 | test.png |       | NULL | image/png |  814 |      1

模型、图像和目录不应为空/null。

-function的调试输出debug($this->request->data)add()

array(
    'Profession' => array(
        [...]
    ),
    'Image' => array(
        'image' => array(
            'name' => 'test.png',
            'type' => 'image/png',
            'tmp_name' => '/Applications/MAMP/tmp/php/phpTMHMF9',
            'error' => (int) 0,
            'size' => (int) 1473
        )
    )
)

问题是,正如我上面所说,上传不起作用并且图像记录不完整。我希望这是可以理解的,我真的不想将图像信息存储在与模型相同的表中。

4

2 回答 2

2

请原谅我迟到的答案,但我遇到了同样的问题,我花了一段时间(大约 4 小时)才弄清楚,所以这是我的解决方案,以供将来参考。

在我的例子中,上传(附件)模型类看起来像这样(app\Model\Upload.php)

class Upload extends AppModel {
    public $actsAs = array(
        'Upload.Upload' => array('upload')
    );

    public $belongsTo = array(
        'Product' => array(
            'className' => 'Product',
            'foreignKey' => 'foreign_key'
        )
    );
}

我的产品看起来像这样(app\Model\Product.php)

class Product extends AppModel {
    public $hasMany = array(
        'Image' => array(
            'className' => 'Upload',
            'foreignKey' => 'foreign_key',
            'conditions' => array(
                'Image.model' => 'Product'
            )
        )
    );

    // rest of the code
}

我的数据库方案:

id | model | foreign_key | name     | upload | dir  | type       | size | active
---+-------+-------------+----------+--------+------+------------+------+--------
 1 |       |           1 | test.jpg |        | NULL | image/jpeg | 5820 |      1

现在 2 解决方案在产品的添加视图中 (appView\Products\add.ctp)

echo $this->Form->create('Product', array('type' => 'file'));
echo $this->Form->input('name');
echo $this->Form->input('Image.upload.upload', array('type' => 'file'));
echo $this->Form->input('Image.upload.model', array('type' => 'hidden', 'value' => 'Product'));
echo $this->Form->end('Save Product');

您需要通过向表单添加隐藏字段来手动添加模型,并且您需要将数据库字段添加到文件字段。在您的情况下,它将变为:

this->Form->input('Image.image.image', array('type' => 'file', 'label' => ''));
于 2013-02-13T21:26:32.430 回答
0

试试这个:

看法

echo $this->Form>input('Attachment.1.model',array('type'=>'hidden','value'=>'Sludge'));

那现在对我有用。

于 2012-09-10T00:17:12.243 回答