doctrine 相关问题

Doctrine Project是一个开源库和工具的集合,用于处理用PHP编写的数据库抽象和对象关系映射。

给出了“Doctrine\Common\Collections\ArrayCollection”、“Doctrine\ORM\PersistentCollection”类型的预期参数

我在标签和文章实体之间有多对多关系,插入效果很好,但编辑表单(editAction 函数)的创建不起作用。 所有代码都在那里: 艺术...

回答 2 投票 0

Symfony 使用 AdvancedUserInterface

我正在尝试用Symfony2实现一个登录系统。我正在为我的用户实体实现 AdvancedUserInterface,并且当我尝试登录时不断收到以下错误。任何帮助都是值得赞赏的...

回答 2 投票 0

Symfony 6.4 - Doctrine - Xml - 没有可用的匹配全局属性声明,但严格通配符要求

composer.json "学说/学说捆绑": "^2.11.1", "学说/学说-迁移-bundle": "^3.3.0", “教义/规则”:“...

回答 1 投票 0

Doctrine:如何手动管理带有实体的表和其他表?

我有一个应用程序,其数据库按实体学说进行管理。 我的应用程序需要动态创建没有实体的表。 (以编程方式) 当我执行doctrine:schema:update comma...

回答 1 投票 0

Setter 和 getter 无法从 Symfony2 中的自定义用户实体访问

我正在尝试实现 hwioauthbundle 来与 Google 连接。 然而,我面临着 symfony 似乎无法找到 User 实体中声明的方法的问题 - 我相信它有一些......

回答 1 投票 0

当用户不提供值时表单将其清空

