需求分析:
1.属性分为两种
2.类型管理【把属性分类】
3.把属性和商品关联起来
4.商品库存量管理【和这个商品所拥有的属性相关】

总结:在添加的功能:
1.类型管理
2.属性管理
3.商品属性管理
4.商品库存量管理

 

实际操作:

1.建表
类型表

/************属性相关表***************/
drop table if exists p40_type;
create table p40_type
(
id mediumint unsigned not null auto_increment comment 'id',
type_name varchar(30) not null comment '类型名称',
primary key (id)
)engine =InnoDB default charset=utf8 comment '类型';

属性表
drop table if exists p40_attribute;
create table p40_attribute(
id mediumint unsigned not null auto_increment comment 'id',
attr_name varchar(30) not null comment '属性名称',
attr_type enum('唯一','可选') not null comment '属性类型',
attr_option_values varchar(300) not null default '' comment '属性可选值',
type_id mediumint unsigned not null comment '所属类型Id',
primary key(id),
key type_id(type_id)
)engine = InnoDB default charset=utf8 comment'属性表';

商品属性表
修改商品表添加一个字段保存商品所属的类型
drop table if exists p40_goods;
create table p40_goods
(
id mediumint unsigned not null auto_increment comment 'Id',
goods_name varchar(150) not null comment '商品名称',
market_price decimal(10,2) not null comment '市场价格',
shop_price decimal(10,2) not null comment '本店价格',
goods_desc longtext comment '商品描述',
is_on_sale enum('是','否') not null default '是' comment '是否上架',
is_delete enum('是','否') not null default '否' comment '是否放到回收站',
addtime datetime not null comment '添加时间',
logo varchar(150) not null default '' comment '原图',
sm_logo varchar(150) not null default '' comment '小图',
mid_logo varchar(150) not null default '' comment '中图',
big_logo varchar(150) not null default '' comment '大图',
mbig_logo varchar(150) not null default '' comment '更大图',
brand_id mediumint unsigned not null default '0' comment '品牌id',
cat_id mediumint unsigned not null default '0' comment '主分类id',


type_id mediumint unsigned not null default '0' comment '类型id',


primary key (id),
key shop_price(shop_price),
key addtime(addtime),
key brand_id(brand_id),
key cat_id(cat_id),
key is_on_sale(is_on_sale)
)engine=InnoDB default charset=utf8 comment '商品';

alter table p40_goods add type_id mediumint unsigned not null default '0' comment '类型id'
这个字段用来保存以下表单中的下拉框中的值

 

商品属性表
drop table if exists p40_goods_attr;
create table p40_goods_attr(
id mediumint unsigned not null auto_increment comment 'id',
attr_value varchar(150) not null default '' comment '属性值',
attr_id mediumint unsigned not null comment '属性ID',
goods_id mediumint unsigned not null comment '商品Id',
primary key(id),
key attr_id(attr_id),
key goods_id(goods_id)
)engine = InnoDB default charset=utf8 comment '商品属性';

这个表用来保存以下表单中的数据
商品库存量

drop table if exists p40_goods_number;
create table p40_goods_number(
goods_id mediumint unsigned not null comment '商品id',
goods_number mediumint unsigned not null default '0' comment '
库存量',
goods_attr_id varchar(150) not null comment '商品属性表的ID,如果有多个,就用程序拼成字符串存到这个字段中',
key goods_id(goods_id)
) engine =InnoDB default charset=utf8 comment'库存量';
实际数据演示一下:
如果一件商品有这些属性值:
数据是存在商品属性表
如果这件商品根据属性设置了这四个库存量:
库存量的数据是保存在商品库存量表中的

类型与属性管理
主要的思路:
1. 使用GII分别生成两个表的代码
2. 在类型列表中添加一个属性列表的按钮,点击之后跳到属性列表

实际操作:
1.使用GII分别生成两个表的代码
a)生成类型表
'validate' => "
array('type_name', 'require', '类型名称不能为空!', 1, 'regex', 3),
array('type_name', '1,30', '类型名称的值最长不能超过 30 个字符!', 1, 'length', 3),

array('type_name', '', '类型名称已存在!', 1, 'unique', 3),

