简体   繁体   English

Symfony实体类似乎不是托管的Doctrine实体

[英]Symfony entity class seems not to be a managed Doctrine entity

I try to create a form with a dropdown menu which holds all entries of a database table named "main_category". 我尝试创建一个带有下拉菜单的表单,该菜单包含名为“main_category”的数据库表的所有条目。

Here is my TemplateUploadType form: 这是我的TemplateUploadType表单:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->setAction('upload')
        ->setMethod('POST')

        ->add('service_category', 'entity', array(
                'label' => 'tpl_upload_service_category_label',
                'class' => '\AppBundle\Entity\MainCategory',
                'placeholder' => 'tpl_upload_service_category_placeholder',
                'attr' => array(
                    'required' => 'true',
                    'class' => 'form-control'
                ),
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('m')
                        ->orderBy('m.serviceCategoryId', 'ASC');
                },
            )
        )

        // button
        ->add('submit', 'submit', array(
                'attr' => array(
                    'class' => 'btn btn-default'
                )
            )
        );
}

And here is my "MainCategory" entity which I created using the doctrine command line tool: 这是我使用doctrine命令行工具创建的“MainCategory”实体:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * MainCategory
 *
 * @ORM\Table(name="main_category")
 * @ORM\Entity
 */
class MainCategory
{
    /**
     * @var integer
     *
     * @ORM\Column(name="service_category_id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $serviceCategoryId;

    /**
     * @var string
     *
     * @ORM\Column(name="service_category", type="string", length=50, nullable=false)
     */
    private $serviceCategory = '';

    /**
     * @var string
     *
     * @ORM\Column(name="main_category", type="string", nullable=false)
     */
    private $mainCategory = 'SAP';

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

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Costfactor", inversedBy="scid")
     * @ORM\JoinTable(name="category_has_costfactor",
     *   joinColumns={
     *     @ORM\JoinColumn(name="scid", referencedColumnName="service_category_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="cfid", referencedColumnName="costfactor_id")
     *   }
     * )
     */
    private $cfid;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="CcRef", inversedBy="serviceCategory")
     * @ORM\JoinTable(name="cc_master",
     *   joinColumns={
     *     @ORM\JoinColumn(name="service_category_id", referencedColumnName="service_category_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="cc_parameter_id", referencedColumnName="cc_parameter_id")
     *   }
     * )
     */
    private $ccParameter;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="KpiRef", inversedBy="serviceCategory")
     * @ORM\JoinTable(name="kpi_master",
     *   joinColumns={
     *     @ORM\JoinColumn(name="service_category_id", referencedColumnName="service_category_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="kpi_parameter_id", referencedColumnName="kpi_parameter_id")
     *   }
     * )
     */
    private $kpiParameter;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="SecRef", inversedBy="serviceCategory")
     * @ORM\JoinTable(name="sec_master",
     *   joinColumns={
     *     @ORM\JoinColumn(name="service_category_id", referencedColumnName="service_category_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="sec_parameter_id", referencedColumnName="sec_parameter_id")
     *   }
     * )
     */
    private $secParameter;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="SlRef", inversedBy="serviceCategory")
     * @ORM\JoinTable(name="sl_master",
     *   joinColumns={
     *     @ORM\JoinColumn(name="service_category_id", referencedColumnName="service_category_id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="sl_parameter_id", referencedColumnName="sl_parameter_id")
     *   }
     * )
     */
    private $slParameter;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->cfid = new \Doctrine\Common\Collections\ArrayCollection();
        $this->ccParameter = new \Doctrine\Common\Collections\ArrayCollection();
        $this->kpiParameter = new \Doctrine\Common\Collections\ArrayCollection();
        $this->secParameter = new \Doctrine\Common\Collections\ArrayCollection();
        $this->slParameter = new \Doctrine\Common\Collections\ArrayCollection();
    }


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

    /**
     * Set serviceCategory
     *
     * @param string $serviceCategory
     * @return MainCategory
     */
    public function setServiceCategory($serviceCategory)
    {
        $this->serviceCategory = $serviceCategory;

        return $this;
    }

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

    /**
     * Set mainCategory
     *
     * @param string $mainCategory
     * @return MainCategory
     */
    public function setMainCategory($mainCategory)
    {
        $this->mainCategory = $mainCategory;

        return $this;
    }

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

    /**
     * Set comment
     *
     * @param string $comment
     * @return MainCategory
     */
    public function setComment($comment)
    {
        $this->comment = $comment;

        return $this;
    }

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

