以下是代码:
config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: false
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Assetic Configuration
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: [ ]
#java: /usr/bin/java
filters:
cssrewrite: ~
#closure:
# jar: "%kernel.root_dir%/Resources/java/compiler.jar"
#yui_css:
# jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
fos_rest:
routing_loader:
default_format: json
param_fetcher_listener: true
body_listener: true
#disable_csrf_role: ROLE_USER
body_converter:
enabled: true
view:
view_response_listener: force
nelmio_cors:
defaults:
allow_credentials: false
allow_origin: []
allow_headers: []
allow_methods: []
expose_headers: []
max_age: 0
paths:
'^/api/':
allow_origin: ['*']
allow_headers: ['*']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
max_age: 3600
sensio_framework_extra:
request: { converters: true }
view: { annotations: false }
router: { annotations: true }
Property.php
/**
* Property
*
* @ORM\Table(name="property")
* @ORM\Entity
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"house" = "House", "office" = "Office"})
*/
abstract class Property
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=100, nullable=false)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="description", type="text", nullable=false)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="price", type="decimal", precision=10, scale=0, nullable=true)
*/
private $price;
function getId() {
return $this->id;
}
function getTitle() {
return $this->title;
}
function getDescription() {
return $this->description;
}
function getPrice() {
return $this->price;
}
function setId($id) {
$this->id = $id;
}
function setTitle($title) {
$this->title = $title;
}
function setDescription($description) {
$this->description = $description;
}
function setPrice($price) {
$this->price = $price;
}
}
House.php
/**
* House
*
* @ORM\Table(name="house")
* @ORM\Entity
*/
class House extends Property
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="numberOfRooms", type="integer", nullable=true)
*/
private $numberOfRooms;
/**
* @var integer
*
* @ORM\Column(name="numberOfBathrooms", type="integer", nullable=true)
*/
private $numberOfBathrooms;
/**
* @var boolean
*
* @ORM\Column(name="hasGarage", type="boolean", nullable=true)
*/
private $hasGarage;
/**
* @var boolean
*
* @ORM\Column(name="hasYard", type="boolean", nullable=true)
*/
private $hasYard;
/**
* @var boolean
*
* @ORM\Column(name="hasGas", type="boolean", nullable=true)
*/
private $hasGas;
function getId() {
return $this->id;
}
function getNumberOfRooms() {
return $this->numberOfRooms;
}
function getNumberOfBathrooms() {
return $this->numberOfBathrooms;
}
function getHasGarage() {
return $this->hasGarage;
}
function getHasYard() {
return $this->hasYard;
}
function getHasGas() {
return $this->hasGas;
}
function setId($id) {
$this->id = $id;
}
function setNumberOfRooms($numberOfRooms) {
$this->numberOfRooms = $numberOfRooms;
}
function setNumberOfBathrooms($numberOfBathrooms) {
$this->numberOfBathrooms = $numberOfBathrooms;
}
function setHasGarage($hasGarage) {
$this->hasGarage = $hasGarage;
}
function setHasYard($hasYard) {
$this->hasYard = $hasYard;
}
function setHasGas($hasGas) {
$this->hasGas = $hasGas;
}
}
PropertyType.php
class PropertyType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('description')
->add('price')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Cboujon\PropertyBundle\Entity\Property'
));
}
}
HouseType.php
class HouseType extends PropertyType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('numberOfRooms')
->add('numberOfBathrooms')
->add('hasGarage')
->add('hasYard')
->add('hasGas')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Cboujon\PropertyBundle\Entity\House'
));
}
}
当我请求http://localhost:8000/api/houses/4
时,我收到状态代码为500的回复以及以下内容:
{"error":{"code":500,"message":"Internal Server Error","exception":[{"message":"Property Cboujon\\PropertyBundle\\Entity\\House::$numberofrooms does not exist","class":"ReflectionException","trace": ...
如果我将属性从$numberOfRooms
重命名为$numberofrooms
,则应用程序会返回新错误
{"error":{"code":500,"message":"Internal Server Error","exception":[{"message":"Property Cboujon\\PropertyBundle\\Entity\\House::$numberofbathrooms does not exist","class":"ReflectionException","trace": ...
我想问题是camelcase所以我尝试配置
fos_rest:
body_listener:
array_normalizer: fos_rest.normalizer.camel_keys
但我一直在收到错误。我做错了什么?
更新1
我尝试使用JSON参数执行请求[POST] http://localhost:8000/api/houses/
并得到相同的错误,但数据已插入数据库中。我真的很困惑。