我的实体是:
namespace OAuthBundle\Entity;
use FOS\OAuthServerBundle\Entity\Client as BaseClient;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table("oauth_client")
*/
class Client extends BaseClient
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
public function __construct()
{
parent::__construct();
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param $name
* @return Client
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
}
在扩展FOS \ OAuthServerBundle \ Entity \ Client的FOS \ OAuthServerBundle \ Model中,有一些字段,其中之一是randomId
当我尝试插入一些带有教义的记录时,例如:
$client = new Client();
$client->setId(4);
$client->setRandomId('1wy8z9ayt6kgsw0s4cwk04ogs884cg08kkgg04gso4kckcscog');
$client->setRedirectUris(array('http://foo.com'));
$client->setSecret('59az3u43xn4so4g4wswscso4kookwsw00488oogw8kw4w4wgg0');
$client->setAllowedGrantTypes(array('token', 'authorization_code','http://bar.com/grants/foo' ,'refresh_token'));
$client->setName('foobarApp');
$manager->persist($client);
$manager->flush();
我从教义中收到错误:
Doctrine\DBAL\Exception\NotNullConstraintViolationException: An exception occurred while executing 'INSERT INTO oauth_client (name) VALUES (?)' with params ["TestClient"]:
SQLSTATE[HY000]: General error: 1364 Field 'random_id' doesn't have a default value
在刷新前转储$ client-> getRandomId()的转储会返回正确的值。
如果我坚持并刷新从数据库中检索并更新的现有实体,则它可以正常工作。
有人知道为什么在构建插入查询时学说会忽略实体的某些值吗?
表结构为:
CREATE TABLE `oauth_client` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`random_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`redirect_uris` longtext COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:array)',
`secret` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`allowed_grant_types` longtext COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:array)',
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
BaseClient类为:
class Client implements ClientInterface
{
/**
* @var int
*/
protected $id;
/**
* @var string
*/
protected $randomId;
/**
* @var string
*/
protected $secret;
/**
* @var array
*/
protected $redirectUris = array();
/**
* @var array
*/
protected $allowedGrantTypes = array();
public function __construct()
{
$this->allowedGrantTypes[] = OAuth2::GRANT_TYPE_AUTH_CODE;
$this->setRandomId(Random::generateToken());
$this->setSecret(Random::generateToken());
}
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function setRandomId($random)
{
$this->randomId = $random;
}
/**
* {@inheritdoc}
*/
public function getRandomId()
{
return $this->randomId;
}
/**
* {@inheritdoc}
*/
public function getPublicId()
{
return sprintf('%s_%s', $this->getId(), $this->getRandomId());
}
/**
* {@inheritdoc}
*/
public function setSecret($secret)
{
$this->secret = $secret;
}
/**
* {@inheritdoc}
*/
public function getSecret()
{
return $this->secret;
}
/**
* {@inheritdoc}
*/
public function checkSecret($secret)
{
return null === $this->secret || $secret === $this->secret;
}
/**
* {@inheritdoc}
*/
public function setRedirectUris(array $redirectUris)
{
$this->redirectUris = $redirectUris;
}
/**
* {@inheritdoc}
*/
public function getRedirectUris()
{
return $this->redirectUris;
}
/**
* {@inheritdoc}
*/
public function setAllowedGrantTypes(array $grantTypes)
{
$this->allowedGrantTypes = $grantTypes;
}
/**
* {@inheritdoc}
*/
public function getAllowedGrantTypes()
{
return $this->allowedGrantTypes;
}