【发布时间】:2017-05-13 05:22:13
【问题描述】:
我已经尝试了所有我能想到的方法,但无法让 model->save() 方法实际更新数据库中的某些列。我的用户模型如下所示(使用 Phalcon Cashier):
<?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'
]
]);
}
}
在创建 Stripe 用户/订阅并返回客户 ID (stripe_id)、Last 4 (card_last_four) 等...,这个函数,我想在转发到 IndexController 之前将所有这些保存到 SessionController 中的数据库.
public function subscribeAction($user)
{
$subscribe = $user->subscription();
$user->save();
return $this->dispatcher->forward([
'controller' => 'index',
'action' => 'index'
]);
用户名、密码等都保存得很好,但我无法更新条带特定的列。除非我运行终端命令来更改它们,否则它们将保持为空。如果我等到创建用户,然后登录并运行类似的东西,我也可以使用 save 成功更新它们:
$user = $this->auth->getUser();
$user->stripe_id = "123";
$user->save();
【问题讨论】:
-
尝试使用
var_dump($user->save());die;转储保存结果并查看错误消息的内容 -
它只是给出一个布尔值 true。
-
这对我来说有点奇怪。没有异常或错误。它只是没有做它应该做的事情。
-
save()函数如果失败则返回布尔值false。要获取影响保存数据的 SQL 错误,可以打印getMessages()。如果它总是返回true,这意味着保存是正确的,但是您可能在模型中声明了一些东西,这会阻止保存stripe_id部分。在尝试保存之前检查您是否已确定设置。
标签: phalcon