2016-12-28 27 views
0

我試過了所有我能想到的,但無法獲得模型 - > save()方法來實際更新數據庫中的某些列。我的用戶模型看起來像這樣(使用爾康出納):Phalcon save()默默無聞

<?php 
namespace Vokuro\Models; 

use Phalcon\Mvc\Model; 
use Phalcon\Validation; 
use Phalcon\Cashier\Billable; 
use Phalcon\Validation\Validator\Uniqueness; 

/** 
* Vokuro\Models\Users 
* All the users registered in the application 
*/ 
class Users extends Model 
{ 

use Billable; 

/** 
* 
* @var integer 
*/ 
public $id; 

/** 
* 
* @var string 
*/ 
public $name; 

/** 
* 
* @var string 
*/ 
public $email; 

/** 
* 
* @var string 
*/ 
public $password; 

/** 
* 
* @var string 
*/ 
public $mustChangePassword; 

/** 
* 
* @var string 
*/ 
public $profilesId; 

/** 
* 
* @var string 
*/ 
public $banned; 

/** 
* 
* @var string 
*/ 
public $suspended; 

/** 
* 
* @var string 
*/ 
public $active; 

/** 
* 
* @var string 
*/ 
public $stripe_id; 

/** 
* 
* @var string 
*/ 
public $card_brand; 

/** 
* 
* @var string 
*/ 
public $card_last_four; 

/** 
* 
* @var string 
*/ 
public $trial_ends_at; 

/** 
* Before create the user assign a password 
*/ 
public function beforeValidationOnCreate() 
{ 
    if (empty($this->password)) { 

     // Generate a plain temporary password 
     $tempPassword = preg_replace('/[^a-zA-Z0-9]/', '', base64_encode(openssl_random_pseudo_bytes(12))); 

     // The user must change its password in first login 
     $this->mustChangePassword = 'Y'; 

     // Use this password as default 
     $this->password = $this->getDI() 
      ->getSecurity() 
      ->hash($tempPassword); 
    } else { 
     // The user must not change its password in first login 
     $this->mustChangePassword = 'N'; 
    } 

    // The account must be confirmed via e-mail 
    // Only require this if emails are turned on in the config, otherwise account is automatically active 
    if ($this->getDI()->get('config')->useMail) { 
     $this->active = 'N'; 
    } else { 
     $this->active = 'Y'; 
    } 

    // The account is not suspended by default 
    $this->suspended = 'N'; 

    // The account is not banned by default 
    $this->banned = 'N'; 

} 

/** 
* Send a confirmation e-mail to the user if the account is not active 
*/ 
public function sendConfirmationEmail() 
{ 
    // Only send the confirmation email if emails are turned on in the config 
    if ($this->getDI()->get('config')->useMail) { 

     if ($this->active == 'N') { 

      $emailConfirmation = new EmailConfirmations(); 

      $emailConfirmation->usersId = $this->id; 

      if ($emailConfirmation->save()) { 
       $this->getDI() 
        ->getFlash() 
        ->notice('A confirmation mail has been sent to ' . $this->email); 

      } 
     } 
    } 
} 

/** 
* Validate that emails are unique across users 
*/ 
public function validation() 
{ 
    $validator = new Validation(); 

    $validator->add('email', new Uniqueness([ 
     "message" => "The email is already registered" 
    ])); 

    return $this->validate($validator); 
} 

public function subscription() 
{ 
    $users = Users::find(); 
    $user = $users->getLast(); 
    $result = $user->newSubscription('main', '2017 Online Individual')->create($this->getTestToken()); 
    return $result; 
} 

protected function getTestToken() 
{ 
    return \Stripe\Token::create([ 
     'card' => [ 
      'number' => '4242424242424242', 
      'exp_month' => 5, 
      'exp_year' => 2020, 
      'cvc' => '123', 
     ], 
    ], ['api_key' => 'sk_test_98CUmA7w2JTAp25qVyMZweM9'])->id; 
} 


public function initialize() 
{ 

    $this->belongsTo('profilesId', __NAMESPACE__ . '\Profiles', 'id', [ 
     'alias' => 'profile', 
     'reusable' => true 
    ]); 

    $this->hasMany('id', __NAMESPACE__ . '\SuccessLogins', 'usersId', [ 
     'alias' => 'successLogins', 
     'foreignKey' => [ 
      'message' => 'User cannot be deleted because he/she has activity in the system' 
     ] 
    ]); 

    $this->hasMany('id', __NAMESPACE__ . '\PasswordChanges', 'usersId', [ 
     'alias' => 'passwordChanges', 
     'foreignKey' => [ 
      'message' => 'User cannot be deleted because he/she has activity in the system' 
     ] 
    ]); 

    $this->hasMany('id', __NAMESPACE__ . '\ResetPasswords', 'usersId', [ 
     'alias' => 'resetPasswords', 
     'foreignKey' => [ 
      'message' => 'User cannot be deleted because he/she has activity in the system' 
     ] 
    ]); 
} 

}

