我是Symfony的新手,在表中保存外键时遇到问题。我尽量解释清楚。
我正在申请待办事项。-每个用户都有一个配置文件-每个配置文件都有许多项目-任何用户都可以向列表中添加新项目-当用户创建新项目时,profile_id应保存在项目表中。但是我得到了一个错误。我尝试了不同的方法来解决这个问题,但我找不到解决办法。我希望你能帮我。
这是我的个人资料实体
<?php
namespace AppEntity;
use DateTime;
use DateTimeInterface;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryProfileRepository")
*/
class Profile
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $firstName;
/**
* @ORMColumn(type="string", length=255)
*/
private $lastName;
/**
* @ORMOneToOne(targetEntity="AppEntityUser", inversedBy="profile", cascade={"persist", "remove"})
* @ORMJoinColumn(nullable=false)
*/
private $user;
/**
* @ORMColumn(type="string", length=255)
*/
private $gender;
/**
* @ORMColumn(type="string", length=255)
*/
private $initials;
/**
* @ORMColumn(type="string", length=255)
*/
private $country;
/**
* @ORMColumn(type="string", length=255)
*/
private $city;
/**
* @ORMColumn(type="string", length=255)
*/
private $postalCode;
/**
* @ORMColumn(type="string", length=255)
*/
private $street;
/**
* @ORMColumn(type="string", length=255)
*/
private $houseNumber;
/**
* @ORMColumn(type="string", length=255, nullable=true)
*/
private $houseNumberAddition;
/**
* @ORMColumn(type="string", length=255, nullable=true)
*/
private $phoneNumber;
/**
* @ORMColumn(type="string", length=255, nullable=true)
*/
private $mobileNumber;
/**
* @ORMColumn(type="boolean")
*/
private $accepted;
/**
* @ORMColumn(type="boolean")
*/
private $visible;
/**
* @ORMColumn(type="datetime")
*/
private $createdAt;
/**
* @ORMColumn(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORMOneToMany(targetEntity="AppEntityItems", mappedBy="profile")
*/
private $items;
public function __construct()
{
$this->items = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(User $user): self
{
$this->user = $user;
return $this;
}
public function getGender(): ?string
{
return $this->gender;
}
public function setGender(string $gender): self
{
$this->gender = $gender;
return $this;
}
public function getInitials(): ?string
{
return $this->initials;
}
public function setInitials(string $initials): self
{
$this->initials = $initials;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(string $country): self
{
$this->country = $country;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
public function getPostalCode(): ?string
{
return $this->postalCode;
}
public function setPostalCode(string $postalCode): self
{
$this->postalCode = $postalCode;
return $this;
}
public function getStreet(): ?string
{
return $this->street;
}
public function setStreet(string $street): self
{
$this->street = $street;
return $this;
}
public function getHouseNumber(): ?string
{
return $this->houseNumber;
}
public function setHouseNumber(string $houseNumber): self
{
$this->houseNumber = $houseNumber;
return $this;
}
public function getHouseNumberAddition(): ?string
{
return $this->houseNumberAddition;
}
public function setHouseNumberAddition(?string $houseNumberAddition): self
{
$this->houseNumberAddition = $houseNumberAddition;
return $this;
}
public function getPhoneNumber(): ?string
{
return $this->phoneNumber;
}
public function setPhoneNumber(?string $phoneNumber): self
{
$this->phoneNumber = $phoneNumber;
return $this;
}
public function getMobileNumber(): ?string
{
return $this->mobileNumber;
}
public function setMobileNumber(?string $mobileNumber): self
{
$this->mobileNumber = $mobileNumber;
return $this;
}
public function getAccepted(): ?bool
{
return $this->accepted;
}
public function setAccepted(bool $accepted): self
{
$this->accepted = $accepted;
return $this;
}
public function getVisible(): ?bool
{
return $this->visible;
}
public function setVisible(bool $visible): self
{
$this->visible = $visible;
return $this;
}
/**
* @return DateTimeInterface|null
*/
public function getCreatedAt(): ?DateTimeInterface
{
return $this->createdAt;
}
/**
* @param DateTimeInterface $createdAt
* @return $this
*/
public function setCreatedAt(DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return DateTimeInterface|null
*/
public function getUpdatedAt(): ?DateTimeInterface
{
return $this->updatedAt;
}
/**
* @param DateTimeInterface|null $updatedAt
* @return $this
*/
public function setUpdatedAt(?DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @ORMPrePersist()
*/
public function onPrePersist()
{
$this->createdAt = new DateTime("now");
}
/**
* @ORMPreUpdate()
*/
public function onPreUpdate()
{
$this->updatedAt = new DateTime("now");
}
/**
* @return Collection|Items[]
*/
public function getItems(): Collection
{
return $this->items;
}
public function addItem(Items $item): self
{
if (!$this->items->contains($item)) {
$this->items[] = $item;
$item->setProfile($this);
}
return $this;
}
public function removeItem(Items $item): self
{
if ($this->items->contains($item)) {
$this->items->removeElement($item);
// set the owning side to null (unless already changed)
if ($item->getProfile() === $this) {
$item->setProfile(null);
}
}
return $this;
}
}
这是我的项目实体
<?php
namespace AppEntity;
use DateTime;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryItemsRepository")
* @ORMHasLifecycleCallbacks
*/
class Items
{
/**
* @var int
*
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMManyToOne(targetEntity="AppEntityProfile", inversedBy="items")
* @ORMJoinColumn(nullable=false)
*/
private $profile;
/**
* @var string
*
* @ORMColumn(type="string", length=255, nullable=false)
*/
private $title;
/**
* @var string
*
* @ORMColumn(type="text", nullable=false)
*/
private $description;
/**
* @var datetime $createdAt
*
* @ORMColumn(type="datetime", nullable=false)
*/
private $createdAt;
/**
* @var datetime $updatedAt
*
* @ORMColumn(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
* @return Items
*/
public function setId(int $id): Items
{
$this->id = $id;
return $this;
}
public function getProfile(): ?Profile
{
return $this->profile;
}
public function setProfile(?Profile $profile): self
{
$this->profile = $profile;
return $this;
}
/**
* @return string
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* @param string $title
* @return Items
*/
public function setTitle(string $title): Items
{
$this->title = $title;
return $this;
}
/**
* @return string
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* @param string $description
* @return Items
*/
public function setDescription(string $description): Items
{
$this->description = $description;
return $this;
}
/**
* @return DateTime
*/
public function getCreatedAt(): DateTime
{
return $this->createdAt;
}
/**
* @param DateTime $createdAt
* @return Items
*/
public function setCreatedAt(DateTime $createdAt): Items
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return DateTime
*/
public function getUpdatedAt(): DateTime
{
return $this->updatedAt;
}
/**
* @param DateTime $updatedAt
* @return Items
*/
public function setUpdatedAt(DateTime $updatedAt): Items
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @ORMPrePersist()
*/
public function onPrePersist()
{
$this->createdAt = new DateTime("now");
}
/**
* @ORMPreUpdate()
*/
public function onPreUpdate()
{
$this->updatedAt = new DateTime("now");
}
}
这是我的物品控制器,在这里可以存储新的物品
/**
* @Route("/item/create", name="create_item")
* @return Response
*/
public function create()
{
$form = $this->createForm(CreateTodoFormType::class, null, ['action' => $this->generateUrl('store_item')]);
return $this->render('item/create.html.twig', [
'todoForm' => $form->createView(),
]);
}
/**
* @Route("/item/store", name="store_item", methods={"POST"})
* @param EntityManagerInterface $em
* @param Request $request
* @param TranslatorInterface $translator
* @return RedirectResponse
*/
public function store(EntityManagerInterface $em, Request $request, TranslatorInterface $translator)
{
$form = $this->createForm(CreateTodoFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$profile = $em->getRepository(Profile::class)->findBy(['user' => $this->getUser()->getId()]);
$item = new Items();
$item->setTitle($data->getTitle());
$item->setDescription($data->getDescription());
$item->setProfile($profile);
$em->persist($item);
$em->flush();
return $this->redirectToRoute('item');
}
return $this->redirectToRoute('create_item', [
'todoForm' => $form->createView(),
]);
}
当我这样做的时候,我会出错!
传递给App\Entity\Items::setProfile((的参数1必须是App\Entity\Profile的实例或null,给定数组,在第79行的C:\examplep\htdocs\projects\SymfonyProjects\symfony_to_do\src\Controller\ItemController.php中调用
$profile = $em->getRepository(Profile::class)->findBy(['user' => $this->getUser()->getId()]);
这个请求返回一个数组,即使它只包含一个元素。您可以将$profile[0]用于第一个元素或更好的元素:使用findOneBy方法!