public static function editField($id, $value, $productId) { $stock = Stock::findOne($id); if ($productAmount = $stock->getProducts()->where(['product_id' => $productId, 'stock_id' => $id])->one()) { $productAmount->amount = $value; $productAmount->save(); } else { $productAmount = new StockToProduct(); $productAmount->amount = $value; $productAmount->product_id = $productId; $productAmount->stock_id = $id; $productAmount->save(); } }
public function getStock($id) { return StockModel::findOne($id); }
/** * Finds the Stock model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Stock the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Stock::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }