Commit 7ddc5cbd authored by xiabin's avatar xiabin

编写假数据接口

parent 6c3b5dca
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace common\models; namespace common\models;
use Yii; use Yii;
use yii\behaviors\TimestampBehavior;
use yii\redis\ActiveRecord;
/** /**
* This is the model class for table "orders". * This is the model class for table "orders".
...@@ -27,13 +29,30 @@ class Orders extends \yii\db\ActiveRecord ...@@ -27,13 +29,30 @@ class Orders extends \yii\db\ActiveRecord
return 'orders'; return 'orders';
} }
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
// if you're using datetime instead of UNIX timestamp:
// 'value' => new Expression('NOW()'),
],];
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function rules() public function rules()
{ {
return [ return [
[['user_id', 'course_id', 'number', 'credit', 'pay_order', 'created_at', 'updated_at'], 'required'], [['user_id', 'course_id', 'number', 'credit', 'pay_order'], 'required'],
[['user_id', 'course_id', 'status', 'created_at', 'updated_at'], 'integer'], [['user_id', 'course_id', 'status', 'created_at', 'updated_at'], 'integer'],
[['number', 'credit'], 'string', 'max' => 32], [['number', 'credit'], 'string', 'max' => 32],
[['pay_order'], 'string', 'max' => 64], [['pay_order'], 'string', 'max' => 64],
...@@ -57,4 +76,21 @@ class Orders extends \yii\db\ActiveRecord ...@@ -57,4 +76,21 @@ class Orders extends \yii\db\ActiveRecord
'updated_at' => 'Updated At', 'updated_at' => 'Updated At',
]; ];
} }
/**
* 获取购买记录
* @param $userId
* @return array|\yii\db\ActiveRecord[]
*/
public static function getPurchasedCourseByUserId($userId)
{
$tableA = self::tableName();
$tableB = Course::tableName();
return self::find()->select("$tableA.course_id,$tableA.created_at as purchased_time,$tableB.name,$tableB.avatar,$tableB.price,$tableB.free_time,$tableB.intro,$tableB.preview_pic,$tableB.created_at")
->where("$tableA.user_id=:user_id and $tableA.status = 1", [
':user_id' => $userId
])->join('left join ', $tableB, "$tableA.course_id = $tableB.id")->asArray()->all();
}
} }
...@@ -27,6 +27,7 @@ class m160927_122231_orders_init extends Migration ...@@ -27,6 +27,7 @@ class m160927_122231_orders_init extends Migration
], $tableOptions); ], $tableOptions);
$this->addCommentOnTable($this->table, '用户订单表'); $this->addCommentOnTable($this->table, '用户订单表');
$this->createIndex('index-user_id', $this->table, 'user_id'); $this->createIndex('index-user_id', $this->table, 'user_id');
$this->createIndex('index-user_id-status', $this->table, ['user_id', 'status']);
} }
......
...@@ -13,6 +13,7 @@ use common\libs\Message; ...@@ -13,6 +13,7 @@ use common\libs\Message;
use common\libs\Test; use common\libs\Test;
use common\models\BrowseRecord; use common\models\BrowseRecord;
use common\models\Favorite; use common\models\Favorite;
use common\models\Orders;
use common\models\User; use common\models\User;
use yii\filters\AccessControl; use yii\filters\AccessControl;
use yii\web\Controller; use yii\web\Controller;
...@@ -58,19 +59,33 @@ class UserCenterController extends Controller ...@@ -58,19 +59,33 @@ class UserCenterController extends Controller
return $this->render('info'); return $this->render('info');
} }
/**
* 购买记录
* @return string
*/
public function actionPurchasedCourse() public function actionPurchasedCourse()
{ {
$purchasedCourse = Orders::getPurchasedCourseByUserId(Yii::$app->user->getId());
var_dump($purchasedCourse);
return $this->render('purchased_course'); return $this->render('purchased_course');
} }
/**
* 收藏记录
* @return string
*/
public function actionFavorite() public function actionFavorite()
{ {
$favorite = Favorite::getByUserId(Yii::$app->user->getId()); $favorite = Favorite::getByUserId(Yii::$app->user->getId());
var_dump($favorite); var_dump($favorite);
// return $this->render('favorite'); return $this->render('favorite');
} }
/**
* 浏览记录
* @return string
*/
public function actionBrowseRecord() public function actionBrowseRecord()
{ {
$browseRecord = BrowseRecord::getByUserId(Yii::$app->user->getId()); $browseRecord = BrowseRecord::getByUserId(Yii::$app->user->getId());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment