2015-12-30 60 views
3

我正在創建yii應用程序,我想將數據插入數據庫,但問題是如何插入日期和時間。在我從數據庫中刪除日期時間列之前,它無法插入每個細節。Yii2在sql中插入日期和時間

這是控制器類:

<?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不同。

回答

1

爲此,我從所需的規則中刪除創建的屬性。 然後我創建一個beforeSave()方法,並在新記錄中插入它與表達式。

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()'); 
} 
parent::beforeSave($insert); 
} 

確保導入表達到模型

更新 更新的代碼感謝@crafter

+0

@arinze有什麼錯誤? – Jonnny

+0

沒有錯誤什麼都沒有插入,除非我刪除數據庫中的時間colomn我編輯我的模型類在問題中看看它不是我做得很好 – arinze

+0

試試這個:'if(!model-> save())print_r( $ model-> getErrors())'這將告訴你哪個規則失敗 – Jonnny

0

你有

`created` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP 

分貝表,但似乎你有模型字段創建爲一個字符串。

* @property string $created 

檢查是否有相關的部份領域不匹配問題,並最終前提供合適的轉換保存或以具有相同的數據類型改變模型(或DB)..

反正你不具備創建$規則和不貼 添加

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); 

這樣你保存,如果withot驗證檢查的數據(使用此僅用於調試)以這種方式保存模型的嘗試addding

 [['created'], 'date'], 
在規則

+0

PHP中沒有日期數據類型。 (可讀的)日期在PHP中將以字符串形式存在。 – crafter

+0

我該怎麼做 – arinze

+0

我這個探頭與您在模型規則中沒有規則或安全聲明的事實有關。我已更新回答 – scaisEdge

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() 
} 
1

您應該按以下方式使用Expression。請參閱此鏈接Class yii\db\Expression

當Expression對象嵌入到SQL語句或片段中時,它將被替換爲$ expression屬性值,而不用任何數據庫轉義或引用。例如,

$expression = new Expression('NOW()'); 
$now = (new \yii\db\Query)->select($expression)->scalar(); // SELECT NOW(); 
echo $now; // prints the current date