在写进销存的时候,在入库的时候需要把商品加到仓库中。代码如下
public function actionCreate()
{
$model = new Goods();
$model->sku = strtoupper(uniqid());
$model->datetime = time();
$model->admin_id = Yii::$app->user->id;
if ($model->load(Yii::$app->request->post())) {
$cost = $model->cost_price;
$number = $model->numbers;
$model->total = $cost * $number;
$models=ArrayHelper::toArray($model);
if ($model) {
$model->save();
$models->add($models);
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
这是添加到入库表的,$models->add($models);是添加到仓库表模型层的方法
public function add($data)
{
foreach ($data as $k=>$v)
{
$asku = Warehouse::find()->where(['sku'=>$v['sku']])->one();
$askuCount = Warehouse::find()->where(['sku'=>$v['sku']])->count();
}
if (!$askuCount)
{
$Warehouse=new Warehouse;
$Warehouse->datetime=time();
$Warehouse->cid=$data['cid'];
$Warehouse->name=$data['name'];
$Warehouse->sku=$data['sku'];
$Warehouse->counts=$data['numbers'];
$Warehouse->price=$data['price'];
$Warehouse->flag=1;
$Warehouse->save();
}else{
$asku->counts += $data['numbers'];
$asku->timeLastOp=time();
$asku->save();
}
}
可以打印出数据,但是不知道为什么添加之后不能再Warehouse这个表中添加数据,有人知道吗?刚用框架两天,不是很懂。