我有2个实体,文档和字段,内部实体,我有一些关系onetomany和许多人。当我尝试从控制器中的文档中获取字段值时,我很麻烦。我得到错误,因为我无法在字符串上使用Methode。
恢复:
document.php的一部分:
/**
* @var ArrayCollection $fields
*
* @ORMOneToMany(targetEntity="AUTOFUSIONAutofusionBundleEntityField", mappedBy="document", cascade={"persist", "remove", "merge"})
*/
private $fields;
/**
* Constructor
*/
public function __construct()
{
$this->fields = new DoctrineCommonCollectionsArrayCollection();
}
/**
* Add field
*
* @param AUTOFUSIONAutofusionbundleEntityField $field
*
* @return Document
*/
public function addField(AUTOFUSIONAutofusionBundleEntityField $field)
{
$this->fields[] = $field;
return $this;
}
/**
* Remove field
*
* @param AUTOFUSIONAutofusionBundleEntityField $field
*/
public function removeField(AUTOFUSIONAutofusionBundleEntityField $field)
{
$this->fields->removeElement($field);
}
/**
* Get fields
*
* @return DoctrineCommonCollectionsCollection
*/
public function getFields()
{
return $this->fields;
}
DefaultController.php的一部分:
public function indexAction{
$regroupings = $em->getRepository('AUTOFUSIONAutofusionBundle:Regrouping')->FindRegrouping();
}
public function DocumentsArray($regroupings){
$i=0;
foreach($regroupings as $regrouping){
foreach($regrouping->getDocuments() as $document){
foreach($document->getFields() as $fields){
var_dump($document->getFields());
die();
//$documents[$i] = $document->getFields()->getValue();
}
$i++;
}
return $documents;
}
}
存储库的一部分:
public function FindRegrouping(){
return $this->_em->createQueryBuilder()
->select('p','gdt','dt','d','f')
->from('AUTOFUSIONAutofusionBundleEntityRegrouping', 'p')
->leftJoin('p.groupDocType', 'gdt')
->leftJoin('p.documents','d')
->leftJoin('d.docType', 'dt')
->leftJoin('d.fields', 'f')
//->where('v.cp=:cp')
//->setParameter('cp', $cp);
->getQuery()
->getResult();
}
此处的一部分var_dump($document->getFields());
:
protected 'collection' =>
object(DoctrineCommonCollectionsArrayCollection)[590]
private 'elements' =>
array (size=4)
0 => string 'NOM_CLIENT' (length=10)
1 =>
object(AUTOFUSIONAutofusionBundleEntityField)[599]
...
2 =>
object(AUTOFUSIONAutofusionBundleEntityField)[600]
...
3 =>
object(AUTOFUSIONAutofusionBundleEntityField)[601]
...
所以,我不忽略为什么ArrayCollection的第一项是字符串?!
查看我的代码。当您迭代GetFields((时,无需再次将其调用。getFields()
返回数组,因此,当您不想将其视为数组时,它的行为可能会有所不同。在这种情况下,它试图成为字符串。
ps-您不需要代码中的$i
计数器。
foreach($document->getFields() as $field){
var_dump($field);
$documents[] = $field->getValue();
}