我正在创建一个yii应用程序,我想将数据插入到数据库中,但问题是如何插入日期和时间。在从数据库中删除日期时间列之前,它无法插入每个细节。
这是控制器类:
<?php
namespace app\controllers;
use amnah\yii2\user\models\User;
use app\models\Influence;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use amnah\yii2\user\controllers\DefaultController as SuperDefault;
class DefaultController extends SuperDefault {
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['index', 'confirm', 'resend', 'logout'],
'allow' => true,
'roles' => ['?', '@'],
],
[
'actions' => ['account', 'profile','contact', 'resend-change', 'cancel','advertiser_dashboard','influencer_dashboard', 'influenza_info', 'advertiser'],
'allow' => true,
'roles' => ['@'],
],
[
'actions' => ['login', 'register','contact','influencer_dashboard', 'advertiser_dashboard','register_advert','forgot', 'reset', 'login-email', 'login-callback'],
'allow' => true,
'roles' => ['?'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post', 'get'],
],
],
];
}
public function actionInfluenza_info()
{
$u_id = Yii::$app->user->id;
$user = User::findOne($u_id);
$influence= new Influence();
if ($influence->load(Yii::$app->request->post())) {
$influence_data = Yii::$app->request->post('Influence', []);
$influence->firstname = $influence_data['firstname'];
$influence->lastname = $influence_data['lastname'];
$influence->email = $influence_data['email'];
$influence->city = $influence_data['city'];
$influence->type = $influence_data['type'];
$influence->pic = $influence_data['pic'];
$influence->address = $influence_data['address'];
$influence->country = $influence_data['country'];
$influence->gender = $influence_data['gender'];
$influence->id = $u_id;
$influence->save();
Yii::$app->session->setFlash("Profile-success", Yii::t("user", "Profile updated"));
return $this->refresh();
} else {
return $this->render('influenza_info', [
'user' => $user,
'influence' =>$influence
]);
}
}
}
这是模型类
<?php
namespace app\models;
use Yii;
use app\controllers\Expression;
/**
* This is the model class for table "influence".
*
* @property integer $id
* @property integer $user_id
* @property string $firstname
* @property string $lastname
* @property string $email
* @property string $city
* @property string $type
* @property string $pic
* @property string $address
* @property string $country
* @property string $gender
*/
class Influence extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'influence';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'user_id', 'firstname', 'lastname', 'email', 'city', 'type', 'pic', 'address', 'country', 'gender'], 'required'],
[['id', 'user_id'], 'integer'],
[['firstname', 'lastname', 'email', 'city', 'type', 'pic', 'address', 'country', 'gender'], 'string', 'max' => 30]
];
}
public function beforeSave($insert)
{
if ($this->isNewRecord) { // new record only, otherwise time is inserted every time this record is updated
$this->created_at = new Expression('NOW()');
}
parent::afterSave($insert);
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'firstname' => 'Firstname',
'lastname' => 'Lastname',
'email' => 'Email',
'city' => 'City',
'type' => 'Type',
'pic' => 'Pic',
'address' => 'Address',
'country' => 'Country',
'gender' => 'Gender',
'created_at' => 'Created_at',
];
}
}
表sql
CREATE TABLE `influence` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`firstname` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`lastname` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`city` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`type` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`pic` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`address` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`country` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`gender` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`created` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
) ;
如何让它自动插入。如果我从我的管理员尝试它它的工作,并得到日期和时间。但是yii没有。
答案 0 :(得分:1)
为此,我从所需规则中删除了创建的属性。 然后我创建一个beforeSave()方法,并在新记录中插入Expression。
tinymce.init({
selector:'#editor',
});
Vue.directive('editor', {
twoWay: true,
params: ['body'],
bind: function () {
tinyMCE.get('editor').setContent(this.params.body);
tinyMCE.get('editor').on('change', function(e) {
alert("changed");
});
},
update: function (value) {
$(this.el).val(value).trigger('change')
},
});
var editor = new Vue({
el: '#app',
data: {
body: 'The message'
}
})
确保将Expression导入模型
<强>更新强> 更新代码谢谢@crafter
答案 1 :(得分:1)
您应该按以下方式使用Expression。请参阅此链接Class yii\db\Expression
当Expression对象嵌入在SQL语句或片段中时,它将被$ expression属性值替换,而不会有任何DB转义或引用。例如,
$expression = new Expression('NOW()');
$now = (new \yii\db\Query)->select($expression)->scalar(); // SELECT NOW();
echo $now; // prints the current date
答案 2 :(得分:0)
你有
`created` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
在您的数据库表中但似乎您将模型字段创建为字符串。
* @property string $created
检查是否存在与该字段相关的不匹配问题,并最终在保存或更改模型(或数据库)之前提供正确的转换,以便具有相同的数据类型。
无论如何,你没有创建$的规则而且没有POSTED 添加
public function rules()
{
return [
[['id', 'user_id', 'firstname', 'lastname', 'email', 'city', 'type', 'pic', 'address', 'country', 'gender'], 'required'],
[['id', 'user_id'], 'integer'],
[['firstname', 'lastname', 'email', 'city', 'type', 'pic', 'address', 'country', 'gender'], 'string', 'max' => 30]
[['created',], 'safe'],
];
}
尝试
$influence->save(false);
以这种方式保存数据并通过验证检查(仅用于调试)如果以这种方式保存模型,则尝试添加
[['created'], 'date'],
规则中的
答案 3 :(得分:0)
问题在于您混淆了beforeSave和afterSave事件。
此外,您的字段名称已创建,而不是在。
创建public function beforeSave($insert)
{
if ($this->isNewRecord) { // new record only, otherwise time is inserted every time this record is updated
$this->created = new Expression('NOW()'); // <== You were setting $this->created_at
}
parent::beforeSave($insert); // <== You were calling afterSave()
}