如何解决Symfony:Doctrine 更新外键通过控制器
我的问题是我有两个实体客户和渠道。频道 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",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",type="text")
*/
private $taxnumber;
/**
* @var string $pecMail
* @Encrypted()
* @ORM\Column(name="pec_mail",type="text")
*/
private $pecMail;
/**
* @var string $country
* @Encrypted()
* @ORM\Column(name="country",type="text")
*/
private $country;
/**
* @var string $state
*
* @ORM\Column(name="state",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()
;
}
}
现在我无法从这个控制器更新字段。我做错了什么?
解决方法
因此,在您的控制器操作中,如果您正确注入了 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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。