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();