doutao6380 2019-06-04 14:22
浏览 57

使用非自动生成的ID链接两个实体

I'm doing a website where clients connect to it with an ID that is linked to a machine that generate data. This machine send that data into a csv file with the same ID into it. I want to show the data on the website so that the client can see their own data and not the other's. I created my two entities with their properties. In Property class, I have the relation ManyToOne since a User can have multiple properties and a property can only be linked to one user.

Property class:

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="properties")
 * @ORM\JoinColumn(nullable=false)
 */
private $user;

public function getUser(): ?User
{
    return $this->user;
}

public function setUser(?User $user): self
{
    $this->user = $user;

    return $this;
}

User class:

private $username; /*This is the ID of the machine in User, people connect 
                     to the website with it*/

/**
 * @ORM\OneToMany(targetEntity="App\Entity\Property", mappedBy="user")
 */
private $properties;

public function __construct()
{
    $this->properties = new ArrayCollection();
}

Auto-generated getters and setters in User:

public function getUsername(): ?string
{
    return $this->username;
}

public function setUsername(string $username): self
{
    $this->username = $username;

    return $this;
}

Auto-generated property methods in User:

/**
 * @return Collection|Property[]
 */
public function getProperties(): Collection
{
    return $this->properties;
}

public function addProperty(Property $property): self
{
    if (!$this->properties->contains($property)) {
        $this->properties[] = $property;
        $property->setUser($this);
    }

    return $this;
}

public function removeProperty(Property $property): self
{
    if ($this->properties->contains($property)) {
        $this->properties->removeElement($property);
        // set the owning side to null (unless already changed)
        if ($property->getUser() === $this) {
            $property->setUser(null);
        }
    }

    return $this;
}

Here's how I try to get the data from the csv file with the ID in my csv class:

    /*Read csv file and put the result array in $results*/

    foreach ($results as $row) {

        $properties = (new Property)
            ->set/*Some properties*/
            ->setUser($row['user'])
            ->setDate(new \DateTime());

            $this->em->persist($properties);
    }
    $this->em->flush();

Obviously, this doesn't work because $user is not initialized and I want to be able to get the ID string instead of $user object. The problem is that I don't know how to get the string and link it with the User. Do I just have to make an if statement in the twig file to show only what I want or is there a nice way to do it?

Edit

I tried something but it didn't change anything. I tried linking the property to a User when it's created but it doesn't seem to do anything when I'm rendering the page. I still get every property and not only the one I'm suppose to get. (wich is the ones that have the same ID as the User).

Here's the code

    $properties = (new Property)
            ->set/*Some properties*/
            ->setmachineId($row['machineId']) /*Changed this to string in entity class*/
            ->setDate(new \DateTime());

            $this->em->persist($properties);
    }

    $users = $this->em->getRepository(User::class);
    foreach($users as $user){
        if($properties->getMachineId() === $user->getUsername()){
            $user->addProperty($properties);
            $properties->setUser($user);
        }  
    }

    $this->em->flush();
  • 写回答

2条回答 默认 最新

  • doutuo3899 2019-06-04 16:19
    关注

    This?

    foreach ($results as $row) {
    
        $properties = (new Property)
            ->set/*Some properties*/
            ->setUser($this->em->getReference(User, $row['user']))
            ->setDate(new \DateTime());
    
            $this->em->persist($properties);
    }
    $this->em->flush();
    

    documentation

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP