我对Symfony很陌生,希望有人可以帮助我。我有一个名为 Material 的实体和一个名为 MaterialKey 的关联实体,它们基本上是标签。我在表单的文本字段中显示以字符串分隔的关键字逗号。我创建了一个数据转换器来做到这一点。从数据库中提取关键字并显示它们是没有问题的,但是当我想将现有或新关键字提交到数据库时,我对 reversTransform 函数有问题。
材料类(材料关键字(:
/**
* @AssertType(type="AppBundleEntityMaterialKeyword")
* @AssertValid()
* @ORMManyToMany(targetEntity="MaterialKeyword", inversedBy="material")
* @ORMJoinTable(name="materials_keyword_map",
* joinColumns={@ORMJoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
* inverseJoinColumns={@ORMJoinColumn(name="keywordID", referencedColumnName="id", nullable=false)})
*/
public $materialkeyword;
/**
* Constructor
*/
public function __construct()
{
$this->MaterialKeyword = new ArrayCollection();
}
/**
* Set materialkeyword
*
* @param array $materialkeyword
*
*/
public function setMaterialkeyword(MaterialKeyword $materialkeyword=null)
{
$this->materialkeyword = $materialkeyword;
}
/**
* Get materialkeyword
*
* @AssertType("array")
* @return array
*/
public function getMaterialkeyword()
{
return $this->materialkeyword;
}
这是我来自数据转换器的代码:
这部分正在工作:
class MaterialKeywordTransformer implements DataTransformerInterface
{
/**
* @var EntityManagerInterface
*/
private $manager;
public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}
/**
* Transforms an object (materialkeyword) to a string.
*
* @param MaterialKeyword|null $materialkeyword
* @return string
*/
public function transform($material)
{
$result = array();
if (null === $material) {
return '';
}
foreach ($material as $materialkeyword) {
$result[] = $materialkeyword->getKeyword();
}
return implode(", ", $result);
}
这部分不起作用:
/**
* Transforms a string (keyword) to an object (materialkeyword).
*
* @param string $materialkeyword
* @return MaterialKeyword|null
* @throws TransformationFailedException if object (materialkeyword) is not found.
*/
public function reverseTransform($keywords)
{
// no keyword? It's optional, so that's ok
if (!$keywords) {
return;
}
$repository = $this->manager
->getRepository('AppBundle:MaterialKeyword');
$keyword_array = explode(", ", $keywords);
foreach($keyword_array as $keyword){
$materialkeyword = new MaterialKeyword();
$keyword_entry = $repository->findBy(array('keyword' => $keyword));
if(array_key_exists(0, $keyword_entry)){
$keyword_entry_first = $keyword_entry[0];
}else{
$keyword_entry_first = $keyword_entry;
}
if (null === $keyword_entry_first) {
throw new TransformationFailedException(sprintf('There is no "%s" exists',
$keywords
));
}
$materialkeyword->setKeyword($keyword_entry_first);
}
return $materialkeyword;
}
将有几个关键字,那么我该如何存储它们。我尝试了Arrays和ArrayCollections(新的ArrayCollection(((,但没有成功。
我目前使用上述代码收到的错误:
Catchable Fatal Error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given, called in /.../vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 605 and defined
TL;DR;您的reverseTransform
函数应返回一个包含零或 n 个MaterialKeyword
的数组。
它不应该返回单个MaterialKeyword
对象,因为MaterialKeyord[] --> string
的反向变换不是string --> MaterialKeyword
,而是string --> MaterialKeyword[]
。
考虑到这一点,您拥有的学说 ArrayCollection 例外是有意义的,因为它试图做new ArrayCollection(/** Single MaterialKeyword object */)
而不是new ArrayCollection(/** Array of MaterialKeyword objects */)
.
根据你所说的,我假设Material
和MaterialKeyword
通过 ManyToMany 关联连接,在这种情况下,每个Material
都有一个与之关联的MaterialKeyword
对象数组。
这意味着,您的数据转换器也应该使用数组,但您只处理单个对象。
具体来说,reverseTransform
应该返回一个MaterialKeyword
对象的数组,而你只返回一个(循环中处理的最后一个(。
另一个问题是您的方法每次都会创建新对象,即使$repository->findBy(...)
已经返回MaterialKeyword
实例也是如此。创建新对象将导致复制该条目,而不是简单地使用。
所以正确的方法可能如下所示:
public function reverseTransform($keywords)
{
// no keyword? It's optional, so that's ok
if (!$keywords) {
return array();
}
$repository = $this->manager
->getRepository('AppBundle:MaterialKeyword');
$keyword_array = explode(", ", $keywords);
$result_list = array(); // This will contain the MaterialKeyword objects
foreach($keyword_array as $keyword){
$keyword_entry = $repository->findOneBy(array('keyword' => $keyword));
if (null === $keyword_entry) {
throw new TransformationFailedException(sprintf('There is no "%s" exists',
$keyword
));
}
$result_list[] = $keyword_entry;
}
return $result_list;
}
>@Hanzi让我走上了正确的轨道。它必须是MaterialKeywords
对象的数组。
这是我在类MaterialKeywordTransformer中的最终工作代码:
/**
* Transforms a string (keyword) to an object (materialkeyword).
*
* @param string $materialkeyword
* @return MaterialKeyword|null
* @throws TransformationFailedException if object (materialkeyword) is not found.
*/
public function reverseTransform($keywords)
{
// keyword are optional, so that's ok
if (!$keywords) {
return;
}
$repository = $this->manager
->getRepository('AppBundle:MaterialKeyword');
$repository_m = $this->manager
->getRepository('AppBundle:Material');
$keyword_array = explode(", ", $keywords);
foreach($keyword_array as $keyword){
$materialkeyword = new MaterialKeyword();
$materialkeyword->setKeyword($keyword);
if($this->opt["data"]->getMaterialID() !== null) {
$materialkeyword->setMaterialID($this->opt["data"]->getMaterialID());
} else {
$material = $repository_m->findOne();
$materialID = $material[0]->getMaterialID();
$materialkeyword->setMaterialID($materialID);
}
$materialkeywords[] = $materialkeyword;
if (null === $keywords) {
throw new TransformationFailedException(sprintf('There is no "%s" exists',
$keywords
));
}
}
return $materialkeywords;
}