【发布时间】:2014-08-27 09:10:18
【问题描述】:
我已经定义了一个像这样的实体:
/**
* @ORM\ManyToOne(targetEntity="Pr\UserBundle\Entity\Client")
* @ORM\JoinColumn(name="client_id", referencedColumnName="id")
*/
private $client_id;
.....
public function setClientId($clientId = null)
{
$this->client_id = $clientId;
return $this;
}
有两个控制器可以用来创建新的数据库条目。第一个是“仅限管理员”,管理员可以使用他选择的客户端 ID 创建一个数据库条目:
->add('client_id', 'entity', array(
'data_class' => null,
'attr' => array(
'class' => 'selectstyle'),
'class' => 'PrUserBundle:Client',
'property' => 'name',
'required' => true,
'label' => 'staff.location',
'empty_value' => 'admin.customer_name',
'empty_data' => null
)
)
......
// Handling the form
$em = $this->getDoctrine()->getManager();
$saniType->setName($form->get('name')->getData());
$saniType->setClientId($form->get('client_id')->getData());
$saniType->setCreated(new \DateTime(date('Y-m-d H:m:s')));
$saniType->setCreatedBy($user->getUsername());
$em->persist($saniType);
$em->flush();
第二个是针对客户端本身的,他无法设置不同的客户端 ID。因此,我刚刚删除了表单字段“client_id”并将其替换为 users->getClientId():
$saniType->setSpecialClientId($form->get('client_id')->getData());
当我以管理员身份添加条目时,它工作正常。如果我尝试添加一个作为“客户端”,它会崩溃并显示以下错误消息
Warning: spl_object_hash() expects parameter 1 to be object, integer given in /var/www/symfony/webprojekt/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1389
我是 symfony 的新手,所以我没有力气弄清楚会发生什么。我只知道在第一种情况下,admin 会提交一个对象(实体)。在第二种(客户)情况下,我设置一个整数,因为我得到一个
$user = $this->container->get('security.context')->getToken()->getUser();
是否有解决方案,以便我可以处理实体对象并给定整数,就像客户端添加条目时一样?
【问题讨论】:
标签: symfony object doctrine entity