",
不需要搜索,因为类型的数据量不多
使用配置文件 生成代码
在左侧添加类型管理的按钮
<ul>
<li class="menu-item"><a href="<?php echo U('Goods/lst'); ?>" target="main-frame">商品列表</a></li>
<li class="menu-item"><a href="<?php echo U('Brand/lst'); ?>" target="main-frame">商品品牌</a></li>
<li class="menu-item"><a href="<?php echo U('Category/lst'); ?>" target="main-frame">分类列表</a></li>
<li class="menu-item"><a href="<?php echo U('Type/lst'); ?>" target="main-frame">类型列表</a></li>
</ul>
效果:

3.使用GII生成属性表
A.先生成配置文件
B. 修改配置文件
/**************** 搜索字段的配置 **********************/
'search' => array(
array('attr_name', 'normal', '', 'like', '属性名称'),
array('attr_type', 'in', '唯一-唯一,可选-可选', '', '属性类型'),
array('attr_option_values', 'normal', '', 'like', '属性可选值'),
array('type_id', 'normal', '', 'eq', '所属类型Id'),
),
4.在类型 lst 列表中为每个类型添加一个属性列表的按钮,点击跳到 生成的属性中
<tr>
<th >类型名称</th>
<th width="120">操作</th>
</tr>
<?php foreach ($data as $k => $v): ?>
<tr class="tron">
<td><?php echo $v['type_name']; ?></td>
<td align="center">
<a href=" <?php echo U('Attribute/lst?type_id='.$v['id']); ?>" title="属性列表">属性列表</a>
<a href="<?php echo U('edit?id='.$v['id'].'&p='.I('get.p')); ?>" title="编辑">编辑</a> |
<a href="<?php echo U('delete?id='.$v['id'].'&p='.I('get.p')); ?>" onclick="return confirm('确定要删除吗?');" title="移除">移除</a>
</td>
</tr>


5.修改添加属性的表单,把类型ID制作成一个下拉框,这样选择类型比较方便
<tr>
<td class="label">属性类型:</td>
<td>
<?php buildSelect('Type','type_id','id','type_name') ;?>
</td>
</tr>

6.修改添加属性的按钮,点击时要把类型ID继续传到 表单中
修改属性控制器中按钮的变量

public function lst()
{
$model = D('Attribute');
$data = $model->search();
$this->assign(array(
'data' => $data['data'],
'page' => $data['page'],
));

// 设置页面中的信息
$this->assign(array(
'_page_title' => '属性表列表',
'_page_btn_name' => '添加属性',
'_page_btn_link' => U('add?type_id='.I('get.type_id')),
));
$this->display();
}

 

  1. 根据这个type_id设置下拉框的选中状态

 

<tr>
<td class="label">属性类型:</td>
<td>
<?php buildSelect('Type','type_id','id','type_name',I('get.type_id')) ;?>
</td>
</tr>

在添加属性时把,号统使用英文逗号,否则以后写程序会出错

 

// 添加前
protected function _before_insert(&$data, $option)
{
$data['attr_option_values']=str_replac(',',',',$data['attr_option_values']);
}

 

 

 

  1. 添加成功之后跳转到列表页时type_id又没传过来,那么就又不知道当前是什么类型了

http://localhost/tp/tp40/index.php/Admin/Attribute/lst.html

 

public function add()
{
if(IS_POST)
{
$model = D('Attribute');
if($model->create(I('post.'), 1))
{
if($id = $model->add())
{
$this->success('添加成功!', U('lst?p='.I('get.p').'&type_id='.I('get.type_id')));
exit;
}
}
$this->error($model->getError());
}

总结:在属性中的所有操作都要把type_id一直传!!!

  1. 把属性列表中的搜索里的类型制作成下拉框

<p>
所属类型:
<?php buildSelect('Type', 'type_id', 'id', 'type_name', I('get.type_id')); ?>

</p>
<p><input type="submit" value=" 搜索 " class="button" /></p>

 

同理:把修改也改一下即可!!

  

  1. 当删除一个类型时,把这个类型下所有的属性也一起删除掉

思路:在类型删除之前再删除属性

 

 

// 删除前
protected function _before_delete($option)
{
$attrModel=D('Attribute');
$attrModel->where(array(
'type_id'=array('eq',$option['where']['id']),
))->delete();
}

到此类型和属性管理完成!!!