可捕获的致命错误:类Proxies的对象\ __ CG __ \ AppBundle \ Entity \ Modelo无法转换为字符串

问题描述 投票:2回答:2

我从Symfony 3和EasyAdminBundle开始。我的问题是当我尝试使用外键编辑或创建实体时,我得到一个异常:

“可捕获的致命错误:类代理的对象_CG __ \ AppBundle \ Entity \ Modelo无法转换为字符串500内部服务器错误 - ContextErrorException”在vendor \ symfony \ symfony \ src \ Symfony \ Bridge \ Doctrine \ Form \ Type \ DoctrineType.php中在第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();
}

}

和我的实体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;
}
}
php symfony doctrine-orm symfony2-easyadmin
2个回答
1
投票

你的modelo字段应该定义choice_label选项,以便正确显示允许识别实体的东西:

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

name替换为Modelo实体实现的属性。


0
投票

您需要覆盖实体中的__toString()方法。

easyAdmin包试图向您显示在“新建”和“编辑”视图中映射的实体。但它不知道如何展示它。

只需将以下内容添加到您的实体就可以解决它(如果另一个字符串更有意义,您可以替换patente):

    public function __toString()
{
    return $this->patente;
}
© www.soinside.com 2019 - 2024. All rights reserved.