简体   繁体   中英

Displaying an entity field type as table of some properties of each entity in Symfony2

I need have a unidirectional One-To-Many relation between two entities.
For letting user select the relations I use entity field type:

$builder->add('selectedItems','entity',array(
                'class'=>'MY\ExBundle\Entity\MyRow',
                'multiple'=>true,
                'expanded'=>true,
            )
        )

But with this I only have a checkbox and its label for each entity.
I need to show each entity as a table row that its columns are some properties of the underlying entity (eg price, count , etc.) in addition to a checkbox so user can see more details about entity and check them if he want.
for example:

+---+-------+--------+--------------+
|   | price |  count | deliveryDate |
+---+-------+--------+--------------+
| X |  100  |    6   | 2015-01-02   |
+---+-------+--------+--------------+
|   |   70  |    5   | 2015-02-03   |
+---+-------+--------+--------------+

How can I do this?

Should I create a new Field Type? if so how can I access the underlying child entity?

or I need to use something like embed form collections? if so how make symfony threat it as a choice list and not newly added entries.


Edit: this is the MY\\ExBundle\\Entity\\MyRow entity:

/**
 * MyRow
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="ITW\BidBundle\Entity\MyRowRepository")
 */
class MyRow
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="deliveryDate", type="date")
     */
    private $deliveryDate;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=255,nullable=true)
     */
    private $description;

}

Edit2: I tried to create a field type for this purpose & this is the template I used for it:

{%- block tablechoice_widget -%}
    <table {{ block('widget_container_attributes') }}>
        <thead>
            <th></th>
            <th>price</th>
            <th>count</th>
        </thead>
        <tbody>
    {%- for child in form %}
        <tr>
            <td>{{- form_widget(child) -}}</td>
            <td></td>
            <td></td>
        </tr>
    {% endfor -%}
        </tbody>
    </table>
{%- endblock tablechoice_widget -%}

But I cant access the underlying entity ( MyRow ) of childs ( checkboxes ).

In twig if you use that notation (for example)

{{ entity.child }} 

but entity is a field of your form, you will not access to actual entity object but form representation of it (so checkbox or whatever)

If you want to access "real" object (and its child) you should use

{{ entity.vars.data }}

and for its child

{{ entity.vars.getChild }}

Of course you have to adapt names to your real code.
Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM