Symfony:通过控制器更新外键的教义
Posted
技术标签:
【中文标题】Symfony:通过控制器更新外键的教义【英文标题】:Symfony: Doctrine Update Foreign Key thru controller 【发布时间】:2021-09-13 15:41:49 【问题描述】:我的问题是我有两个实体客户和渠道。通道 ID 在客户表中作为外键引用。我正在尝试制作一个控制器来更新此列,然后在后台编辑客户信息,并使用国家/地区字段返回与频道表中的 id 对应的数字。在mysql中我正在尝试做
UPDATE voltalia.sylius_customer SET channel_id = 'id_channel' WHERE (id = 'customer');
我的Entity/Customer.php:
<?php
declare(strict_types=1);
namespace App\Entity\Customer;
use App\Entity\Channel\Channel;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\Customer as BaseCustomer;
use Symfony\Component\Validator\Constraints as Assert;
use Ambta\DoctrineEncryptBundle\Configuration\Encrypted;
/**
* @ORM\Entity
* @ORM\AttributeOverrides(
* @ORM\AttributeOverride(name="lastName",
* column=@ORM\Column(name="last_name", nullable=true, type="string", length=7500)
* ),
* @ORM\AttributeOverride(name="firstName",
* column=@ORM\Column(name="first_name", nullable=true, type="string", length=7500)
* )
* )
* @ORM\Table(name="sylius_customer")
*/
class Customer extends BaseCustomer implements CustomerInterface
public const STATE_NEW = 'new';
public const STATE_TRUSTED = 'trusted';
/**
* @var CustomerType
* @Assert\NotNull(groups="sylius", message="voltalia.customer.require_accont_type")
* @ORM\ManyToOne(targetEntity="App\Entity\Customer\CustomerType")
* @ORM\JoinColumn(name="customer_type_id", referencedColumnName="id")
*/
private $customerType;
/**
* @var string $taxNumber
* @Encrypted()
* @Assert\NotBlank(groups="sylius")
* @ORM\Column(name="tax_number", nullable=true, type="text")
*/
private $taxNumber;
/**
* @var string $pecMail
* @Encrypted()
* @ORM\Column(name="pec_mail", nullable=true, type="text")
*/
private $pecMail;
/**
* @var string $country
* @Encrypted()
* @ORM\Column(name="country", nullable=true, type="text")
*/
private $country;
/**
* @var string $state
*
* @ORM\Column(name="state", nullable=true, type="string")
*/
private $state = self::STATE_NEW;
/** @var string|null
* @Encrypted
*/
protected $firstName;
/** @var string|null
* @Encrypted
*/
protected $lastName;
protected $gender = CustomerInterface::UNKNOWN_GENDER;
/** @var string|null
* @Encrypted
*/
protected $phoneNumber;
/**
* @var Channel
* @ORM\ManyToOne(targetEntity="App\Entity\Channel\Channel")
* @ORM\JoinColumn(name="channel_id", referencedColumnName="id")
*/
private $registerChannel;
/**
* @return CustomerType | null
*/
public function getCustomerType(): ?CustomerType
return $this->customerType;
/**
* @param CustomerType | null $customerType
*/
public function setCustomerType(?CustomerType $customerType): void
$this->customerType = $customerType;
public function isB2B(): bool
return $this->customerType !== null && $this->customerType->getCode() === 'B2B';
/**
* @return string
*/
public function getTaxNumber(): ?string
return $this->taxNumber;
/**
* @param string $taxNumber
*/
public function setTaxNumber(?string $taxNumber): void
$this->taxNumber = $taxNumber;
/**
* @return string
*/
public function getPecMail(): ?string
return $this->pecMail;
/**
* @param string $pecMail
*/
public function setPecMail(?string $pecMail): void
$this->pecMail = $pecMail;
/**
* @return string
*/
public function getCountry(): ?string
return $this->country;
/**
* @param string $country
*/
public function setCountry(?string $country): void
$this->country = $country;
/**
* @return Channel
*/
public function getRegisterChannel(): ?Channel
return $this->registerChannel;
/**
* @param Channel $registerChannel
*/
public function setRegisterChannel(Channel $registerChannel): void
$this->registerChannel = $registerChannel;
/**
* @return string
*/
public function getState(): ?string
return $this->state;
/**
* @param string $state
*/
public function setState(?string $state): void
$this->state = $state;
我的Entity/Channel.php是sylius默认的:
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Component\Core\Model;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Channel\Model\Channel as BaseChannel;
use Sylius\Component\Currency\Model\CurrencyInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
class Channel extends BaseChannel implements ChannelInterface
/** @var CurrencyInterface */
protected $baseCurrency;
/** @var LocaleInterface */
protected $defaultLocale;
/** @var ZoneInterface */
protected $defaultTaxZone;
/** @var string */
protected $taxCalculationStrategy;
/**
* @var Collection|CurrencyInterface[]
*
* @psalm-var Collection<array-key, CurrencyInterface>
*/
protected $currencies;
/**
* @var Collection|LocaleInterface[]
*
* @psalm-var Collection<array-key, LocaleInterface>
*/
protected $locales;
/** @var string */
protected $themeName;
/** @var string */
protected $contactEmail;
/** @var bool */
protected $skippingShippingStepAllowed = false;
/** @var bool */
protected $skippingPaymentStepAllowed = false;
/** @var bool */
protected $accountVerificationRequired = true;
/** @var ShopBillingDataInterface|null */
protected $shopBillingData;
public function __construct()
parent::__construct();
/** @var ArrayCollection<array-key, CurrencyInterface> $this->currencies */
$this->currencies = new ArrayCollection();
/** @var ArrayCollection<array-key, LocaleInterface> $this->locales */
$this->locales = new ArrayCollection();
/**
* @inheritdoc
*/
public function getBaseCurrency(): ?CurrencyInterface
return $this->baseCurrency;
/**
* @inheritdoc
*/
public function setBaseCurrency(?CurrencyInterface $baseCurrency): void
$this->baseCurrency = $baseCurrency;
/**
* @inheritdoc
*/
public function getDefaultLocale(): ?LocaleInterface
return $this->defaultLocale;
/**
* @inheritdoc
*/
public function setDefaultLocale(?LocaleInterface $defaultLocale): void
$this->defaultLocale = $defaultLocale;
/**
* @inheritdoc
*/
public function getDefaultTaxZone(): ?ZoneInterface
return $this->defaultTaxZone;
/**
* @inheritdoc
*/
public function setDefaultTaxZone(?ZoneInterface $defaultTaxZone): void
$this->defaultTaxZone = $defaultTaxZone;
/**
* @inheritdoc
*/
public function getTaxCalculationStrategy(): ?string
return $this->taxCalculationStrategy;
/**
* @inheritdoc
*/
public function setTaxCalculationStrategy(?string $taxCalculationStrategy): void
$this->taxCalculationStrategy = $taxCalculationStrategy;
/**
* @inheritdoc
*/
public function getCurrencies(): Collection
return $this->currencies;
/**
* @inheritdoc
*/
public function addCurrency(CurrencyInterface $currency): void
if (!$this->hasCurrency($currency))
$this->currencies->add($currency);
/**
* @inheritdoc
*/
public function removeCurrency(CurrencyInterface $currency): void
if ($this->hasCurrency($currency))
$this->currencies->removeElement($currency);
/**
* @inheritdoc
*/
public function hasCurrency(CurrencyInterface $currency): bool
return $this->currencies->contains($currency);
/**
* @inheritdoc
*/
public function getLocales(): Collection
return $this->locales;
/**
* @inheritdoc
*/
public function addLocale(LocaleInterface $locale): void
if (!$this->hasLocale($locale))
$this->locales->add($locale);
/**
* @inheritdoc
*/
public function removeLocale(LocaleInterface $locale): void
if ($this->hasLocale($locale))
$this->locales->removeElement($locale);
/**
* @inheritdoc
*/
public function hasLocale(LocaleInterface $locale): bool
return $this->locales->contains($locale);
/**
* @inheritdoc
*/
public function getThemeName(): ?string
return $this->themeName;
/**
* @inheritdoc
*/
public function setThemeName(?string $themeName): void
$this->themeName = $themeName;
/**
* @inheritdoc
*/
public function getContactEmail(): ?string
return $this->contactEmail;
/**
* @inheritdoc
*/
public function setContactEmail(?string $contactEmail): void
$this->contactEmail = $contactEmail;
/**
* @inheritdoc
*/
public function isSkippingShippingStepAllowed(): bool
return $this->skippingShippingStepAllowed;
/**
* @inheritdoc
*/
public function setSkippingShippingStepAllowed(bool $skippingShippingStepAllowed): void
$this->skippingShippingStepAllowed = $skippingShippingStepAllowed;
/**
* @inheritdoc
*/
public function isSkippingPaymentStepAllowed(): bool
return $this->skippingPaymentStepAllowed;
/**
* @inheritdoc
*/
public function setSkippingPaymentStepAllowed(bool $skippingPaymentStepAllowed): void
$this->skippingPaymentStepAllowed = $skippingPaymentStepAllowed;
/**
* @inheritdoc
*/
public function isAccountVerificationRequired(): bool
return $this->accountVerificationRequired;
/**
* @inheritdoc
*/
public function setAccountVerificationRequired(bool $accountVerificationRequired): void
$this->accountVerificationRequired = $accountVerificationRequired;
public function getShopBillingData(): ?ShopBillingDataInterface
return $this->shopBillingData;
public function setShopBillingData(ShopBillingDataInterface $shopBillingData): void
$this->shopBillingData = $shopBillingData;
而且,目前,我尝试的是为客户端创建一个控制器并使用 createQueryBuilder 进行更新:
<?php
namespace App\Controller;
use App\Entity\Channel\Channel;
use App\Entity\Customer\Customer;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class CustomerController extends AbstractController
/*
*@Route("/admin/customers/id/edit", name="voltalia_update_channel", methods="POST")
*/
public function updateChannel (Customer $customer, Channel $channel, $id, EntityManagerInterface $entityManager)
$entityManager->createQueryBuilder()
->update('channel_id', ':channel')
->setParameter('channel', $id)
->where('id',':customer')
->setParameter('id', $customer)
->getQuery()
;
现在我无法从这个控制器更新字段。我做错了什么?
【问题讨论】:
【参考方案1】:因此,在您的控制器操作中,如果您正确注入了Customer
、Channel
和EntityManagerInterface
,您可以设置Customer
的Channel
并使用$entityManager
来漂亮地更新实体很容易。
// tell the EntityManager to track changes to entity/entities
$entityManager->persist($customer);
// update entity/entities to suit your needs
$customer->setRegisterChannel($channel);
// tell the EntityManager to update tracked entities in the database
$entityManager->flush();
【讨论】:
但是如何从模板中接收一个 id 并将这个 id 与频道数据库中的现有的进行比较,然后定义具有相同 id 的频道?以上是关于Symfony:通过控制器更新外键的教义的主要内容,如果未能解决你的问题,请参考以下文章