使用magento eav数据模型为用户提供图片上传功能的实践

一,在megento表中,增加一个存储上传图片路径的属性,

给magento的customer实体类型增加一个audit_file_path属性,因为要customer使用的是EAV模型,得操作几个关联的数据表,方便使用magento模型的方法,

1,为表eav_attribute添加一个条记录,即增加一个audit_file_path属性对应于attribute_code字段,其中backend_type为varchar类型,在eav里对应的属性值会被存储到customer_entity_varchar表当中,SQL语句如下:

1 INSERT INTO yo_eav_attribute(entity_type_id,attribute_code,attribute_model,backend_model,backend_type,backend_table,frontend_model,frontend_input,frontend_label,frontend_class,source_model,is_required,is_user_defined,default_value,is_unique,note) VALUE(1,'audit_file_path',NULL,NULL,'varchar',NULL,NULL,'text','Audit file path',NULL,NULL,0,0,0,0,NULL)

2,属性添加成功后,会生成attribute_id:157,其它字段值可参考eav_entity_attribure表里面,entity_type_id的实体类型为customer的一些记录设置,SQL语句如下:

1 INSERT INTO yo_eav_entity_attribute(entity_type_id,attribute_set_id,attribute_group_id,attribute_id,sort_order) VALUE(1,1,1,157,110)

3,接着为customer_eav_attribute表(用户实体),增加属性id为157的一条记录,SQL语句如下:

1 INSERT INTO yo_customer_eav_attribute VALUE(157,1,NULL,0,NULL,1,0,NULL)

二,在megento系统中的app\design\frontend\Packagename\yourstore\template目录中的找到对应的模板.phtml,并添加文件上传的html结构,代码如下:

1                     <div class="fileContainer">
2                         <div class="audit_file_wrp">
3                             <input type="file" name="audit_file[]" class="audit_file" />
4                             <span class="auditRemove">remove</span>
5                         </div>
6                     </div>
7                     <div><span class="addAuditFile">add file</span></div>

   在对应js文件中添加文件按钮的添加与移除按钮,js代码如下:

 1  jQuery(document).ready(function(){
 2         //增加上传文件按钮
 3         jQuery('.addAuditFile').click(function(){
 4             var addHtml='<div class="audit_file_wrp"> <input type="file" name="audit_file[]" class="audit_file" /> <span class="auditRemove">remove</span></div>';
 5             jQuery(addHtml).appendTo('.fileContainer');
 6         });
 7         //删除上传文件按钮,至少保留一个
 8         jQuery('.fileContainer').on('click', '.auditRemove', function() {
 9             if (jQuery('.auditRemove').length > 1) {
10                 jQuery(this).parents('.audit_file_wrp').remove();
11             } else {
12                 alert('at least one image need to be selected');
13             }
14         });
15     });

三,在magento系统中,app\code\local\Packagename\yourmodule\controllers目录下找到自己对应的anction方法处理文件的上传,与存储并存到数据库,方便以后查阅文件,这里是以图形式上传,代码如下:

 1       /**
 2          * 获取当前用户对象
 3          */
 4         $customer = Mage::getSingleton ( "customer/session" )->getCustomer ();
 5         /**
 6          * 获取当前对象ID
 7          */
 8         $customerId = $customer->getId ();
 9         /**
10          * 加载当前用户对象的模型
11          */
12         $model = Mage::getModel ( 'customer/customer' )->load ( $customerId );
13 
14         /**
15          * 设置存储图片的默认路径
16          */
17         $basedir = Mage::getBaseDir ( 'media' );
18         /**
19          * 遍历上传的文件,并存储到对应路径
20          */
21         $imagePath=array();
22         foreach($_FILES ["audit_file"]["name"] as $key=>$image){
23             if (isset ( $_FILES ["audit_file"] ['name'][$key] ) && (file_exists ( $_FILES ["audit_file"] ['tmp_name'][$key] ))) {
24                 try {
25 
26                     $uploader = new Varien_File_Uploader ( array(
27                         'name'=> $_FILES ["audit_file"] ['name'][$key],
28                         'type'=> $_FILES ["audit_file"] ['type'][$key],
29                         'tmp_name'=> $_FILES ["audit_file"] ['tmp_name'][$key],
30                         'error'=> $_FILES ["audit_file"] ['error'][$key],
31                         'size'=> $_FILES ["audit_file"] ['size'][$key]
32                     ) );
33                     /**
34                      * 定义可上传的文件格式
35                      */
36                     $uploader->setAllowedExtensions ( array (
37                         'jpg',
38                         'jpeg',
39                         'gif',
40                         'png'
41                     ) );
42                     /**
43                      * 定义存储的文件名是否可变
44                      */
45                     $uploader->setAllowRenameFiles ( false );
46                     $uploader->setFilesDispersion ( false );
47                     /**
48                      * 定义具体路径
49                      */
50                     $path = $basedir . DS . 'sellerimage'.DS.'audit';
51                     /**
52                      * 保存文件到具体路径,并按照用户id设置用户名
53                      */
54 
55                     $RenameArr=explode('.',$image);
56                     $RenameArr[0]="audit_file_".$customerId."_".$key;
57                     $imagePath[$key]=implode('.',$RenameArr);
58                     $uploader->save ( $path, $imagePath[$key]);
59                     
60                 } catch ( Exception $e ) {
61                     /**
62                      * 显示上传出错的信息
63                      */
64                     Mage::getSingleton ( 'core/session' )->addError ( $this->__ ( $e->getMessage () ) );
65                 }
66 
67             }
68         }
69         /**
70          * 存储文件名到数据库
71          */
72         $model->setAuditfilepath(implode(',',$imagePath));
73         $model->save();

总结:在整个操作过程中,难点在与magento的EVA数据模型的理解,及在内置的eav模型的使用,里面使用Varien_File_Uploader对象处理图片的存储,简单易行


posted @ 2016-03-21 19:36  lokou  阅读(562)  评论(0编辑  收藏  举报