阅读此页,我已经设置了一个表单来处理 PATCH 请求。 我有一个 Player 实体: 阅读this页面我已经设置了一个表单来处理 PATCH 请求。 我有一个玩家实体: <?php namespace Acme\PlayerBundle\Entity; use Symfony\Component\Validator\Constraints as Assert; use Doctrine\ORM\Mapping as ORM; /** * Acme\PlayerBundle\Entity\Player * * @ORM\Table() * @ORM\Entity(repositoryClass="Acme\PlayerBundle\Entity\PlayerRepository") */ class Player { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string $name * * @ORM\Column(name="name", type="string", length=255) * @Assert\NotBlank() */ private $name; /** * @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy="players") * @ORM\JoinColumn(nullable=false) */ private $owner; /** * @ORM\ManyToOne(targetEntity="Acme\TeamBundle\Entity\Team", inversedBy="players") * @ORM\JoinColumn(nullable=false) * @Assert\NotBlank() */ private $team; /** * @var integer $shirtNumber * * @ORM\Column(name="shirtNumber", type="smallint") * @Assert\NotBlank() */ private $shirtNumber; /** * @var integer $vsid * * @ORM\Column(name="vsid", type="integer", nullable=true) */ private $vsid; /** * @var string $firstname * * @ORM\Column(name="firstname", type="string", length=255, nullable=true) */ private $firstname; /** * @var string $lastname * * @ORM\Column(name="lastname", type="string", length=255, nullable=true) */ private $lastname; /** * @var boolean $deleted * * @ORM\Column(name="deleted", type="boolean") */ private $deleted = false; /** * @var integer $role * * @ORM\Column(type="integer", nullable=true) */ private $role; /** * Create the user salt */ public function __construct() { //TODO: just for test $this->uniqueId = substr(uniqid(), 0, 14); } /* MANAGED BY DOCTRINE, DON'T EDIT */ /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set name * * @param string $name * @return Player */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set shirtNumber * * @param integer $shirtNumber * @return Player */ public function setShirtNumber($shirtNumber) { $this->shirtNumber = $shirtNumber; return $this; } /** * Get shirtNumber * * @return integer */ public function getShirtNumber() { return $this->shirtNumber; } /** * Set vsid * * @param integer $vsid * @return Player */ public function setVsid($vsid) { $this->vsid = $vsid; return $this; } /** * Get vsid * * @return integer */ public function getVsid() { return $this->vsid; } /** * Set firstname * * @param string $firstname * @return Player */ public function setFirstname($firstname) { $this->firstname = $firstname; return $this; } /** * Get firstname * * @return string */ public function getFirstname() { return $this->firstname; } /** * Set lastname * * @param string $lastname * @return Player */ public function setLastname($lastname) { $this->lastname = $lastname; return $this; } /** * Get lastname * * @return string */ public function getLastname() { return $this->lastname; } /** * Set deleted * * @param boolean $deleted * @return Player */ public function setDeleted($deleted) { $this->deleted = $deleted; return $this; } /** * Get deleted * * @return boolean */ public function getDeleted() { return $this->deleted; } /** * Set role * * @param integer $role * @return Player */ public function setRole($role) { $this->role = $role; return $this; } /** * Get role * * @return integer */ public function getRole() { return $this->role; } /** * Set owner * * @param Acme\UserBundle\Entity\User $owner * @return Player */ public function setOwner(\Acme\UserBundle\Entity\User $owner) { $this->owner = $owner; return $this; } /** * Get owner * * @return Acme\UserBundle\Entity\User */ public function getOwner() { return $this->owner; } /** * Set team * * @param Acme\TeamBundle\Entity\Team $team * @return Player */ public function setTeam(\Acme\TeamBundle\Entity\Team $team) { $this->team = $team; return $this; } /** * Get team * * @return Acme\TeamBundle\Entity\Team */ public function getTeam() { return $this->team; } } 和团队实体: <?php namespace Acme\TeamBundle\Entity; use Symfony\Component\Validator\Constraints as Assert; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** * Acme\TeamBundle\Entity\Team * * @ORM\Table() * @ORM\Entity(repositoryClass="Acme\TeamBundle\Entity\TeamRepository") */ class Team { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string $uniqueId * * @ORM\Column(name="uniqueId", type="string", length=15) */ private $uniqueId; /** * @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy="teams") * @ORM\JoinColumn(nullable=false) */ private $owner; /** * @var string $name * * @ORM\Column(name="name", type="string", length=50) * @Assert\NotBlank() */ private $name; /** * @var string $homeColor * * @ORM\Column(name="homeColor", type="string", length=7, nullable=true) */ private $homeColor; /** * @var string $awayColor * * @ORM\Column(name="awayColor", type="string", length=7, nullable=true) */ private $awayColor; /** * @var string $homeShirt * * @ORM\Column(name="homeShirt", type="string", length=50, nullable=true) */ private $homeShirt; /** * @var string $awayShirt * * @ORM\Column(name="awayShirt", type="string", length=50, nullable=true) */ private $awayShirt; /** * @var string $teamLogo * * @ORM\Column(name="teamLogo", type="string", length=50, nullable=true) */ private $teamLogo; /** * @var boolean $deleted * * @ORM\Column(name="deleted", type="boolean") */ private $deleted = false; /** * @ORM\OneToMany(targetEntity="Acme\PlayerBundle\Entity\Player", mappedBy="team", cascade={"persist", "remove"}) */ private $players; /** * @ORM\OneToMany(targetEntity="Acme\MatchBundle\Entity\Match", mappedBy="homeTeam") */ private $homeMatches; /** * @ORM\OneToMany(targetEntity="Acme\MatchBundle\Entity\Match", mappedBy="awayTeam") */ private $awayMatches; /** * Create the user salt */ public function __construct() { $this->players = new ArrayCollection(); $this->homeMatches = new ArrayCollection(); $this->awayMatches = new ArrayCollection(); //TODO: just for test $this->uniqueId = substr(uniqid(), 0, 14); } public function getMatches() { return array_merge($this->awayMatches->toArray(), $this->homeMatches->toArray()); } /* MANAGED BY DOCTRINE, DON'T EDIT */ /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set uniqueId * * @param string $uniqueId * @return Team */ public function setUniqueId($uniqueId) { $this->uniqueId = $uniqueId; return $this; } /** * Get uniqueId * * @return string */ public function getUniqueId() { return $this->uniqueId; } /** * Set name * * @param string $name * @return Team */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set homeColor * * @param string $homeColor * @return Team */ public function setHomeColor($homeColor) { $this->homeColor = $homeColor; return $this; } /** * Get homeColor * * @return string */ public function getHomeColor() { return $this->homeColor; } /** * Set awayColor * * @param string $awayColor * @return Team */ public function setAwayColor($awayColor) { $this->awayColor = $awayColor; return $this; } /** * Get awayColor * * @return string */ public function getAwayColor() { return $this->awayColor; } /** * Set homeShirt * * @param string $homeShirt * @return Team */ public function setHomeShirt($homeShirt) { $this->homeShirt = $homeShirt; return $this; } /** * Get homeShirt * * @return string */ public function getHomeShirt() { return $this->homeShirt; } /** * Set awayShirt * * @param string $awayShirt * @return Team */ public function setAwayShirt($awayShirt) { $this->awayShirt = $awayShirt; return $this; } /** * Get awayShirt * * @return string */ public function getAwayShirt() { return $this->awayShirt; } /** * Set teamLogo * * @param string $teamLogo * @return Team */ public function setTeamLogo($teamLogo) { $this->teamLogo = $teamLogo; return $this; } /** * Get teamLogo * * @return string */ public function getTeamLogo() { return $this->teamLogo; } /** * Set deleted * * @param boolean $deleted * @return Team */ public function setDeleted($deleted) { $this->deleted = $deleted; return $this; } /** * Get deleted * * @return boolean */ public function getDeleted() { return $this->deleted; } /** * Add players * * @param Acme\PlayerBundle\Entity\Player $players * @return Team */ public function addPlayer(\Acme\PlayerBundle\Entity\Player $players) { $this->players[] = $players; return $this; } /** * Remove players * * @param Acme\PlayerBundle\Entity\Player $players */ public function removePlayer(\Acme\PlayerBundle\Entity\Player $players) { $this->players->removeElement($players); } /** * Get players * * @return Doctrine\Common\Collections\Collection */ public function getPlayers() { return $this->players; } /** * Add homeMatches * * @param Acme\MatchBundle\Entity\Match $homeMatches * @return Team */ public function addHomeMatche(\Acme\MatchBundle\Entity\Match $homeMatches) { $this->homeMatches[] = $homeMatches; return $this; } /** * Remove homeMatches * * @param Acme\MatchBundle\Entity\Match $homeMatches */ public function removeHomeMatche(\Acme\MatchBundle\Entity\Match $homeMatches) { $this->homeMatches->removeElement($homeMatches); } /** * Get homeMatches * * @return Doctrine\Common\Collections\Collection */ public function getHomeMatches() { return $this->homeMatches; } /** * Add awayMatches * * @param Acme\MatchBundle\Entity\Match $awayMatches * @return Team */ public function addAwayMatche(\Acme\MatchBundle\Entity\Match $awayMatches) { $this->awayMatches[] = $awayMatches; return $this; } /** * Remove awayMatches * * @param Acme\MatchBundle\Entity\Match $awayMatches */ public function removeAwayMatche(\Acme\MatchBundle\Entity\Match $awayMatches) { $this->awayMatches->removeElement($awayMatches); } /** * Get awayMatches * * @return Doctrine\Common\Collections\Collection */ public function getAwayMatches() { return $this->awayMatches; } /** * Set owner * * @param Acme\UserBundle\Entity\User $owner * @return Team */ public function setOwner(\Acme\UserBundle\Entity\User $owner) { $this->owner = $owner; return $this; } /** * Get owner * * @return Acme\UserBundle\Entity\User */ public function getOwner() { return $this->owner; } } 现在我已经使用应用程序/控制台创建了一个玩家表单类,并且我已将团队字段编辑为团队实体的实例,如下所示: <?php namespace Acme\PlayerBundle\Form; use Acme\TeamBundle\Entity\TeamRepository; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class PlayerType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name') ->add('shirtNumber') ->add('firstname') ->add('lastname') ->add('role') ->add('team', 'entity', array( 'class' => 'AcmeTeamBundle:Team', 'query_builder' => function(TeamRepository $er) { $query = $er->createQueryBuilder('t'); return $query; } )); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Acme\PlayerBundle\Entity\Player', 'csrf_protection' => false )); } public function getName() { return 'player'; } } 这是我的控制器的相关部分: /** * Create a new player * * @Route(".{_format}", name="api_player_create") * @Method("POST") * @ApiDoc( * description="Create a new player", * statusCodes={ * 201="Player created and informations are returned", * 400="Missing informations", * 403="The user isn't authorized" * }, * input="Acme\PlayerBundle\Form\PlayerType", * return="Acme\PlayerBundle\Entity\Player" * ) * * @return Renders the player just created */ public function createPlayerAction() { return $this->processForm(new Player()); } /** * Edit a player * * @param integer $id The id of the player to be created * * @Route("/{id}.{_format}", name="api_player_patch", requirements={ "id": "\d+" }) * @Method("PATCH") * @ApiDoc( * description="Edit a player", * statusCodes={ * 200="Player is updated", * 400="Missing informations", * 403="The user isn't authorized" * }, * input="Acme\PlayerBundle\Form\PlayerType", * return="Acme\PlayerBundle\Entity\Player" * ) * * @return Renders the player just edited */ public function editPlayerAction(Player $player) { if ($player->getOwner() != $this->getUser()) { throw new ApiException\PermissionDeniedException; } return $this->processForm($player); } /** * Function to handle a form to create/edit a player * * @param Player $player The player to be created or edited * * @return Api Response */ private function processForm(Player $player) { /** * Check if the player is new (to be created) or we're editing a player */ $statusCode = is_null($player->getId()) ? 201 : 200; $form = $this->createForm(new PlayerType(), $player); $form->bind($this->getRequest()); if ($form->isValid()) { if($player->getTeam()->getOwner() != $this->getUser()) { throw new ApiException\PermissionDeniedException; } $player->setOwner($this->getUser()); $this->entityManager->persist($player); $this->entityManager->flush(); return $this->apiResponse->getResponse($player, $statusCode); } return $this->apiResponse->getResponse($form, 400, 'Missing arguments'); } 播放器创建工作正常,播放器编辑则不然,当用户发出 api 请求时,传递 url 中的 ID 和我得到的播放器名称: Catchable Fatal Error: Argument 1 passed to Acme\PlayerBundle\Entity\Player::setTeam() must be an instance of Acme\TeamBundle\Entity\Team, null given, called in /Volumes/Dati/Users/alessandro/Sites/acme-api/vendor/symfony/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 538 and defined in /Volumes/Dati/Users/alessandro/Sites/acme-api/src/Acme/PlayerBundle/Entity/Player.php line 278 似乎表单试图将团队设置为空,为什么? 我尝试过发送而不是团队作为表单参数,但它不起作用。 有什么线索吗? 发现表单中的 null 未发送字段是 symfony 中的默认行为。 在 symfony 中存在对部分表单绑定的开放请求。现在,一个人创建了一个表单事件订阅者,它用实际值添加了缺失的字段: https://gist.github.com/3720535 这是他的代码: <?php namespace Foo; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormEvent; /** * Changes Form->bind() behavior so that it treats not set values as if they * were sent unchanged. * * Use when you don't want fields to be set to NULL when they are not displayed * on the page (or to implement PUT/PATCH requests). */ class PatchSubscriber implements EventSubscriberInterface { public function onPreBind(FormEvent $event) { $form = $event->getForm(); $clientData = $event->getData(); $clientData = array_replace($this->unbind($form), $clientData ?: array()); $event->setData($clientData); } /** * Returns the form's data like $form->bind() expects it */ protected function unbind($form) { if ($form->hasChildren()) { $ary = array(); foreach ($form->getChildren() as $name => $child) { $ary[$name] = $this->unbind($child); } return $ary; } else { return $form->getClientData(); } } static public function getSubscribedEvents() { return array( FormEvents::PRE_BIND => 'onPreBind', ); } } 要添加到表单,您必须编辑表单类中的 buildForm 方法: public function buildForm(FormBuilderInterface $builder, array $options) { $subscriber = new PatchSubscriber(); $builder->addEventSubscriber($subscriber); $builder->add('name'); .... } 在这种情况下,您可以使用 PATCH REST 请求仅通过发送的字段来编辑实体

回答 1 投票 0

Symfony Doctrine 对于数据库中的同一列有两个属性

这是一个示例实体 使用 ApiPlatform\Metadata\ApiResource; 使用 Doctrine\ORM\Mapping 作为 ORM; 使用 Symfony\Component\Serializer\Annotation\Groups; #[ORM\Table(名称: '用户')] #[API资源( 操作...

回答 1 投票 0

Symfony Doctrine 迁移破坏了 PostgreSQL 中的主键顺序

我一直在使用 pgloader 从 MariaDB 迁移到 PostgreSQL 如果我执行 pgloader 来迁移数据库并加载我的程序,一切都会正常;我可以插入一个新帖子作为示例 然后我尝试...

回答 1 投票 0

Symfony Doctrine 迁移破坏了 Postgre 中的主键序列

我一直在使用 pg loader 从 MariaDB 迁移到 Postgre 如果我执行 pgloader 来迁移数据库并加载我的程序,一切都会正常;我可以插入一个新帖子作为示例 然后我尝试...

回答 1 投票 0

当使用 ORM 验证约束时,Symfony 表单验证将无效子表单设置为有效

情况 我有一种带有子表单的表单类型,我使用选项卡在前端呈现,因此一次只能显示一个子表单(作为选项卡)。因此,当不同选项卡上发生错误时,我弹出...

回答 1 投票 0

使用 Symfony 2.8 框架连接动态实体收集器中的表

在这种情况下我得到了一个 Product 数组。产品链接到类别。类别没有自己的数组,而是放在产品数组旁边。 使用的框架是 Symfony ...

回答 1 投票 0

Symfony,在远程主机中:错误 500。“sfGuardUser”上的未知记录属性/相关组件“算法”

部署后,登录后出现以下错误。 Sf 1.3,sfDoctrineGuardPlugin。我在 config/doctrine 中有这个 schema.yml : 乌苏阿里奥: 遗产: 扩展:sfGuardUser 类型: si...

回答 1 投票 0

symfony 验证码错误重定向

我正在使用 sfExtraForm 插件验证码在 symfony 中构建一个简单的表单邮件程序。一切正常,除了 sfValidator 不会重定向用户并在任何字段无效时显示错误。唯一的

回答 1 投票 0

如何使用已弃用的捆绑包恢复旧的 Symfony 网站?

我目前面临一个具有挑战性的问题,需要一些建议。 一位客户联系我,希望将 Gutenberg API 集成到他们使用 Symfony 构建的网站中。网站已部署并且

回答 1 投票 0

遵循 Doctrine 查询的关系

我尝试学习 Symfony 速成课程,但我被 ATM 困住了。结构相对简单,我有 3 个实体,其设置如下: 定价计划好处 姓名 细绳 ...

回答 1 投票 0

双重列表在 Symfony 1.4 嵌入表单中不起作用

我正在努力让一个简单的表单在 Symfony 1.4 后端模块中工作。 这个想法是创建一个嵌入式表单(一对多关系,Inschrijvingen -> Danser)。 每个嵌入表单都有一个主要...

回答 1 投票 0

替代 fetchAllAssociative()` 并在学说中执行

我正在通过学说使用本机sql调用存储过程。不推荐使用 fetchAllAssociative() 和执行。有哪些替代方案? $sql = '调用 spWithParams(:param)'; $stmt = $这个...

回答 1 投票 0

如何在 Doctrine 中将 BLOB 列视为字符串?

使用教义 3.1 - 在我的实体中,我有此专栏: #[ORM\Column(类型:'blob')] 私人$dataJpg; 如果我将其设置为字符串,PHP 会抱怨资源已被使用并且无法在

回答 1 投票 0

MySQL Blob 字段语法错误

我创建了一个 Doctrine ORM 实体,其属性为 BLOB 类型: 公共常量 MAX_BLOB_SIZE = 2097152; #[ORM\Column(名称: 'blob', 类型: Types::BLOB, 长度: self::MAX_BLOB_SIZE, 可空: ...

回答 1 投票 0

Symfony ParamConverter 到 MapEntity

我正在尝试将我的symfony应用程序从版本5.4升级到6.4,因为Sensio Bundle已被放弃,我需要使用属性而不是注释,我如何将paramcoverter注释转换为

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.