Search code examples
phpsymfonydoctrine-ormarraycollection

arrayCollection doctrine give a string in place of object


I have 2 entities, document and field, inside entities I have some relation OneToMany and ManyToOne.. I'm troubled cause when I try to get values of fields from document in the controller. I get error cause I can't use methode on string.

to resume:

part of document.php:

/**
 * @var ArrayCollection $fields
 *
 * @ORM\OneToMany(targetEntity="AUTOFUSION\AutofusionBundle\Entity\Field", mappedBy="document", cascade={"persist", "remove", "merge"})
 */
private $fields;

/**
 * Constructor
 */
public function __construct()
{
    $this->fields = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add field
 *
 * @param \AUTOFUSION\Autofusionbundle\Entity\Field $field
 *
 * @return Document
 */
public function addField(\AUTOFUSION\AutofusionBundle\Entity\Field $field)
{
    $this->fields[] = $field;

    return $this;
}

/**
 * Remove field
 *
 * @param \AUTOFUSION\AutofusionBundle\Entity\Field $field
 */
public function removeField(\AUTOFUSION\AutofusionBundle\Entity\Field $field)
{
    $this->fields->removeElement($field);
}

/**
 * Get fields
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getFields()
{
    return $this->fields;
} 

Part of 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;
    }

}

Part of Repository:

 public function FindRegrouping(){

        return $this->_em->createQueryBuilder()
                ->select('p','gdt','dt','d','f')
                ->from('AUTOFUSION\AutofusionBundle\Entity\Regrouping', '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();
    }

here part of the output var_dump($document->getFields());:

protected 'collection' => 
    object(Doctrine\Common\Collections\ArrayCollection)[590]
      private 'elements' => 
        array (size=4)
          0 => string 'NOM_CLIENT' (length=10)
          1 => 
            object(AUTOFUSION\AutofusionBundle\Entity\Field)[599]
              ...
          2 => 
            object(AUTOFUSION\AutofusionBundle\Entity\Field)[600]
              ...
          3 => 
            object(AUTOFUSION\AutofusionBundle\Entity\Field)[601]
              ...

So, I don't understant why the first item of arraycollection is a string?!


Solution

  • Look at my code. When you iterate over getFields(), there is no need to call it again inside loop. getFields() returns array, so when you're not trying to treat it as array, it can behave different. In this case, it tried to be a string.

    PS - you don't need $i counter in your code.

    foreach($document->getFields() as $field){
        var_dump($field);
        $documents[] = $field->getValue();
    }