Question

Hi I'm writing a little Web App with Symfony.

I have a one to many relation ship between two entities.

Entity one:

/**
 * @ORM\Entity
 * @ORM\Table(name="Helfer")
 */
class Helfer {

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     * @Assert\NotBlank()
     */
    protected $name;

    /**
     * @ORM\Column(type="string")
     * @Assert\NotBlank()
     */
    protected $lastName;

    /**
     * @ORM\Column(type="string")
     */
    protected $tel;

    /**
     * @ORM\Column(type="string")
     * @Assert\NotBlank()
     * @Assert\Email()
     */
    protected $email;

    /**
     * @ORM\Column(type="string")
     */
    protected $organisation;

    /**
     * @ORM\Column(type="integer")
     * @Assert\NotBlank()
     * @Assert\Range(min=6, minMessage="Du musst mindestens 6 Jahre Alt sein um bei uns helfen zu können.")
     */
    protected $age;

    /**
     * @ORM\OneToMany(targetEntity="Aufgaben", mappedBy="helfer", cascade={"persist","remove"})
     */
    protected $aufgaben;

    /**
     * @ORM\OneToMany(targetEntity="Zeiten", mappedBy="helfer", cascade={"persist","remove"})
     */
    protected $zeiten;

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

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Helfer
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set lastName
     *
     * @param string $lastName
     * @return Helfer
     */
    public function setLastName($lastName)
    {
        $this->lastName = $lastName;

        return $this;
    }

    /**
     * Get lastName
     *
     * @return string 
     */
    public function getLastName()
    {
        return $this->lastName;
    }

    /**
     * Set tel
     *
     * @param string $tel
     * @return Helfer
     */
    public function setTel($tel)
    {
        $this->tel = $tel;

        return $this;
    }

    /**
     * Get tel
     *
     * @return string 
     */
    public function getTel()
    {
        return $this->tel;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return Helfer
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set organisation
     *
     * @param string $organisation
     * @return Helfer
     */
    public function setOrganisation($organisation)
    {
        $this->organisation = $organisation;

        return $this;
    }

    /**
     * Get organisation
     *
     * @return string 
     */
    public function getOrganisation()
    {
        return $this->organisation;
    }

    /**
     * Set age
     *
     * @param integer $age
     * @return Helfer
     */
    public function setAge($age)
    {
        $this->age = $age;

        return $this;
    }

    /**
     * Get age
     *
     * @return integer 
     */
    public function getAge()
    {
        return $this->age;
    }

    /**
     * Add aufgaben
     *
     * @param \DLRG\HelferBundle\Entity\Aufgaben $aufgaben
     * @return Helfer
     */
    public function addAufgaben(\DLRG\HelferBundle\Entity\Aufgaben $aufgaben)
    {
        $this->aufgaben[] = $aufgaben;

        return $this;
    }

    /**
     * Remove aufgaben
     *
     * @param \DLRG\HelferBundle\Entity\Aufgaben $aufgaben
     */
    public function removeAufgaben(\DLRG\HelferBundle\Entity\Aufgaben $aufgaben)
    {
        $this->aufgaben->removeElement($aufgaben);
    }

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

    /**
     * Add zeiten
     *
     * @param \DLRG\HelferBundle\Entity\Zeiten $zeiten
     * @return Helfer
     */
    public function addZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
    {
        $this->zeiten[] = $zeiten;

        return $this;
    }

    /**
     * Remove zeiten
     *
     * @param \DLRG\HelferBundle\Entity\Zeiten $zeiten
     */
    public function removeZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
    {
        $this->zeiten->removeElement($zeiten);
    }

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

Entity two:

 /**
 * @ORM\Entity
 * @ORM\Table(name="Aufgaben")
 */
class Aufgaben {

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $name;

    /**
     * @ORM\Column(type="string")
     */
    protected $beschreibung;

    /**
     * @ORM\ManyToOne(targetEntity="Helfer", inversedBy="aufgaben")
     * @ORM\JoinColumn(name="helfer_aufgaben_id", referencedColumnName="id")
     */
    protected $helfer;

    /**
     * @ORM\OneToMany(targetEntity="Zeiten", mappedBy="aufgaben", cascade={"persist","remove"})
     */
    protected $zeiten;

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

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Aufgaben
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set beschreibung
     *
     * @param string $beschreibung
     * @return Aufgaben
     */
    public function setBeschreibung($beschreibung)
    {
        $this->beschreibung = $beschreibung;

        return $this;
    }

    /**
     * Get beschreibung
     *
     * @return string 
     */
    public function getBeschreibung()
    {
        return $this->beschreibung;
    }

    /**
     * Set helfer
     *
     * @param \DLRG\HelferBundle\Entity\Helfer $helfer
     * @return Aufgaben
     */
    public function setHelfer(\DLRG\HelferBundle\Entity\Helfer $helfer = null)
    {
        $this->helfer = $helfer;

        return $this;
    }

    /**
     * Get helfer
     *
     * @return \DLRG\HelferBundle\Entity\Helfer 
     */
    public function getHelfer()
    {
        return $this->helfer;
    }

    /**
     * Add zeiten
     *
     * @param \DLRG\HelferBundle\Entity\Zeiten $zeiten
     * @return Aufgaben
     */
    public function addZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
    {
        $this->zeiten[] = $zeiten;

        return $this;
    }

    /**
     * Remove zeiten
     *
     * @param \DLRG\HelferBundle\Entity\Zeiten $zeiten
     */
    public function removeZeiten(\DLRG\HelferBundle\Entity\Zeiten $zeiten)
    {
        $this->zeiten->removeElement($zeiten);
    }

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

My database has entries in Aufgaben table. Now my user should enter their names and attributes in Helfer.php. Than the user should choose some values from the Aufgaben Entity.

I try to add the Values from Aufgaben.php to a collection.

class HelferType extends AbstractType {
private $aufgaben;
public function __construct($aufgaben) {
    $this->aufgaben = $aufgaben;
}
/**
 *
 * @param FormBuilderInterface $builder         
 * @param array $options            
 */
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add ( 'name' )->add ( 'lastName' )->add ( 'tel' )->add ( 'email' )->add ( 'organisation' )->add ( 'age' );

    $builder->add ( 'aufgaben', 'collection', array (
            'type' => 'choice',
            'options' => array (
                    'choices' => $this->aufgaben 
            ) 
    ) );
}

But this code doesn't work.

Any Ideas ?

Workflow should be:

  1. Enter User information
  2. Choose one or more values from Aufgaben.php
  3. Save all

Thanks in advance

Était-ce utile?

La solution

Depending on your exact needs, one possible solution would be to use an entity form type.

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add ( 'name' )->add ( 'lastName' )->add ( 'tel' )->add ( 'email' )->add ( 'organisation' )->add ( 'age' );

    $builder->add ( 'aufgaben', 'entity', array (
            'class' => 'YourBundle:Aufgaben',
            'multiple' => true,
            'property' => 'name',
            'choices' => $this->aufgaben
        )
    );
}

This would result in a SELECT tag in your HTML.

Autres conseils

maybe this and this can help you.

I resolve this kind of cases overriding the __toString() method in my entity file, not the repository file.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top