小编Jea*_*uwy的帖子

Symfony-doctrine:当我删除他的目标实体时需要将列设置为空

我有一个包含此的类区域:

/**
 * @ORM\OneToMany(targetEntity="Planification", mappedBy="zone")
 */
protected $planifications;
Run Code Online (Sandbox Code Playgroud)

和一个类 Planification:

/**
 * @ORM\ManyToOne(targetEntity="Zone", inversedBy="planifications")
 * @ORM\JoinColumn(name="zone_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
 * @Expose
 */
 protected $zone;
Run Code Online (Sandbox Code Playgroud)

但是当我在数据库中查看时,当我删除区域时,zone_id 并未设置为 null。我有任何错误代码。我只看到该区域已被删除,仅此而已。

但如果我以另一种方式配置:“当我删除区域时,我会删除规划”,它会起作用:

在区域类中:

/**
 * @ORM\OneToMany(targetEntity="Planification", mappedBy="zone", orphanRemoval=true, cascade={"remove"})
 */
protected $planifications;
Run Code Online (Sandbox Code Playgroud)

在规划类中:

/**
 * @ORM\ManyToOne(targetEntity="Zone", inversedBy="planifications")
 * @ORM\JoinColumn(name="zone_id", referencedColumnName="id", nullable=true)
 * @Expose
 */
 protected $zone;
Run Code Online (Sandbox Code Playgroud)

但删除区域时我不需要删除规划。我需要将 zone_id 设置为 null。

任何想法 ?

php doctrine symfony doctrine-orm symfony-2.7

5
推荐指数
1
解决办法
4815
查看次数

如何断言由两个 JSON 字符串表示的数据相等?

我第一次尝试对我的项目使用单元测试,但是我被一个断言模型正确存储的测试阻止了。

这是我要测试的 API 控制器:

public function store(QuestionFormRequest $request)
{
    $questionRequest = $request->question();

    $question = new Question($questionRequest);
    $question->save();

    $question->answers()->createMany($questionRequest['answers']);

    return response()->json($question->load('answers'), 201);
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试:

public function it_can_store_a_question()
{
    $surveyFactory = factory(Survey::class)->create();
    $themeFactory = factory(Theme::class)->create();
    $pillarFactory = factory(Pillar::class)->create();

    $questionToStore = [
        'survey_id' => $surveyFactory->id,
        'theme_id' => $themeFactory->id,
        'pillar_id' => $pillarFactory->id,
        'name' => 'question',
        'type' => 'simple',
        'answers' => [
            [
                'label' => 'reponse1',
                'points' => '3',
            ],
            [
                'label' => 'reponse2',
                'points' => '5',
            ]
        ]
    ];

    $response = $this->post('/api/1.0/question', $questionToStore);
    $response->assertStatus(201); …
Run Code Online (Sandbox Code Playgroud)

phpunit json unit-testing laravel

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