我試過了所有我能想到的,但無法獲得模型 - > 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();
嘗試使用'var_dump($ user-> save()); die;'轉儲保存結果並查看錯誤消息的內容 – Timothy
它只是給出了一個布爾值true。 – ace
這對我來說是一種奇怪的部分。沒有例外或錯誤。它只是沒有做它應該做的事情。 – ace