2016-12-28 300 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

相关问题