Laravel doctrine2与额外列有很多关系

时间:2018-02-08 15:40:11

标签: php laravel doctrine-orm many-to-many entity-relationship

因此,当涉及到关系有一个额外列的项目的多对多关系时,我开始挣扎于Doctrine2。

我有以下表格:

  • 配置文件
    • id
    • 额外数据
  • 技能

    • ID
    • 名称
  • profile_has_skills

    • PROFILE_ID
    • skill_id也
    • 水平

现在我稍后添加了级别列,并注意到发生了一些问题,当然我每次尝试创建关系时都会丢失级别。 我的问题是,通过下面的代码,我将如何在我的学说中添加这个?

我的控制器:

public function store(Request $request)
{
    $time = new DateTime();

    $this->validate($request, [
        'name' => 'required',
        'lastname' => 'required',
        'gender' => 'required',
        'profile_skills' => 'required'
    ]);

    $this->em->getConnection()->beginTransaction();

    try {
        $profile = new Profile(
            $request->input('company_id'),
            $request->input('name'),
            $request->input('lastname'),
            $request->input('gender'),
            new DateTime(),
            $time,
            $time
        );

        $company = $this->em->getRepository(Company::class)->find($request->input('company_id'));
        $profile->addCompany($company);

        foreach($request->input('profile_skills') as $skill => $level) {
            $skill = $this->em->getRepository(Skill::class)->find($skill);
            $skill->level = $level;
            $profile->addSkill($skill);
        }

        $this->em->persist($profile);
        $this->em->flush();
        $this->em->getConnection()->commit();

    } catch (OptimisticLockException $e) {

        $this->em->getConnection()->rollBack();

        throw $e;
    }

    return redirect(route('profiles.index'));
}

我的ProfileHasSkill实体如下所示:

/**
 * @ORM\Entity
 * @ORM\Table(name="profile_has_skill")
 *
 */
class ProfileHasSkill
{
/**
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 */
protected $id;

/**
 * @Column(type="integer", name="profile_id")
 */
protected $profile_id;


/**
 * @Column(type="integer", name="skill_id")
 */
protected $skill_id;

/**
 * @Column(type="integer", name="level")
 */
protected $level;

/**
 * @param $profile_id
 * @param $skill_id
 * @param $level
 */
public function __construct($profile_id, $skill_id, $level = 0)
{
    $this->profile_id = $profile_id;
    $this->skill_id = $skill_id;
    $this->level = $level;
}

我在配置文件实体中的addSkill方法如下:

public function addSkill(Skill $skill)
{
    if ($this->skills->contains($skill)) {
        return;
    }
    return $this->skills->add($skill);
}

但是无论何时我尝试运行它都会给我以下错误

An exception occurred while executing 
'INSERT INTO profile_has_skill (profile_id, skill_id) VALUES (?, ?)' 
with params [3, 2]: SQLSTATE[HY000]: General error: 1364 Field 'level' 
doesn't have a default value

现在我知道摆脱这个错误的一种方法是在数据库中设置一个默认值,但我更愿意找出为什么它没有提升我的技能水平,我也正在传递?

2 个答案:

答案 0 :(得分:1)

根据我的解决方案,通过阅读@Nicola Havric传递的另一个问题 - Read as follow该学说不支持多对多关系中的额外列。因此,您应该将关系用作自己的实体。我自己的解决方案是改变我希望它用刷新运行的方式。

在我的控制器中,我更改了我的代码如下:

try {
    $profile = new Profile(
        $request->input('company_id'),
        $request->input('name'),
        $request->input('lastname'),
        $request->input('gender'),
        new DateTime(),
        $time,
        $time
    );

    $company = $this->em->getRepository(Company::class)->find($request->input('company_id'));
    $profile->addCompany($company);
    //Flush the user, so I can grab it's profile ID
    $this->em->persist($profile);
    $this->em->flush();

    foreach($request->input('profile_skills') as $skill => $level) {
        $skill = $this->em->getRepository(Skill::class)->find($skill);
        $skill->level = $level;
        $profile->addSkill($skill);
    }


    $this->em->getConnection()->commit();

在我的个人资料实体功能中:

public function addSkill(Skill $skill)
{
    //I left this check since it only checks if the relation is set already. If so, it will skip it.
    if ($this->skills->contains($skill)) {
        return;
    }

    //Since this function gets called inside a loop, I can call the entity to add a new "relation" to the table.
    (new ProfileHasSkill($this->getId(), $skill, $skill->level))->addSkill($this->getId(), $skill, $skill->level);

    return true;
}

在我的ProfileHasSkill实体中:

 public function addSkill($profileId, $skill)
{
    //Creating a new ProfileHasSkill inside the table.
    $profileSkill = new ProfileHasSkill(
        $profileId,
        $skill->getId(),
        $skill->level
    );

    /*Since I do a roll-back inside my controller in case something goes wrong.
       I decided to add the flush here. 
       As far no additional checks where needed in my case 
       since I require a Profile instance and a Skill instance inside the Profile entity.*/
    EntityManager::persist($profileSkill);
    EntityManager::flush();
}

答案 1 :(得分:0)

具有多对多关系的东西是,除两个表中的两个主键之外的任何其他列都被视为 pivot 列,当将实体附加到此类关系时,您希望使用方法< strong> attach ,它接受id数组作为第一个参数,接受带有 pivot 列的数组,请考虑以下因素。

public function addSkill(Skill $skill)
{
    if ($this->skills->contains($skill)) {
        return;
    }
    //Dunno what this method does
    return $this->skills->add($skill);
    //But this is the correct way of adding a skill
    $this->skills->attach($skill->id, ['level' => $skill->level]);
}

希望这可以澄清一些事情,即使以Eloquent为例; here is the manual link代码为上述代码。

相关问题