【问题标题】:symfony2 Entity Object vs. integer crashessymfony2 实体对象与整数崩溃
【发布时间】: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


    【解决方案1】:

    您应该将关系定义更改为:

    /**
     * @ORM\ManyToOne(targetEntity="Pr\UserBundle\Entity\Client")
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id")
    
     */
    private $client;
    
    .....
    
    public function setClient($client = null)
    {
        $this->client = $client;
    
       return $this;
    }
    

    然后是形式:

    ->add('client', '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
        )
    )
    

    处理表单:

    form = $this->createForm(new SaniType(), $entity);
    
    $form->handleRequest($request);
    
    if ($form->isValid()) {
        // perform some action...
    
        return $this->redirect($this->generateUrl('some_success'));
    }
    

    更多处理表单:http://symfony.com/doc/current/book/forms.html#handling-form-submissions


    同样一文不值:

    对于 createdBy / updatedBy 等自动更新属性,我建议您使用 Doctrine Extension:https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/blameable.md

    【讨论】:

    • 好的,我改了。我的代码中还有一个错误,因为我应该使用以下命令创建一个数据库条目作为客户端: $saniType->setClient($this->clientId);现在,我收到另一个错误:( 可捕获的致命错误:传递给 Pr\SensorBundle\Entity\SanitationType::setClient() 的参数 1 必须是 Pr\UserBundle\Entity\Client 的实例,
    • 我的意思是,如果有一个 ManyToOne,我将提供一个整数,因为我没有在表单中包含客户选择,我该如何处理?
    • 而不是: $saniType->setClient($this->clientId) 调用: $saniType->setClient($client) 其中 $client 是客户端实体对象。如果您只有 clientId,您始终可以使用 Repository 获取 Client 对象。
    • 哦,好的。 CleintId 来自用户安全令牌,因为每个用户都通过其 ID 分配给客户端。对于,在将新条目添加为“客户端”的情况下,我只有从我的用户安全令牌中分配的客户编号。
    【解决方案2】:

    您不必知道客户端 ID。您必须自己设置客户端。

    /**
     * @ORM\ManyToOne(targetEntity="Pr\UserBundle\Entity\Client")
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id")
     */
    private $client;
    
    .....
    
    public function setClient(\Pr\UserBundle\Entity\Client $client = null)
    {
        $this->client = $client;
    
        return $this;
    }
    

    【讨论】:

      【解决方案3】:

      我找到了解决方案:

      Symfony2: spl_object_hash() expects parameter 1 to be object, string given in Doctrine

      这是一种解决方法,但目前还可以。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-25
        • 2013-01-29
        • 1970-01-01
        • 2011-03-09
        • 1970-01-01
        • 2010-10-04
        相关资源
        最近更新 更多