    /**
     * Add cfid
     *
     * @param \AppBundle\Entity\Costfactor $cfid
     * @return MainCategory
     */
    public function addCfid(\AppBundle\Entity\Costfactor $cfid)
    {
        $this->cfid[] = $cfid;

        return $this;
    }

    /**
     * Remove cfid
     *
     * @param \AppBundle\Entity\Costfactor $cfid
     */
    public function removeCfid(\AppBundle\Entity\Costfactor $cfid)
    {
        $this->cfid->removeElement($cfid);
    }

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

    /**
     * Add ccParameter
     *
     * @param \AppBundle\Entity\CcRef $ccParameter
     * @return MainCategory
     */
    public function addCcParameter(\AppBundle\Entity\CcRef $ccParameter)
    {
        $this->ccParameter[] = $ccParameter;

        return $this;
    }

    /**
     * Remove ccParameter
     *
     * @param \AppBundle\Entity\CcRef $ccParameter
     */
    public function removeCcParameter(\AppBundle\Entity\CcRef $ccParameter)
    {
        $this->ccParameter->removeElement($ccParameter);
    }

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

    /**
     * Add kpiParameter
     *
     * @param \AppBundle\Entity\KpiRef $kpiParameter
     * @return MainCategory
     */
    public function addKpiParameter(\AppBundle\Entity\KpiRef $kpiParameter)
    {
        $this->kpiParameter[] = $kpiParameter;

        return $this;
    }

    /**
     * Remove kpiParameter
     *
     * @param \AppBundle\Entity\KpiRef $kpiParameter
     */
    public function removeKpiParameter(\AppBundle\Entity\KpiRef $kpiParameter)
    {
        $this->kpiParameter->removeElement($kpiParameter);
    }

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

    /**
     * Add secParameter
     *
     * @param \AppBundle\Entity\SecRef $secParameter
     * @return MainCategory
     */
    public function addSecParameter(\AppBundle\Entity\SecRef $secParameter)
    {
        $this->secParameter[] = $secParameter;

        return $this;
    }

    /**
     * Remove secParameter
     *
     * @param \AppBundle\Entity\SecRef $secParameter
     */
    public function removeSecParameter(\AppBundle\Entity\SecRef $secParameter)
    {
        $this->secParameter->removeElement($secParameter);
    }

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

    /**
     * Add slParameter
     *
     * @param \AppBundle\Entity\SlRef $slParameter
     * @return MainCategory
     */
    public function addSlParameter(\AppBundle\Entity\SlRef $slParameter)
    {
        $this->slParameter[] = $slParameter;

        return $this;
    }

    /**
     * Remove slParameter
     *
     * @param \AppBundle\Entity\SlRef $slParameter
     */
    public function removeSlParameter(\AppBundle\Entity\SlRef $slParameter)
    {
        $this->slParameter->removeElement($slParameter);
    }

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

As a result of using above code I recieve the error message: 由于使用上面的代码我收到错误消息:
Class "\\AppBundle\\Entity\\MainCategory" seems not to be a managed Doctrine entity. 类“\\ AppBundle \\ Entity \\ MainCategory”似乎不是托管的Doctrine实体。 Did you forget to map it? 你忘记了映射吗?

What am I doing wrong? 我究竟做错了什么? If you need more information please let me know. 如果您需要更多信息,请告诉我。

I think the problem is in this line: 我认为问题出在这一行:

'class' => '\AppBundle\Entity\MainCategory',

Remove first trailing slash: 删除第一个尾部斜杠:

'class' => 'AppBundle\Entity\MainCategory',

I found the answer thanks to @malcolm 我找到了答案,感谢@malcolm

First I changed my TemplateUploadType form so there was no slash at the beginning of the line: 首先,我更改了我的TemplateUploadType表单,因此在行的开头没有斜杠:

'class' => 'AppBundle\Entity\MainCategory',

This led to another error called "Catchable Fatal Error: Object of class AppBundle\\Entity\\MainCategory could not be converted to string" . 这导致另一个错误称为“可捕获的致命错误:类AppBundle \\ Entity \\ MainCategory的对象无法转换为字符串” Investigating this I found the answer in this blog post . 调查这个我在这篇博文中找到了答案。

Basically I had to overwrite the __toString() function inside my entity class. 基本上我不得不覆盖实体类中的__toString()函数。

public function __toString()
{
    return strval($this->serviceCategory);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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