A
A
Alexey Alekseev2018-12-02 17:43:59
symfony
Alexey Alekseev, 2018-12-02 17:43:59

How to display fields from a linked table in Symfony 3 in a form?

There are 2 entities. Relationship between them Serviceviewgrouptable:Serviceviewtable = One:Many
Serviceviewgrouptable entity:

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Serviceviewgrouptable
 *
 * @ORM\Table(name="ServiceViewGroupTable")
 * @ORM\Entity
 */
class Serviceviewgrouptable
{
    /**
     * @var string
     *
     * @ORM\Column(name="ServiceGroupID", type="string", length=4, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $servicegroupid;

    /**
     * @var string
     *
     * @ORM\Column(name="ServiceGroupName", type="string", length=200, nullable=false)
     */
    private $servicegroupname;



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

    /**
     * Set servicegroupname
     *
     * @param string $servicegroupname
     *
     * @return Serviceviewgrouptable
     */
    public function setServicegroupname($servicegroupname)
    {
        $this->servicegroupname = $servicegroupname;

        return $this;
    }

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

Serviceviewtable entity:
<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Serviceviewtable
 *
 * @ORM\Table(name="ServiceViewTable", indexes={@ORM\Index(name="IDX_D60FFB86795E9A44", columns={"ServiceGroupKod"}), @ORM\Index(name="IDX_D60FFB8664257B57", columns={"ServiceMeasureKod"})})
 * @ORM\Entity
 */
class Serviceviewtable
{
    /**
     * @var string
     *
     * @ORM\Column(name="ServiceID", type="string", length=10, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $serviceid;

    /**
     * @var string
     *
     * @ORM\Column(name="ServiceName", type="string", length=200, nullable=false)
     */
    private $servicename;

    /**
     * @var string
     *
     * @ORM\Column(name="ServiсeDefaultPrice", type="decimal", precision=18, scale=2, nullable=true)
     */
    private $servicedefaultprice;

    /**
     * @var string
     *
     * @ORM\Column(name="ServiсeDescription", type="string", length=2000, nullable=true)
     */
    private $servicedescription;

    /**
     * @var \AppBundle\Entity\Serviceviewgrouptable
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Serviceviewgrouptable")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="ServiceGroupKod", referencedColumnName="ServiceGroupID")
     * })
     */
    private $servicegroupkod;

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

    /**
     * Set servicename
     *
     * @param string $servicename
     *
     * @return Serviceviewtable
     */
    public function setServicename($servicename)
    {
        $this->servicename = $servicename;

        return $this;
    }

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

    /**
     * Set servicedefaultprice
     *
     * @param string $servicedefaultprice
     *
     * @return Serviceviewtable
     */
    public function setServicedefaultprice($servicedefaultprice)
    {
        $this->servicedefaultprice = $servicedefaultprice;

        return $this;
    }

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

    /**
     * Set servicedescription
     *
     * @param string $servicedescription
     *
     * @return Serviceviewtable
     */
    public function setServicedescription($servicedescription)
    {
        $this->servicedescription = $servicedescription;

        return $this;
    }

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

    /**
     * Set servicegroupkod
     *
     * @param \AppBundle\Entity\Serviceviewgrouptable $servicegroupkod
     *
     * @return Serviceviewtable
     */
    public function setServicegroupkod(\AppBundle\Entity\Serviceviewgrouptable $servicegroupkod = null)
    {
        $this->servicegroupkod = $servicegroupkod;

        return $this;
    }

    /**
     * Get servicegroupkod
     *
     * @return \AppBundle\Entity\Serviceviewgrouptable
     */
    public function getServicegroupkod()
    {
        return $this->servicegroupkod;
    }

}

I create a form:
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('serviceid', HiddenType::class)
            ->add('servicename', TextType::class, array(
                'label' => 'Наименование',
                'attr' => array('class' => 'form-control'),
            ))
            ->add('servicedefaultprice', TextType::class, array(
                'label' => 'Стоимость',
                'attr' => array('class' => 'form-control count')
            ))
            ->add('servicedescription', TextareaType::class, array(
                'label' => 'Описание',
                'attr' => array('class' => 'form-control')
            ))
            ->add('submit', SubmitType::class, array(
                'label' => 'Сохранить',
                'attr' => array('class' => 'btn btn-danger')
            ))
            ->add('servicegroupkod');
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Serviceviewtable'
        ));
    }

But when displaying the form, an error occurs:
Catchable Fatal Error: Object of class AppBundle\Entity\Serviceviewgrouptable could not be converted to string

How can I display data from a field in a linked table?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question