我有一个带有数据转换器的简单表单,它可以正常工作(更新,保留和删除),但是我在twig上收到错误。
无法在SonataDoctrineORMAdminBundle中调用NULL变量(“”)的方法(“trans”):CRUD:第30行的edit_orm_one_to_many.html.twig
错误在这一行:
{{ nested_field.vars['sonata_admin'].admin.trans(nested_field.vars.label) }}
所有字段在nested_field.vars ['sonata_admin']中的值都少于我的自定义字段
我的代码是:
$formMapper
->add(
$formMapper->create('articleAmount', 'text')
->addModelTransformer($articleAmountToStringTransformer)
)
...
实体
/**
* AppShopHasArticles
*
* @ORM\Table(name="app_shop_has_articles")
* @ExclusionPolicy("all")
* @ORM\Entity(repositoryClass="Nvia\ShopAppBundle\Entity\Repository\AppShopHasArticlesRepository")
* @ORM\HasLifecycleCallbacks()
*/
class AppShopHasArticles
{
/**
* @var \Nvia\CommonBundle\Entity\Article
*
* @ORM\ManyToOne(targetEntity="Nvia\ShopAppBundle\Entity\Article", inversedBy="appShopHasArticles")
* @ORM\JoinColumn(name="article_id", referencedColumnName="id", nullable=false)
* @ORM\Id
* @Expose
*/
private $article;
/**
* @var \Nvia\CommonBundle\Entity\Country
*
* @ORM\ManyToOne(targetEntity="Nvia\CommonBundle\Entity\Country")
* @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
* @ORM\Id
* @Expose
*/
private $country;
/**
* @var \Nvia\ShopAppBundle\Entity\AppShop
*
* @ORM\ManyToOne(targetEntity="Nvia\ShopAppBundle\Entity\ArticleAmount")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="country_id", referencedColumnName="country_id", nullable=false),
* @ORM\JoinColumn(name="article_id", referencedColumnName="article_id", nullable=false)
* })
* @Expose
*/
private $articleAmount;
...
}
/**
* ArticleAmount
*
* @ORM\Table(name="article_amount")
*/
class ArticleAmount
{
/**
* @var \Nvia\CommonBundle\Entity\Article
*
* @ORM\ManyToOne(targetEntity="Nvia\ShopAppBundle\Entity\Article", inversedBy="articleAmounts")
* @ORM\JoinColumn(name="article_id", referencedColumnName="id", nullable=false)
* @ORM\Id
*/
private $article;
/**
* @var \Nvia\CommonBundle\Entity\Country
*
* @ORM\ManyToOne(targetEntity="Nvia\CommonBundle\Entity\Country")
* @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
* @ORM\Id
*/
private $country;
/**
* @var float
*
* @ORM\Column(name="amount", type="float", precision=10, scale=0, nullable=false)
* @Expose
*/
private $amount;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
}
我做错了什么:/?
答案 0 :(得分:3)
当使用formMapper的创建方法时,sonata不会插入它自己的逻辑。
而是尝试这样的事情
$formMapper
->add('articleAmount', 'text')
->get('articleAmount')
->addModelTransformer($articleAmountToStringTransformer)
在Sonata 2.4上测试