i'm trying to create a OneToMany form, and the symfony profiler throws an entity error, here's the code:
Tasks.php
<?php
namespace ShopBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
*
*
* @ORM\Entity
* @ORM\Table(name="task")
*/
class Tasks
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", nullable=true,length=100)
*
*/
protected $description;
/**
* @ORM\OneToMany(targetEntity="ShopBundle\Entity\Tags", cascade={"all"}, mappedBy="name")
*/
protected $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getDescription()
{
return $this->description;
}
public function setDescription($description)
{
$this->description = $description;
}
public function getTags()
{
return $this->tags;
}
public function addTag(Tags $tag)
{
$this->tags->add($tag);
}
public function removeTag(Tags $tag)
{
$this->tags->removeElement($tag);
}
}
this is the Tags.php
<?php
namespace ShopBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
*
* @ORM\Entity
* @ORM\Table(name="tag")
*/
class Tags
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="ShopBundle\Entity\Tasks", inversedBy="tags")
* @ORM\JoinColumn(name="task")
*/
protected $name;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
}
the profiler throws 2 errors: The association ShopBundle\\Entity\\Tasks#tags refers to the owning side field ShopBundle\\Entity\\Tags#name which is not defined as association, but as field.
and
The association ShopBundle\\Entity\\Tasks#tags refers to the owning side field ShopBundle\\Entity\\Tags#name which does not exist.
it saves the data into the database, but i can't get the data from the database
Firstly check what debug CLI tells you:
php bin/console doctrine:schema:validate
Look at this example from Doctrine docs One-To-Many, Bidirectional
In Tags@JoinColumn
you should add also field name of fields in entity Tasks
, which you want to be connected with. ( referencedColumnName
)
Also I can see that $name
is not a field, where you want to keep this mapped entity? Try to add another field with name of entity instead of putting annotations before $name
(btw. it's also misleading to expect Tag name, but getting Entity Task
).
For example :
// Tags.php
/**
* @ManyToOne(targetEntity="ShopBundle\Entity\Tasks", inversedBy="tags")
* @JoinColumn(name="task_id", referencedColumnName="id")
*/
protected $task;
Then remember to change also mapping in Tasks
entity:
// Tasks.php
/**
* @ORM\OneToMany(targetEntity="ShopBundle\Entity\Tags",
* cascade={"all"}, mappedBy="task")
*/
protected $tags;
Finally update your schema:
php bin/console doctrine:schema:update --force
Hope it helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.