douxing5199 2015-12-12 11:59
浏览 79
已采纳

具有CRUD的Symfony2实体

I have two entities:

File Company.php

<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="Company")
 */

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

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

        /** @ORM\Column(type="integer") */
        protected $bulstat;

        /** @ORM\Column(type="string", length=100) */
        protected $city;

        /** @ORM\Column(type="string", length=50) */
        protected $email;

        /** @ORM\Column(type="string", length=50) */
        protected $contact_person;

        /** @ORM\Column(type="string", length=50) */
        protected $telephone;
}
?>

File Employer.php

<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="Employer")
 */

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

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

        /** @ORM\Column(type="string", length=100) */
        protected $position;

        /**
        * @ORM\ManyToMany(targetEntity="Company")
        * @ORM\JoinTable(name="employer2companies",
        *       joinColumns={@ORM\JoinColumn(name="employer_id", referencedColumnName="id")},
        *       inverseJoinColumns={@ORM\JoinColumn(name="company_id", referencedColumnName="id", unique=true)}
        *       )
        */

        private $companies;

        public function __construct() {
                $this->companies = new \Doctrine\Common\Collections\ArrayCollection();
        }

}
?>

Everything seems correct:

php console doctrine:generate:entities AppBundle:
Generating entities for bundle "AppBundle"
  > backing up Company.php to Company.php~
  > generating AppBundle\Entity\Company
  > backing up Employer.php to Employer.php~
  > generating AppBundle\Entity\Employer

php console doctrine:schema:update --force:
Updating database schema...
Database schema updated successfully! "5" queries were executed

After this, I'm generating CRUD for AppBundle:Company and AppBundle:Employer successfully!

I can successfully create a new company from the CRUD interface, but when I try to create a new Employer I get this error at http://xxxxx.com/admin/employer/new:

Oops! An Error Occurred
The server returned a "500 Internal Server Error".
Something is broken. Please let us know what you were doing when thiserror occurred. We will fix it as soon as possible. Sorry for any inconvenience caused. 

Here is the exception:

Catchable Fatal Error: Object of class AppBundle\Entity\Company could not be converted to string 
  • 写回答

1条回答 默认 最新

  • duanche4578 2015-12-12 12:05
    关注

    Just add a __toString() method to your class AppBundle\Entity\Company.

    Best regards, Alexander

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历