带有 CRUD 的 Symfony2 实体
Symfony2 Entities with CRUD
我有两个实体:
文件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;
}
?>
文件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();
}
}
?>
一切似乎都是正确的:
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
在此之后,我成功地为 AppBundle:Company 和 AppBundle:Employer 生成了 CRUD!
我可以从 CRUD 界面成功创建新公司,但是当我尝试创建新雇主时,我在 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.
例外情况:
Catchable Fatal Error: Object of class AppBundle\Entity\Company could not be converted to string
只需将 __toString()
方法添加到您的 class AppBundle\Entity\Company
。
最好的问候,亚历山大
我有两个实体:
文件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;
}
?>
文件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();
}
}
?>
一切似乎都是正确的:
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
在此之后,我成功地为 AppBundle:Company 和 AppBundle:Employer 生成了 CRUD!
我可以从 CRUD 界面成功创建新公司,但是当我尝试创建新雇主时,我在 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.
例外情况:
Catchable Fatal Error: Object of class AppBundle\Entity\Company could not be converted to string
只需将 __toString()
方法添加到您的 class AppBundle\Entity\Company
。
最好的问候,亚历山大