小编Tob*_*ies的帖子

Doctrine 2 - 禁止ManyToOne关系的外键上的空值

我在我的一个实体中有一个ManyToOne关系,如下所示:

class License {
    // ...
    /**
     * Customer who owns the license
     * 
     * @var \ISE\LicenseManagerBundle\Entity\Customer
     * @ORM\ManyToOne(targetEntity="Customer", inversedBy="licenses")
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
     */
    private $customer;
    // ...
}

class Customer {
    // ...
    /**
     * Licenses that were at one point generated for the customer
     * 
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ORM\OneToMany(targetEntity="License", mappedBy="customer")
     */
    private $licenses;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

这将生成一个数据库模式,其中允许许可证表的"customer_id"字段为空,这正是我不想要的.

这里是一些代码,我创建一个记录来证明它确实允许引用字段的空值:

$em = $this->get('doctrine')->getEntityManager();
$license = new License();
// Set some fields - not the reference fields though
$license->setValidUntil(new \DateTime("2012-12-31")); …
Run Code Online (Sandbox Code Playgroud)

php relationship many-to-one doctrine-orm

52
推荐指数
2
解决办法
2万
查看次数

Symfony2:使用Doctrine2实体进行身份验证 - 保存实体后密码设置为空值

我在这里走到了尽头.当我在控制器中保存任何类型的实体时,当前登录的用户的密码和盐在数据库中被消隐.

这是我的安全配置的相关部分:

security:
    encoders:
        ISE\LoginBundle\Entity\User:
            algorithm: sha1
            iterations: 1
            encode_as_base64: false
    providers:
        main:
            entity:
                class: ISE\LoginBundle\Entity\User
                property: username
Run Code Online (Sandbox Code Playgroud)

这是我的用户类的eraseCredentials方法.我怀疑在某些时候调用此方法,然后使用这些更改将用户对象保存到数据库.但我不知道那可能是什么:

class User implements UserInterface {
    // ...
    public function eraseCredentials() {
        $this->password = null;
        $this->salt = null;
    }
    // ...
}
Run Code Online (Sandbox Code Playgroud)

这是我如何在我的一个控制器中保存实体的示例,在这种情况下它是ProductController.只是提醒一下:我没有以任何方式操纵我的代码中的User对象:

public function createAction() {
    // ...
    if ($form->isValid()) {
        $em = $this->get('doctrine')->getEntityManager();
        $em->persist($product);
        $em->flush();
        return $this->redirect($this->generateUrl('product_create', array('created' => true)));
    }
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我不希望任何此代码删除数据库中的用户密码或salt,但确实如此.任何人都可以帮我打败我的代码吗?

php authentication entity symfony doctrine-orm

2
推荐指数
1
解决办法
1818
查看次数

Symfony2 - 来自表单集合元素的数据最终为数组而不是实体

我有两个具有一对多关系的Doctrine实体,如下所示:

执照

class License {    
    /**
     * Products this license contains
     * 
     * @var \Doctrine\Common\Collections\ArrayCollection
     * @ORM\OneToMany(targetEntity="LicenseProductRelation", mappedBy="license")
     */
    private $productRelations;
}
Run Code Online (Sandbox Code Playgroud)

LicenseProductRelation:

class LicenseProductRelation {
    /**
     * The License referenced by this relation
     * 
     * @var \ISE\LicenseManagerBundle\Entity\License
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="License", inversedBy="productRelations")
     * @ORM\JoinColumn(name="license_id", referencedColumnName="id", nullable=false)
     */
    private $license;
}
Run Code Online (Sandbox Code Playgroud)

我有许可证实体的此表格:

class LicenseType extends AbstractType {

    public function buildForm(FormBuilder $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder->add('productRelations', 'collection',
            array('type' => new LicenseProductRelationType(),
                  'allow_add' => true,
                  'allow_delete' => true,
                  'prototype' => …
Run Code Online (Sandbox Code Playgroud)

php forms symfony

2
推荐指数
1
解决办法
6703
查看次数