Reputation: 607
I have 2 forms with a many-to-many relationship between user and profile status. Everything works fine but I can't post a collection using a json object from angular with the following code:
/*
* Update Status
*/
$scope.updateProfilestatus = function () {
var user = [];
user.push($scope.user.id);
var status =
{
body: $scope.status,
enabled: 1,
user: user
};
alert('Tests');
console.log(status);
TGWebService.profilestatuses.getAll.post(status).then(function (result) {
console.log(result);
alert("New Status Created");
});
};
Using the classes below as is produces a response with 200 HTTP Status but no association between users and profilestatuses is created in the database. I could probably parse the user and unserialise the objects and add them programmatically but I'm hoping for a magic bullet solution.
ProfileStatusesType.php
class ProfileStatusesType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('body')
->add('created',
'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))
->add('updated',
'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))
->add('enabled')
->add('user')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TeamGraduate\APIBundle\Entity\ProfileStatuses'
));
}
/**
* @return string
*/
public function getName()
{
return 'teamgraduate_apibundle_profilestatuses';
}
}
users.php
class UsersType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstName')
->add('lastName')
->add('birthdate',
'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD'))
->add('created',
'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))
->add('updated',
'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))
->add('profileStatus')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TeamGraduate\APIBundle\Entity\Users'
));
}
/**
* @return string
*/
public function getName()
{
return 'teamgraduate_apibundle_users';
}
}
Modifying the code in my ProfileStatuses.php produces the following 400 HTTP Status error below:
Additional Code for supporting collections
->add('user',
'collection',
array(
'type'=> new UsersType(),
'prototype'=>true,
'allow_add'=>true,
'allow_delete'=>true,
'options'=>array(
)
)
)
400 Error Response
{
"code": 400,
"message": "Validation Failed",
"errors": {
"errors": ["This value is not valid."],
"children": {
"body": {},
"created": {},
"updated": {},
"enabled": {},
"user": {
"children": [{
"children": {
"firstName": {},
"middleName": {},
"lastName": {},
"birthdate": {},
"postalAddress": {},
"physicalAddress": {},
"idPassportNumber": {},
"telephone": {},
"mobile": {},
"institutionName": {},
"created": {},
"updated": {},
"apiKey": {},
"grade": {},
"institutionUser": {},
"gender": {},
"location": {},
"profileStatus": {},
"view": {},
"milestone": {},
"reportCard": {},
"device": {},
"badge": {},
"followerUser": {},
"inspirationalQuote": {}
}
}
]
},
"cap": {}
}
}
}
Upvotes: 0
Views: 88
Reputation: 711
Try to use by_reference attribute for collection type field
->add('user', 'collection', [
'type' => new UsersType(),
'by_reference' => false,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
])
Upvotes: 1