Symfony:条令通过控制器更新外键



我的问题是我有两个实体客户和渠道。通道id在customer表中被引用为外键。我正试图制作一个控制器来更新这一列,然后在后台编辑带有国家/地区字段的客户信息,该字段返回一个与频道表中的id相对应的数字。在mysql中,我正在尝试做

UPDATE voltalia.sylius_customer SET channel_id = 'id_channel' WHERE (id = 'customer'); 

我的实体/客户.php


<?php

declare(strict_types=1);

namespace AppEntityCustomer;

use AppEntityChannelChannel;
use DoctrineORMMapping as ORM;
use SyliusComponentCoreModelCustomer as BaseCustomer;
use SymfonyComponentValidatorConstraints as Assert;
use AmbtaDoctrineEncryptBundleConfigurationEncrypted;

/**
* @ORMEntity
* @ORMAttributeOverrides({
*     @ORMAttributeOverride(name="lastName",
*          column=@ORMColumn(name="last_name", nullable=true, type="string", length=7500)
*     ),
*     @ORMAttributeOverride(name="firstName",
*          column=@ORMColumn(name="first_name", nullable=true, type="string", length=7500)
*     )
* })
* @ORMTable(name="sylius_customer")
*/
class Customer extends BaseCustomer implements CustomerInterface
{
public const STATE_NEW = 'new';
public const STATE_TRUSTED = 'trusted';

/**
* @var CustomerType
* @AssertNotNull(groups={"sylius"}, message="voltalia.customer.require_accont_type")
* @ORMManyToOne(targetEntity="AppEntityCustomerCustomerType")
* @ORMJoinColumn(name="customer_type_id", referencedColumnName="id")
*/
private $customerType;

/**
* @var string $taxNumber
* @Encrypted()
* @AssertNotBlank(groups={"sylius"})
* @ORMColumn(name="tax_number", nullable=true, type="text")
*/
private $taxNumber;

/**
* @var string $pecMail
* @Encrypted()
* @ORMColumn(name="pec_mail", nullable=true, type="text")
*/
private $pecMail;

/**
* @var string $country
* @Encrypted()
* @ORMColumn(name="country", nullable=true, type="text")
*/
private $country;

/**
* @var string $state
*
* @ORMColumn(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
* @ORMManyToOne(targetEntity="AppEntityChannelChannel")
* @ORMJoinColumn(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;
}
}

MyEntity/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 SyliusComponentCoreModel;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use SyliusComponentAddressingModelZoneInterface;
use SyliusComponentChannelModelChannel as BaseChannel;
use SyliusComponentCurrencyModelCurrencyInterface;
use SyliusComponentLocaleModelLocaleInterface;

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 AppController;

use AppEntityChannelChannel;
use AppEntityCustomerCustomer;
use DoctrineORMEntityManagerInterface;
use SymfonyBundleFrameworkBundleControllerAbstractController;



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()
;

}
}

现在我无法从这个控制器更新字段。我做错了什么?

因此,在控制器操作中,如果正确注入了CustomerChannelEntityManagerInterface,则可以很容易地设置CustomerChannel并使用$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();

最新更新