Silex object cannot be converted to a valid array

I am trying to create a web application with Silex. For my application, I have two objects: Project () and Credential (). Project():

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 account:

 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 the credentials () in the credentials values. No problem transferring 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 to fetch Project () from the database and pass it to 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:

A value of type "object" cannot be converted to a valid array.

I think my problem is that the Project () object has a property containing the Credential () object.

+5
source share
1 answer

I was able to recreate the exception 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 cannot use the credential property with the choice form field.

 public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('credential', 'choice', array( 'choices' => $choices, 'multiple' => false, 'expanded' => false )) ->add('submit', 'submit') ; } 

You can try adding the id property of the identity to Project ().

 public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('credentialId', 'choice', array( 'choices' => $choices, 'multiple' => false, 'expanded' => false )) ->add('submit', 'submit') ; } 

Or, you may need to configure the type of the entity field, as described in this surface stack question.

+1
source

All Articles