創建條紋用戶/預約並返回客戶ID(stripe_id),最後4(card_last_four)等之後... ,這個函數,我想在轉發到IndexController之前將所有這些保存到SessionController中的數據庫中。

public function subscribeAction($user) 
{ 

    $subscribe = $user->subscription(); 

    $user->save(); 

    return $this->dispatcher->forward([ 
     'controller' => 'index', 
     'action' => 'index' 
    ]); 

用戶名,密碼等全部保存正常,但是我無法獲取要更新的條帶特定列。除非我運行終端命令來更改它們,否則它們將保持爲空。我也一直能夠使用保存如果等到我創建了一個用戶後,再登錄成功地更新它們,然後運行是這樣的:在

if ($user->save($_POST) === false) { 
    $messages = $user->getMessages(); 

    $errorMsg = ''; 
    foreach ($messages as $message) { 
     $errorMsg . "{$message} <br>"; 
    } 
    $this->flashSession->error("Error: $errorMsg"); 
} 

然後:

$user = $this->auth->getUser(); 

$user->stripe_id = "123"; 

$user->save(); 
+0

嘗試使用'var_dump($ user-> save()); die;'轉儲保存結果並查看錯誤消息的內容 – Timothy

+0

它只是給出了一個布爾值true。 – ace

+0

這對我來說是一種奇怪的部分。沒有例外或錯誤。它只是沒有做它應該做的事情。 – ace

回答

0

嘗試是這樣的你的觀點把

<?php $this->flashSession->output() ?> 
+0

我這樣做,並得到「trial_ends_at是必需的」。所以,我進去並手動將該變量設置爲保存之前的當前時間戳。 – ace

+0

它刪除了錯誤,但沒有解決問題 – ace

0

很難說完全是怎麼回事,但它幾乎在我看來像你在一個未提交的數據庫事務。您說在創建帳戶時字段正在保存,但在作爲收銀員的一部分調用收費特徵的方法後,這些字段不會被保存。另外save()方法返回true,這意味着Phalcon 認爲記錄被保存。我沒有看到任何明確開始交易的收銀員代碼,儘管我沒有深入瞭解。

下面是你如何測試我的理論。使用交易系統中爾康從隱性轉向顯性

use Phalcon\Mvc\Model\Transaction\Manager as TxManager;

然後在您的subscribeAction方法:

public function subscribeAction($user) 
{ 
    $manager = new TxManager(); 

    // Request a transaction 
    $transaction = $manager->get(); 

    $subscribe = $user->subscription(); 

    $user->save(); 

    $transaction->commit(); 

    return $this->dispatcher->forward([ 
     'controller' => 'index', 
     'action' => 'index' 
    ]); 
} 

交易系統旨在幫助避免衝突的寫入或競態條件,確保您在交易過程中編寫的所有內容都可以基於全部或全部基礎,並且直到您明確提交爲止。特別是在處理商業問題時,這可以幫助您避免讓一張桌子表示進行了購買,而另一張桌子在該分段處理期間沒有任何支付記錄。

This page有更多關於如何使用Phalcon交易的細節。

+0

所以,我實現了這一點,並拋出了提交的結果,這是一個布爾值TRUE。仍然在數據庫中獲得NULL。也許我沒有正確地初始化模型或其他東西。 – ace

+0

您試圖插入的值是否添加到$ user對象?你說你在數據庫中得到了空值。在保存之前轉儲您期望看到的實際值,例如'echo $ user-> stripe_id'。從你的另一篇文章中,我以爲你根本沒有在數據庫中找到任何東西。 –

+0

下面是我跑的: 'public function subscribeAction($ user) { $ manager = new TxManager();要求交易 $ transaction = $ manager-> get(); $ subscribe = $ user-> subscription(); $ dump = $ subscribe-> stripe_id; var_dump($ dump); $ subscribe-> save(); $ trans = $ transaction-> commit(); 退出; return $ this-> dispatcher-> forward([ 'controller'=>'index', 'action'=>'index' ]); }' subscription()函數返回用戶對象。轉儲提供了正確的值,但仍然在數據庫中爲NULL。 – ace

相關問題