我試圖堅持一個實體,它不工作。在我的測試設置中,我能夠成功保留其中一個條目。但在實際的代碼中,這並不是持久的。所以,我深深一頭扎進了代碼,我發現,雖然堅持,在堅持不工作,在UnitOfWork.php
,下面一行將返回的東西在每種情況下不同:新實體不持久STATE_MANAGED
$entityState = $this->getEntityState($entity, self::STATE_NEW);
switch ($entityState) {
工作回報:case self::STATE_NEW:
或2
不工作的回報:case self::STATE_MANAGED:
或1
但不工作的代碼應該是新的。
工作:
public static function createAuthorizationCode(string $authorizationCodeHash, Client $client, DateTime $expires)
{
$authorizationCode = new AuthorizationCode();
$authorizationCode->setAuthorizationCodeHash($authorizationCodeHash);
$authorizationCode->setClient($client);
$authorizationCode->setExpires($expires);
self::getEntityManager()->persist($authorizationCode);
return $authorizationCode;
}
不工作:
public function setAuthorizationCode(AuthorizationCode $authorizationCode, string $authorizationCodeHash, Client $client, User $user, $redirectUri, DateTime $expires, $scope, $idToken)
{
$authorizationCode->setAuthorizationCodeHash($authorizationCodeHash);
$authorizationCode->setClient($client);
$authorizationCode->setUser($user);
$authorizationCode->setRedirectUri($redirectUri);
$authorizationCode->setExpires($expires);
if($scope)
$authorizationCode->setScope($scope);
if ($idToken)
$authorizationCode->setIdToken($idToken);
$this->em->persist($authorizationCode);
$this->em->flush();
}
不工作的來電者:
public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null, $id_token = null)
{
try {
$authorizationCode = $this->authorizationCodeRepository->getAuthorizationCode($code);
} catch (AuthorizationCodeNotFoundException $e) {
//this line is hit for sure and an empty object is being
//passed into setAuthorizationCode
$authorizationCode = new AuthorizationCode();
}
$oScope = $scope ? $this->scopeRepository->getScopeById($scope) : null;
$this->authorizationCodeRepository->setAuthorizationCode(
$authorizationCode,
$code,
$this->clientRepository->getClientById($client_id),
$this->userRepository->getUserByUserName($user_id),
$redirect_uri,
self::timeStampToDateTime($expires),
$oScope,
$id_token);
return true;
}
型號:
<?php
namespace PIAuth\Entity\AccessToken;
use PIAuth\Entity\Client\Client;
use PIAuth\Entity\Scope\Scope;
use PIAuth\Entity\User\User;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class AccessToken
{
public function __construct()
{
$this->deleted = false;
}
/**
* @ORM\Id
* @ORM\Column(type="string", unique=true, length=40, nullable=false)
* @var string
*/
protected $accessTokenHash;
/**
* @ORM\Column(type="string", length=80, nullable=true)
* @var string
*/
protected $idToken;
/**
* @ORM\Column(type="boolean")
* @var bool
*/
protected $deleted;
/**
* @ORM\ManyToOne(targetEntity="PIAuth\Entity\Client\Client")
* @ORM\JoinColumn(referencedColumnName="clientId")
* @var Client
*/
protected $client;
/**
* @ORM\ManyToOne(targetEntity="PIAuth\Entity\User\User")
* @ORM\JoinColumn(referencedColumnName="userName")
* @var User
*/
protected $user;
/**
* @ORM\Column(type="datetime")
* @var DateTime
*/
protected $expires;
/**
* @ORM\ManyToOne(targetEntity="PIAuth\Entity\Scope\Scope")
* @ORM\JoinColumn(referencedColumnName="scope")
* @var Scope
*/
protected $scope;
/**
* @return string
*/
public function getAccessTokenHash(): string
{
return $this->accessTokenHash;
}
/**
* @param string $accessTokenHash
*/
public function setAccessTokenHash(string $accessTokenHash)
{
$this->accessTokenHash = $accessTokenHash;
}
/**
* @return Client
*/
public function getClient()
{
return $this->client;
}
/**
* @param Client $client
*/
public function setClient(Client $client)
{
$this->client = $client;
}
/**
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
/**
* @return DateTime
*/
public function getExpires(): DateTime
{
return $this->expires;
}
/**
* @param DateTime $expires
*/
public function setExpires(DateTime $expires)
{
$this->expires = $expires;
}
/**
* @return Scope
*/
public function getScope()
{
return $this->scope;
}
/**
* @param Scope $scope
*/
public function setScope(Scope $scope)
{
$this->scope = $scope;
}
/**
* @return mixed
*/
public function getIdToken()
{
return $this->idToken;
}
/**
* @param mixed $idToken
*/
public function setIdToken($idToken)
{
$this->idToken = $idToken;
}
/**
* @return bool
*/
public function isDeleted(): bool
{
return $this->deleted;
}
/**
* @param bool $deleted
*/
public function setDeleted(bool $deleted)
{
$this->deleted = $deleted;
}
}
它與例外無關。我回答了我自己的問題。 – Bluebaron
@Bluebaron在你的問題中你沒有提到它...... – ste