我对学说有疑问。我有两个实体。
FriendRequest
class FriendRequest
{
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue **/
protected $id;
/**
* First person from friendship
*
* @ORM\OneToOne(targetEntity="User")
* @ORM\JoinColumn(name="id", referencedColumnName="id")
*/
protected $from;
/**
* Second person from friendship
*
* @ORM\OneToOne(targetEntity="User")
* @ORM\JoinColumn(name="id", referencedColumnName="id")
*/
protected $to;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getFrom()
{
return $this->from;
}
/**
* @param mixed $from
*/
public function setFrom($from)
{
$this->from = $from;
}
/**
* @return mixed
*/
public function getTo()
{
return $this->to;
}
/**
* @param mixed $to
*/
public function setTo($to)
{
$this->to = $to;
}
}
和用户
/**
* @ORM\Entity @ORM\Table(name="users")
*/
class User
{
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue **/
protected $id;
/** @ORM\Column(type="string") **/
protected $email;
/** @ORM\Column(type="string") **/
protected $password;
/** @ORM\Column(type="string") **/
protected $name;
/** @ORM\Column(type="string") **/
protected $surname;
/** @ORM\Column(type="date") **/
protected $date;
/** @ORM\Column(type="string") **/
protected $sex;
/**
* @return mixed
*/
public function getSex()
{
return $this->sex;
}
/**
* @param mixed $sex
*/
public function setSex($sex)
{
$this->sex = $sex;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* @param mixed $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* @return mixed
*/
public function getPassword()
{
return $this->password;
}
/**
* @param mixed $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getSurname()
{
return $this->surname;
}
/**
* @param mixed $surname
*/
public function setSurname($surname)
{
$this->surname = $surname;
}
/**
* @return mixed
*/
public function getDate()
{
return $this->date;
}
/**
* @param mixed $date
*/
public function setDate($date)
{
$this->date = $date;
}
}
我试图创建新的友谊请求。
$from = $this->entity_manager->getRepository(User::class)->findOneBy(
["id" => $my_id]
);
$to = $this->entity_manager->getRepository(User::class)->findOneBy(
["id" => $target_user_id]
);
/** @var FriendRequest $friend_request */
$friend_request = new FriendRequest();
$friend_request->setFrom($from);
$friend_request->setTo($to);
$this->entity_manager->persist($friend_request);
$this->entity_manager->flush();
但是它不起作用。教义告诉我错误:
使用参数[186]执行'INSERT INTO friendRequest(id)VALUES(?)'时发生异常:
SQLSTATE [HY000]:常规错误:1364字段'from'没有默认值。
我尝试打印$ from广告$ to,但它们是正确的。有人知道这是什么意思吗?我花了很多时间,但是我不知道...
答案 0 :(得分:0)
我认为您的问题来自于FriendRequest实体类中$ from和$ to属性的声明,如果您尝试更新数据库模型,将会收到如下错误:
[Doctrine \ ORM \ Mapping \ MappingException]
字段或区分符列映射中实体“ FriendRequest”上列“ id”的重复定义。
由于您已经为两个不同的属性分配了相同的名称“ id”。数据库中可能有两列“从”和“到”,但是它们没有与模型映射,这就是为什么您收到一条错误消息,指出“从”没有默认值。由于数据库中的列名称与您实体中的属性名称不同步。
假设您已经有一个FriendRequest表,并且您使用“ from”和“ to”作为列名,这应该是FriendRequest实体类的更新。
class FriendRequest
{
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue **/
protected $id;
/**
* First person from friendship
*
* @ORM\OneToOne(targetEntity="User")
* @ORM\JoinColumn(name="from", referencedColumnName="id")
*/
protected $from;
/**
* Second person from friendship
*
* @ORM\OneToOne(targetEntity="User")
* @ORM\JoinColumn(name="to", referencedColumnName="id")
*/
protected $to;
...
}