Doctrine Project是一个开源库和工具的集合,用于处理用PHP编写的数据库抽象和对象关系映射。
我有一些实体,例如带有投票者的设备实体,允许当前用户访问或不访问某些设备。 在搜索设备时,为了进行过滤,我使用了 array_filter 函数...
启动我的网络应用程序时,出现 500 错误。 prod.log 显示与数据库的连接错误,并且在其下方有一个缓存。警告:无法在 /var/www/project/var/cache 中保存密钥
symfony 5.4 和学说:SQLSTATE[HY000] [2019] 未知字符集
我设法读了一本关于 symfony 5 的书,但是我被困在这本书开始谈论数据库的地方。我已将其添加到 .env 文件中: DATABASE_URL="mysql://amaury:pma@localhost/
无法删除在 Symfony / Doctrine 的联结表中具有相关条目的实体
我有以下实体: 轮廓 标签 标签类别 个人资料标签 ProfileTag 是一个联结表,它在 Profile 和 Tag 之间形成多对多的关系。 每个标签属于一个类别。 我...
我正在尝试设计一个学说查询,我对学说很陌生,但在我的另一篇文章的帮助下,我想出了一个在我的 Mysql 中运行时有效的查询。但我希望它能够将查询转换为
为什么使用doctrine直接访问从数据库获取的数据不起作用,而使用getter却可以?
在具有学说的 PHP Symfony 项目中,我使用以下具有自引用属性“credit”的类和引用类 foo 的另一个属性,如下所示。 /** * 账单 * * @ORM\表(
在学说、symfony 中创建同一实体的两个实例之间的关系(外键指向其所在的表)
提前感谢您的所有回复 这是我在这里的第一条消息,所以请随时告诉我是否有一些我忘记提供给您的信息,以便确定问题的大小...
我有一个带有 Doctrine 的 Symfony 项目。 Doctrine有一个命令doctrine:schema:update。 它被使用得太频繁了,可以说是出于习惯。运行原则:架构:更新后,所有迁移都是
某些属性在 API 资源中是“只读”的,即使它们允许在 Symfony 中写入
我刚刚使用 symfony 和学说创建了一个实体“候选者”,您可能会在下面看到: < ?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use App\Repository\CandidateRepo...
postgresql 中的教义 JSON 列存储相同数据的对象或数组
我有一个Symfony应用程序,用户[doctrine]实体具有属性角色,它应该是一个数组或字符串。 /** * @ORM\Column(类型=“json”) */ 私人$角色= [];...
我是 Symfony2 和 Doctrine 的新手,我有点卡住了。保存到数据库时接收错误。 执行 'INSERT INTO tho_provider (provid_name,provid_logo,
PropertyAccessor 需要对对象或数组进行操作,但它发现类型为“NULL”
我在AssignmentTypeAdmin中有这个方法: 受保护的函数configureFormFields(FormMapper $formMapper) { $formMapper ->add('代码', '文本') ->添加('标签...
理解 Symfony 5 Doctrine ManyToOne 与用户和角色的关系概念
我正在尝试理解 Doctrine ORM 关系概念,因为据我所知,我花了很多时间来创建一个非常简单的用例,但是,我没能做到这一点,因为我有一个松弛的
给出了“Doctrine\Common\Collections\ArrayCollection”、“Doctrine\ORM\PersistentCollection”类型的预期参数
我在标签和文章实体之间有多对多关系,插入效果很好,但编辑表单(editAction 函数)的创建不起作用。 所有代码都在那里: 艺术...
Symfony 使用 AdvancedUserInterface
我正在尝试用Symfony2实现一个登录系统。我正在为我的用户实体实现 AdvancedUserInterface,并且当我尝试登录时不断收到以下错误。任何帮助都是值得赞赏的...
Symfony 6.4 - Doctrine - Xml - 没有可用的匹配全局属性声明,但严格通配符要求
composer.json "学说/学说捆绑": "^2.11.1", "学说/学说-迁移-bundle": "^3.3.0", “教义/规则”:“...
我有一个应用程序,其数据库按实体学说进行管理。 我的应用程序需要动态创建没有实体的表。 (以编程方式) 当我执行doctrine:schema:update comma...
Setter 和 getter 无法从 Symfony2 中的自定义用户实体访问
我正在尝试实现 hwioauthbundle 来与 Google 连接。 然而,我面临着 symfony 似乎无法找到 User 实体中声明的方法的问题 - 我相信它有一些......
阅读此页,我已经设置了一个表单来处理 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 请求仅通过发送的字段来编辑实体
Symfony Doctrine 对于数据库中的同一列有两个属性
这是一个示例实体 使用 ApiPlatform\Metadata\ApiResource; 使用 Doctrine\ORM\Mapping 作为 ORM; 使用 Symfony\Component\Serializer\Annotation\Groups; #[ORM\Table(名称: '用户')] #[API资源( 操作...