E
E
e-hot2015-06-23 10:16:52
symfony
e-hot, 2015-06-23 10:16:52

Displaying entities, their relationships with each other. How to access them from form classes, including embedded ones?

Situation:
1. Displaying relationships between entities - in test mode,
you can take parent entities from the controller and, through their field properties, query related (child)
entities and, accordingly, any of their field properties - everything is configured, everything works - all data is taken
from corresponding database tables.
2. Now I'm trying to map entities and display relationships between entities
to shove calls from the class (s) of forms or in other words: to tie the work of forms (form classes) to
existing related entities - this is where I have a wedge. I read up and down
the docks on forms and embedded forms (subforms as a collection) and I can’t comprehend the mechanism of this -
and the QUESTION itself:
how to indicate relationships between forms, base and
embedded in it, on top of mappings of relationships in entities. On the forums they write, use the documentation - everything is there, yes, the
documentation has about displaying links - separately, about forms - separately. In the Doctrine Dock, when
using a collection, the collection is placed on the reverse side of the entity relationship. In the Forms and Collections doc,
the collection is placed on the direct side of the relationships between forms. I can’t
understand: in order for the embedded forms to work in the base form, I must
enter an additional field-property into the parent entity in order to
subsequently link the collection of child entities through it? Or should I just
modify an existing field in the parent entity for binding? If I understood correctly, then already at the controller level, should I place the entity object with all entities associated with it
in the $form = $this->createForm(new TaskType(), $task) construct in $task ?
These are the questions and a bunch of
other related questions.
Can you help me figure out these things?
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2015-06-23
@prototype_denis

<?php

class Entity
{
    private $a;
}

class FormType
{
    $builder->add('a');
    $builder->add('other_form'); // ????
}

If you need to attach a form to an entity that has nothing to do with it (a field in the entity), then first, 'mapped' must be false in the foreign form options, and the processing of this form must be hung on the listener.
The question is too confused, please provide better code. It will be easier for everyone.

E
e-hot, 2015-06-23
@e-hot

Situation (see the code below):
There is an entity Posout (equipment), there are entities Postype (equipment type) and Brand (brand).
The Posout (equipment) entity has ONE-WAY ManyToOne relationships with Postype (equipment type) and Brand (brand), respectively, through the fields:

/**
     * @var integer
     * @ORM\ManyToOne(targetEntity="Acme\AppBundle\Entity\Postype", inversedBy="postype_id")
     * @ORM\JoinColumn(name="postype_id", referencedColumnName="id")
     */
    private $postype;

and
/**
     * @var integer
     * @ORM\ManyToOne(targetEntity="Acme\AppBundle\Entity\Brand", inversedBy="posbrand_id")
     * @ORM\JoinColumn(name="posbrand_id", referencedColumnName="id")
     */
    private $posbrand;

Two-way communication is not needed, because Postype and Brand are ready-made lists of values ​​in the corresponding database tables that will not be edited, added to or deleted, they will simply be used stupidly.
The form for creating new equipment (while I am considering a separate form for creating and a separate form for editing equipment) works fine through constructions, such as:
...
->add( 'postype', new PostypeType() )
->add( 'posbrand', new BrandType() )
...

- drop-down lists of types and brands are displayed, each in its own field.
But my task is to make a form for editing equipment (changing the type and brand), where in these same drop-down lists the current values ​​​​should be highlighted as selected. In connection with this formulation of the problem, I had a general question:
how to access related entities from the form class (possibly embedded)?
If again something is not clear in the description, write - I will decipher.
The Posout entity (part of the code with fields that are unnecessary to understand and with getters and setters is omitted):
<?php
namespace Acme\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * Posout
 * @ORM\Table(name="posout")
 * @ORM\Entity(repositoryClass="Acme\AppBundle\Repository\PosoutRepository")
 */
class Posout {
    /**
     * @var integer
     * @ORM\Column(name="id", type="bigint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    ...
    /**
     * @var integer
     * @ORM\ManyToOne(targetEntity="Acme\AppBundle\Entity\Postype", inversedBy="postype_id")
     * @ORM\JoinColumn(name="postype_id", referencedColumnName="id")
     */
    private $postype;
    
    /**
     * @var integer
     * @ORM\ManyToOne(targetEntity="Acme\AppBundle\Entity\Brand", inversedBy="posbrand_id")
     * @ORM\JoinColumn(name="posbrand_id", referencedColumnName="id")
     */
    private $posbrand;

    ...

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

    /**
     * Set postype
     * @param integer $postype
     * @return Posout
     */
    public function setPostype( $postype )
    {
        $this->postype = $postype;
        return $this;
    }

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

    /**
     * Set posbrand
     * @param integer $posbrand
     * @return Posout
     */
    public function setPosbrand( $posbrand )
    {
        $this->posbrand = $posbrand;
        return $this;
    }

    /**
     * Get posbrand_id
     * @return integer 
     */
    public function getPosbrand()
    {
        return $this->posbrand;
    }

    ...
}

Postype entity (part of the code with fields that are unnecessary to understand and with getters and setters is omitted):
<?php
namespace Acme\AppBundle\Entity;use Doctrine\ORM\Mapping as ORM;

/**
 * Postype
 * @ORM\Table(name="postype")
 * @ORM\Entity
 */
class Postype {
    /**
     * @var integer
     * @ORM\Column(name="id", type="bigint")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer
     * @ORM\Column(name="postype_id", type="integer")
     */
    private $postype_id;

    
    /**
     * @var string
     * @ORM\Column(name="postype_desc", type="string")
     */
    private $postype_desc;
    
    /**
     * @var string
     * @ORM\Column(name="postype_name", type="string")
     */
    private $postype_name;
    
    /**
     * Get id
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set postype_id
     * @param integer $postypeId
     * @return Postype
     */
    public function setPostypeId($postypeId)
    {
        $this->postype_id = $postypeId;
        return $this;
    }

    /**
     * Get postype_id
     * @return integer 
     */
    public function getPostypeId()
    {
        return $this->postype_id;
    }
    ...
}

And the Brand entity (part of the code with fields that are unnecessary for understanding and with getters and setters is omitted):
<?phpnamespace Acme\AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * Brand
 * @ORM\Table(name="brand")
 * @ORM\Entity
 */
class Brand {
    /**
     * @var integer
     * @ORM\Column(name="id", type="bigint")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var integer
     * @ORM\Column(name="posbrand_id", type="bigint")
     */
    private $posbrand_id;
    
    /**
     * @var string
     * @ORM\Column(name="posbrand_name", type="string")
     */
    private $posbrand_name;

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

    /**
     * Set posbrand_id
     * @param integer $posbrandId
     * @return Brand
     */
    public function setPosbrandId($posbrandId)
    {
        $this->posbrand_id = $posbrandId;
        return $this;
    }

    /**
     * Get posbrand_id
     * @return integer 
     */
    public function getPosbrandId()
    {
        return $this->posbrand_id;
    }
    ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question