php - YII2 - 保存用户 ID 以便在其他页面上使用它
问题描述
我正在建立一个问卷调查网站。
在这个网站上,用户输入他的电子邮件以接收他的问卷调查结果。所以这个网站没有认证。
如何将用户的电子邮件存储到问卷的末尾?
这是我的User
模型:
<?php
namespace common\models;
use Yii;
use \yii\db\ActiveRecord;
// use yii\web\IdentityInterface;
/**
* This is the model class for table "user".
*
* @property int $id
* @property string $email
* @property string $name
* @property string $family
* @property string $major
* @property string $univercity
* @property int $education
* @property int $gender
* @property int $age
* @property int $income
* @property int $get_result
* @property int $created_at
*/
class User extends ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'user';
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
// TimestampBehavior::className(),
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['email', 'created_at'], 'required'],
[['education', 'gender', 'age', 'income', 'get_result', 'created_at'], 'integer'],
[['email', 'name', 'family', 'major', 'univercity'], 'string', 'max' => 255],
[['email'], 'unique'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'email' => 'Email',
'name' => 'Name',
'family' => 'Family',
'major' => 'Major',
'univercity' => 'Univercity',
'education' => 'Education',
'gender' => 'Gender',
'age' => 'Age',
'income' => 'Income',
'get_result' => 'Get Result',
'created_at' => 'Created At',
];
}
}
解决方案
有很多方法可以实现这一点,这主要取决于你的逻辑。
最简单的方法之一是使用会话。
首先将电子邮件存储在会话中:
\Yii::$app->session->set('questionnaire-email', $usersEmail);
然后,当你想使用它时:
$email = \Yii::$app->session->get('questionnaire-email');
推荐阅读
- mysql - MySQL:查找在一个日期(时间)有条目但在另一个日期(时间)没有条目的用户行
- css - 如何进行响应式列换行?
- java - 从 Java 访问 Kotlin 枚举
- ios - 在我的 iOS 应用程序中包含 HTTP(不是 HTTPS)URL 链接,我是否需要禁用应用程序传输安全性?
- c# - 将 Base64 转换为字符串会插入空格
- python - 如何使用python禁用windows上的printscreen键
- python - 如何提取 HDF5 数组矩阵的一部分及其对应的行属性
- firebase - Firebase Cloud Firestore 的成本优化模式:通过集合获取文档
- javascript - 每 3-4 个字符插入空格
- css - 设置背景混合模式和背景颜色在 HTML 电子邮件中不起作用