我已关注One-to-Many relation not working并创建了一对多关系
我有一个用户表,我有以下字段
- id(primary key)
- name
- pwd
我有附件表,用户可以上传多个文件,即一个user_id包含多个文件
- id
- user_id(foreignkey)
- path
我的用户实体包含以下代码
namespace Repair\StoreBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* users
* @ORM\Entity(repositoryClass="Repair\StoreBundle\Entity\usersRepository")
*/
class users
{
/**
* @var integer
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
--some code --
/**
* @ORM\OneToMany(targetEntity="attachments", mappedBy="user_id")
*/
private $attachments;
public function __construct()
{
$this->attachments= new ArrayCollection();
}
/**
* Add attachments
*
* @param \Repair\StoreBundle\Entity\attachments $attachments
* @return users
*/
public function addAttachment(\Repair\StoreBundle\Entity\attachments $attachments)
{
$this->attachments[] = $attachments;
return $this;
}
/**
* Remove attachments
*
* @param \Repair\StoreBundle\Entity\attachments $attachments
*/
public function removeAttachment(\Repair\StoreBundle\Entity\attachments $attachments)
{
$this->attachments->removeElement($attachments);
}
/**
* Get attachments
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAttachments()
{
return $this->attachments;
}
这是我的附件实体
namespace Repair\StoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* attachments
*/
class attachments
{
-- some code for id--
private $id;
/**
* @var integer
* @ORM\Column(name="user_id", type="integer", nullable=false)
* @ORM\ManyToOne(targetEntity="users", inversedBy="users")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
*/
protected $userId;
public function getId()
{
return $this->id;
}
/**
* Set userId
* @param integer $userId
* @return attachments
*/
public function setUserId($userId)
{
$this->userId = $userId;
return $this;
}
/**
* Get userId
*
* @return integer
*/
public function getuserId()
{
return $this->userId;
}
--Some code for paths --
}
没有显示任何错误 但是如何知道foriegn键是否设置我去了phpmyadmin并检查索引它只显示主键。请说明我是否正确以及如何检查外键是否设置
答案 0 :(得分:0)
问题出在您的注释中。在您的附件实体中,您应该有这样的注释。
* @ORM\ManyToOne(targetEntity="users", inversedBy="annotations")
您不必具有连接列注释。但如果你想在那里,它应该是这样的。
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
此外,它不应被称为user_id
而是user
,因为学说将其视为整个实体。