Catchable Fatal Error: Object of class Proxies\__CG__\AppBundle\Entity\Modelo could not be converted to string

3.8k Views Asked by At

I'm starting with Symfony 3 and EasyAdminBundle. My problem is when I tried to edit or create a entity with a foreign key I get an Exception:

"Catchable Fatal Error: Object of class Proxies__CG__\AppBundle\Entity\Modelo could not be converted to string 500 Internal Server Error - ContextErrorException" in vendor\symfony\symfony\src\Symfony\Bridge\Doctrine\Form\Type\DoctrineType.php at line 59

public static function createChoiceLabel($choice)
{
    return (string) $choice;
}

VehiculoType.php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class VehiculoType extends AbstractType
{
/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('patente')->add('modelo', 'entity');
}

/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Vehiculo'
    ));
}

/**
 * {@inheritdoc}
 */
public function getBlockPrefix()
{
    return 'appbundle_vehiculo';
}
public function __toString()
{
    return (string) $this->getVehiculo();
}

}

And my entity Vehiculo.php

namespace AppBundle\Entity;
/*** Vehiculo*/
class Vehiculo{
/**
 * @var int
 */
private $id;

/**
 * @var string
 */
private $patente;


/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * Set patente
 *
 * @param string $patente
 *
 * @return Vehiculo
 */
public function setPatente($patente)
{
    $this->patente = $patente;

    return $this;
}

/**
 * Get patente
 *
 * @return string
 */
public function getPatente()
{
    return $this->patente;
}
/**
 * @var string
 */
private $oneToOne;


/**
 * Set oneToOne
 *
 * @param string $oneToOne
 *
 * @return Vehiculo
 */
public function setOneToOne($oneToOne)
{
    $this->oneToOne = $oneToOne;

    return $this;
}

/**
 * Get oneToOne
 *
 * @return string
 */
public function getOneToOne()
{
    return $this->oneToOne;
}
/**
 * @var \AppBundle\Entity\Modelo
 */
private $modelo;


/**
 * Set modelo
 *
 * @param \AppBundle\Entity\Modelo $modelo
 *
 * @return Vehiculo
 */
public function setModelo(\AppBundle\Entity\Modelo $modelo = null)
{
    $this->modelo = $modelo;

    return $this;
}

/**
 * Get modelo
 *
 * @return \AppBundle\Entity\Modelo
 */
public function getModelo()
{
    return $this->modelo;
}
}
2

There are 2 best solutions below

0
On

Your modelo field should define the choice_label option in order to display correctly something that allows to identify the entity:

$builder->add('patente')->add('modelo', 'entity', [
    'choice_label' => 'name'
]);

Replace name with a property implemented by the Modelo entity.

0
On

You need to override the __toString() method in your entity.

The easyAdmin bundle is trying to show you the entity that is mapped in the New and Edit view. But it doesn't know how to show it.

Simply adding the following to your entity should resolve it (you can replace patente if another string makes more sense):

    public function __toString()
{
    return $this->patente;
}