Php 无法持久化实体,尽管设置了外键,但外键始终为空
我只是不知道我做错了什么。我正在与Symfony合作,我创建了一个名为“UserProfile”的实体。它有一个用户id和一个帐户id。我在控制器中设置它们,如下所示:Php 无法持久化实体,尽管设置了外键,但外键始终为空,php,symfony,doctrine,Php,Symfony,Doctrine,我只是不知道我做错了什么。我正在与Symfony合作,我创建了一个名为“UserProfile”的实体。它有一个用户id和一个帐户id。我在控制器中设置它们,如下所示: $profile = new UserProfile; $profile->setAccountId($account->getId()) ->setUserId($user->getId()); 然后,我尝试像这样持久化实体: $entityManager->persist($profi
$profile = new UserProfile;
$profile->setAccountId($account->getId())
->setUserId($user->getId());
然后,我尝试像这样持久化实体:
$entityManager->persist($profile);
$entityManager->flush();
如果我转储配置文件,它将显示帐户\u id设置为1,用户\u id设置为17(这正是我所期望的)。然而,当我试图持久化实体时,我得到一个异常,说明account\u id和user\u id不能为null。这是我的实体定义。我只能假设我对它的定义做了一些错误的事情。如果您还需要什么帮助我诊断问题,请告诉我。提前谢谢大家
<?php
namespace Denizen\AppBundle\Entity;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* UserProfile
*
* I have set up a unique index on account_id/user_id so that only one profile may exist per user per account.
*
* @ORM\Entity(repositoryClass="Denizen\AppBundle\Repository\UserProfileRepository")
* @ORM\Table(name="users_profiles", uniqueConstraints={@ORM\UniqueConstraint(name="account_user_profile_unique", columns={"account_id", "user_id"})})
*/
class UserProfile
{
const STATUS_NEW = 'new';
const STATUS_COMPLETED = 'completed';
/**
* @var User
* @ORM\ManyToOne(targetEntity="User", inversedBy="profiles")
*/
protected $user;
/**
* @var Account
* @ORM\ManyToOne(targetEntity="Account", inversedBy="profiles")
*/
protected $account;
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var int
* @ORM\Column(type="integer", name="user_id", nullable=false)
*/
protected $userId;
/**
* @var int
* @ORM\Column(type="integer", name="account_id", nullable=false)
*/
protected $accountId;
/**
* @var string
* @ORM\Column(type="string", name="status", nullable=true)
*/
protected $status;
/** snip **/
/**
* UserProfile constructor.
*/
public function __construct()
{
$this->setStatus(static::STATUS_NEW);
}
public function getUser()
{
return $this->user;
}
public function getAccount()
{
return $this->account;
}
/**
* Get id
*
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @return int
*/
public function getUserId(): int
{
return $this->userId;
}
/**
* @param int $userId
*/
public function setUserId(int $userId)
{
$this->userId = $userId;
return $this;
}
/**
* @return int
*/
public function getAccountId(): int
{
return $this->accountId;
}
/**
* @param int $accountId
*/
public function setAccountId(int $accountId)
{
$this->accountId = $accountId;
return $this;
}
/**
* @return string
*/
public function getStatus(): ?string
{
return $this->status;
}
/**
* @param string $status
* @return UserProfile
*/
public function setStatus(?string $status)
{
$this->status = $status;
return $this;
}
/** snip **/
}
实际上,您不必定义属性$userId和$accountId,因为您已经定义了$user和$account属性,而且在定义中,您还应该告诉symfony连接列名。
例如,如果数据库架构中的“profile”表有一个“user\u id”列,则应按如下方式映射它:
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="profiles")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
*/
protected $user;
假设表“profile”和“user”的列“user\u id”的名称相同
然后创建UserProfile的实例
$userProfile = new UserProfile();
$userProfile->setUser($someExistingUser);
$entityManager->persist($userProfile);
$entityManager->flush();
此外,如果您希望稍后检索用户id,则应按如下操作
$userId = $userProfile->getUser()->getId();
根据实体中getter和setter方法的名称是否尝试在用户和帐户实体上设置“mappedBy”属性?可能是条令不知道这些实体之间的关系。您的实体设置不正确。您不想在那里有$userId
或$accountId
,它们已经由$user
和$account
处理了。那么这是否意味着我没有设置$profile->setAccountId($account->getId())
,而是设置$profile->setAccount($account)
?如果是这样,那就意味着我必须手动创建这些方法,对吗?谢谢大家,你们帮了我很大的忙。你已经解决了我的问题。