0

我在文章和图像之间有一个多态附件模型(附件模型的别名)ala https://github.com/josegonzalez/upload。文章有很多图片。图片是附件。

当通过文章表单的保存按钮提交表单时,所有内容都会正确保存,包括关联的模型。

但是,我已经设置了附加图像的动态显示,以便用户可以在保存文章之前选择使用哪一个来预览文章。动态图像显示有效(通过删除 phpMyAdmin 中的行证明),但我无法弄清楚如何在不按提交按钮的情况下上传和保存已选择的关联图像。我想在选择后立即上传图片(或者按下不提交文章的按钮,也是!

这是相关的表单部分,以及最终导致显示所有关联图像的脚本。

<div id='imagesControl'>
<?php
//TODO gotta make this happen live
echo $this->Form->input('Image.0.attachment', array('type' => 'file', 'label' => 'Image'));
echo $this->Form->input('Image.0.model', array('type' => 'hidden', 'value' => 'Content'));
echo $this->Form->input('Image.0.foreign_key', array('type' => 'hidden', 'value' => $id));
echo $this->Form->input('Image.0.id', array('type' => 'hidden')); //Thought this would help
?>
<div id='attachedImages'>
</div> </div>

<script>
$(document).ready(function() {
$('#imagesControl').change(function() {window.alert("sometext");
    $("#attachedImages").load('../imageDisplay/<?php echo $id ?> #attachedImages', 
        {id: $(this).attr('id')});
    })
.change();
});
</script> 

值得注意的是,在渲染的视图文件“../imageDisplay”中,$this->data 包含通过发送的 div id$(this).attr('id').我无法在任何地方找到包含我在表单保存之前在输入部分中选择的文件的变量(我的文件我正在尝试上传和保存)。它显然在那里,因为它确实使用$this->Article->saveAll()自动魔法保存。

但是,我什至无法开始猜测我可以在何处/何时/如何上传图像并在不提交文章的情况下向包含新图像数据的附件表添加一行。我被困了好几天了。

4

0 回答 0