I'm trying to create a web application with Silex. For my application I have two objects : Project() and Credential(). The Project() one :
protected function buildDomainObject($row)
{
$credential = new Credential();
$credential->setIdCred($row['idCred']);
$credential->setNameCred($row['nameCred']);
$credential->setToken($row['token']);
$project = new Project();
$project->setId($row['id']);
$project->setName($row['name']);
$project->setBranch($row['branch']);
$project->setCredential($credential);
$project->setComment($row['comment']);
$project->setAlive($row['alive']);
$project->setNumberTaskList($row['numberTaskList']);
return $project;
}
And the Credential one :
protected function buildDomainObject($row)
{
$credential = new Credential();
$credential->setIdCred($row['idCred']);
$credential->setNameCred($row['nameCred']);
$credential->setToken($row['token']);
return $credential;
}
As you can see Project() contains Credential() in the value credential. There are no issues when passing a new Project() object to FormBuilder.
public function addProjectAction(Request $request, Application $app)
{
$credentials = $app['credential_repository']->findAllAsArray();
$project = new Project();
$projectForm = $app['form.factory']->create(new ProjectType(), $project, ['credentialChoices' => $credentials]);
$projectForm->handleRequest($request);
if ($projectForm->isSubmitted() && $projectForm->isValid()) {
$app['project_repository']->save($project);
}
return $app['twig']->render('projectList_form.html.twig', array(
'title' => 'New project',
'legend' => 'New project',
'projectForm' => $projectForm->createView(),
)
);
}
The problem occurs when I try and fetch a Project() from the database and pass it into FormBuilder.
$credentials = $app['credential_repository']->findAllAsArray();
$project = $app['project_repository']->find($id);
$projectForm = $app['form.factory']->create(new ProjectType(), $project, ['credentialChoices' => $credentials]);
I have the following error :
The value of type "object" cannot be converted to a valid array key.
I think my problem comes from the fact that the Project() object has a property containing the Credential() object.
I was able to recreate the exception by using the object property in the buildForm method.
InvalidArgumentException in ArrayKeyChoiceList.php line 71: The value of type "object" cannot be converted to a valid array key.
You can't use
credential
property with thechoice
form field.You could try adding a credential id property to Project().
Or you might need to setup the entity field type as explained in this stackoverflow question