本文整理汇总了PHP中app\models\Product::findOne方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::findOne方法的具体用法?PHP Product::findOne怎么用?PHP Product::findOne使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\models\Product
的用法示例。
在下文中一共展示了Product::findOne方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getProduct
/**
* @return Product
*/
public function getProduct()
{
if (is_null($this->_product)) {
$this->_product = Product::findOne($this->product_id);
}
return $this->_product;
}
示例2: actionList
/**
* @param $node
* @return string
* @throws NotFoundHttpException
*/
public function actionList($node)
{
/** @var Order $order */
$order = Order::findOne($node);
if (!$order) {
throw new NotFoundHttpException();
}
$newProduct = new AddProductForm();
if ($newProduct->load(\Yii::$app->request->post()) && $newProduct->validate()) {
/** @var OrderData $existingLine */
$existingLine = OrderData::find()->where(['product_id' => $newProduct->product_id, 'order_id' => $order->id])->one();
if ($existingLine) {
$existingLine->quantity += $newProduct->quantity;
$existingLine->save();
} else {
/** @var Product $product */
$product = Product::findOne($newProduct->product_id);
if ($product) {
$line = new OrderData(['product_id' => $product->id, 'quantity' => $newProduct->quantity, 'price' => $product->price]);
$order->link('orderLines', $line);
}
}
}
return $this->render('list', ['order' => $order, 'newProduct' => $newProduct]);
}
示例3: process
public function process()
{
if ($this->validate()) {
if (!is_null($this->importFile)) {
$imported = 0;
$errors = 0;
if (($handle = fopen($this->importFile->tempName, "r")) !== FALSE) {
$keys = fgetcsv($handle);
while (($row = fgetcsv($handle)) !== FALSE) {
$data = [];
for ($c = 0; $c < count($row); $c++) {
$data[$keys[$c]] = $row[$c];
}
/** @var Product $product */
$product = array_key_exists('id', $data) && $data['id'] ? Product::findOne($data['id']) : new Product();
if ($product->from_array($data)) {
$imported++;
} else {
$errors++;
}
}
fclose($handle);
}
return "Imported: {$imported}, Errors: {$errors}";
} else {
return "File not found";
}
} else {
return "Invalid File";
}
}
示例4: actionAjaxAdd
public function actionAjaxAdd()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$request = Yii::$app->request;
$id = $request->get('id');
$color = $request->get('color');
$size = $request->get('size');
$number = $request->get('num');
$model = Product::findOne($id);
if (!Yii::$app->session->isActive) {
Yii::$app->session->open();
}
$cart = new Cart();
$cart->session_id = Yii::$app->session->id;
$cart->user_id = Yii::$app->user->isGuest ? 0 : Yii::$app->user->id;
$cart->product_id = $id;
$cart->name = $model->name;
$cart->color = $color;
$cart->size = $size;
$cart->number = $number;
$cart->price = $model->price;
if ($cart->save()) {
return ['status' => 1, 'productId' => $id, 'size' => $size, 'color' => $color];
} else {
return ['status' => -2, 'productId' => $id, 'size' => $size, 'color' => $color];
}
}
示例5: findModel
/**
* Finds the Product model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Product the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Product::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('Продукт с номером ID ' . $id . ' не обнаружен.');
}
}
示例6: findModel
/**
* Finds the Product model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Product the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Product::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
示例7: actionUpdate
public function actionUpdate($itemId, $quantity)
{
$product = Product::findOne($itemId);
if ($product) {
\Yii::$app->cart->update($product, $quantity);
$this->redirect(['cart/list']);
}
}
示例8: actionUpdate
public function actionUpdate($id, $quantity)
{
$product = Product::findOne($id);
if ($product) {
$this->_cart->update($product, $quantity);
$this->redirect(['cart/list']);
}
}
示例9: actionDelete
/**
* @param $id
* @return \yii\web\Response
* @throws NotFoundHttpException
* @throws \Exception
*/
public function actionDelete($id)
{
/** @var Product $model */
$model = Product::findOne($id);
if (!$model) {
throw new NotFoundHttpException();
}
$model->delete();
return $this->redirect(['list', 'node' => $model->category_id]);
}
示例10: actionEditProduct
/**
* Ajaxified version of ProductController actionEdit
* @return partial
*/
public function actionEditProduct($id)
{
$model = Product::findOne($id);
if (Yii::$app->request->isAjax || Yii::$app->request->isPjax) {
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model = new Product();
}
return $this->renderPartial('_product_edit', ['model' => $model]);
}
}
示例11: findModel
public function findModel($id)
{
$search = new Product();
$search->scenario = Product::SCENARIO_READ;
if (($model = $search->findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
示例12: actionAddToCart
public function actionAddToCart($id)
{
$cart = new ShoppingCart();
$model = Product::findOne($id);
if ($model) {
$cart->put($model, 1);
return $this->redirect(['cart-view']);
}
throw new NotFoundHttpException();
}
示例13: actionProduct
public function actionProduct()
{
$request = Yii::$app->request;
$product = null;
if ($code = $request->get('code')) {
$product = Product::find()->where(['url_code' => $code])->one();
}
if (is_null($product) && ($id = $request->get('id'))) {
$product = Product::findOne($id);
}
if (!is_null($product)) {
return $this->render('product', ['model' => $product]);
} else {
return $this->goHome();
}
}
示例14: exportRestaurantMenuToCsv
public function exportRestaurantMenuToCsv($id)
{
$restaurantName = Restaurant::findOne(['restaurant_id' => $id])->name;
Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/web/upload/';
$string = "name, description, price \n";
$product = null;
foreach (Warehouse::find()->where(['restaurant_id' => $id])->batch(1) as $warehouse) {
try {
$product = Product::findOne(['product_id' => $warehouse[0]->product_id]);
$string .= '"' . $product->name . '","' . $product->description . '","' . $warehouse[0]->price . '"';
$string .= "\n";
} catch (Exception $e) {
}
}
$path = $this->saveStringToCsvFile($string, $restaurantName);
$this->giveUserFile($path);
}
示例15: validateProduct
/**
* Validates product
*/
public function validateProduct()
{
if (!Product::findOne($this->product_id)) {
$this->addError('product_id', \Yii::t('app', 'Product does not exist.'));
}
}