简体   繁体   中英

Yii: Update A (Model's) Table Column In A Static Page?

I'm using a static page to perform some actions and am wondering how I can update certain data from a model. For this example I want to process a virtual payment. The logged in user has a certain amount of credits ('credits' column in tbl_profiles from yii-user extension), the code checks the price of the product and subtracts it from the user's credits:

$productid = $_GET['product'];
$user = Yii::app()->getModule('user')->user()->profile;
$userid = Yii::app()->user->id;
$credits = Yii::app()->getModule('user')->user()->profile->credits;
$product = Product::model()->findByPk($productid);
$price = Product::model()->findByPk($productid)->price_total;

if($credits >= $product){
$newcredits = ($credits - $price);
//Update 'credits' for logged in user
}else{
//Payment Failed
echo "Not enough credits";
}

How do I update the credits for the logged in user in this example?

I dont know the yii-user extension but it seems, that

Yii::app()->getModule('user')->user()->profile;

returns the profile model, which probably is an Active Record

the following code could change the credits

Yii::app()->getModule('user')->user()->profile->credits = $newcredits;
Yii::app()->getModule('user')->user()->profile->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM