1

我遵循一对多关系不起作用并创建了一对多关系

我有一个用户表,其中有以下字段

 - 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 -- 
}

它没有显示任何错误,但如何知道是否设置了外键我去了 phpmyadmin 并检查了它只显示主键的索引。请说我是否正确以及如何检查是否设置了外键或不

4

1 回答 1

0

问题出在您的注释中。在您的附件实体中,您应该有这样的注释。

 * @ORM\ManyToOne(targetEntity="users", inversedBy="annotations")

您不必加入列注释。但是如果你想在那里拥有它,它应该看起来像这样。

 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")

也不应该将其称为user_idbut user,因为教义将其视为整个实体。

于 2013-05-24T13:36:39.177 回答