Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silex object cannot be converted to a valid array key

Tags:

php

symfony

silex

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.

like image 973
CorentinDodon Avatar asked Nov 09 '22 11:11

CorentinDodon


1 Answers

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 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 could try adding a credential id property 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 might need to setup the entity field type as explained in this stackoverflow question

like image 51
ooXei1sh Avatar answered Nov 14 '22 21:11

ooXei1sh