gpt4 book ai didi

php - 具有多对多关系的 Symfony 3 uniqueEntity 验证

转载 作者:可可西里 更新时间:2023-11-01 09:00:12 30 4
gpt4 key购买 nike

我很难找到这个问题的解决方案:我有 2 类联系人和电话,具有多对多关系。 phones 表有 2 个字段:countryCode 和 number。我想检查 2 个联系人没有相同的电话号码,因此要对 2 个字段的组合进行验证。最后一个代码是这个,但我尝试了很多可能性。

    /**
* Contact
*
* @ORM\Table(
* name="contact"
* )
* @ORM\Entity(repositoryClass="EPI\PlatformBundle\Repository\ContactRepository")
*
* @UniqueEntity(fields={"email"}, message="ce mail existe déjà")
* @UniqueEntity(fields={"phoneNumbers"}, message="this phone number already exists")
*
*/
class Contact
{
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;


/**
* One User can have several Phonenumbers.
* @ORM\ManyToMany(targetEntity="Phone", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\JoinTable(name="contact_phones",
* joinColumns={@ORM\JoinColumn(name="contact_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="phone_id", referencedColumnName="id", unique=true, onDelete="CASCADE")}
* )
*/


/**
* Phone
*
* @ORM\Table(name="phone")
* @ORM\Entity(repositoryClass="EPI\PlatformBundle\Repository\PhoneRepository")
*
*/

class Phone
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var string
*
* @ORM\Column(name="country_code", type="string", length=4)
*/
private $countryCode;

/**
* @var string
*
* @ORM\Column(name="phone_nb", type="string", length=9)
*/
private $phoneNb;



class ContactType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', ChoiceType::class, array(
'choices' => array(
'Madame' => 'Madame',
'Monsieur' => 'Monsieur'
)
))
->add('firstName', TextType::class)
->add('surname', TextType::class)
->add('email', EmailType::class)
->add('phoneNumbers', CollectionType::class, array(
'entry_type' => PhoneType::class,
'entry_options' => array('label' => false),
'allow_add' => 'true',
'allow_delete' => 'true',
'error_bubbling' => 'true'
));
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'EPI\PlatformBundle\Entity\Contact',
));
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'contact';
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'contact';
}
}

使用这段代码我得到以下错误:

    An exception occurred while executing 'SELECT t0.id AS id_1, t0.title AS title_2, t0.first_name AS first_name_3, t0.surname AS surname_4, t0.email AS email_5 FROM contact t0 WHERE contact_phones.phone_id = ?' with params [{}]:

SQLSTATE[42S22]: Column not found: 1054 Champ 'contact_phones.phone_id' inconnu dans where clause

感谢您的帮助。

最佳答案

我终于找到了解决办法,联系人实体中的“uniqueEntity”注解不是正确的工作方式,这个注解必须放在电话实体中,但必须添加“@Assert\Valid()”注解在指向联系人实体中电话对象的集合引用之上。为了更清楚,这是代码:

    class Contact
{
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* One Contact can have Many Addresses.
* @ORM\ManyToMany(targetEntity="Address", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\JoinTable(name="contact_addresses",
* joinColumns={@ORM\JoinColumn(name="contact_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="address_id", referencedColumnName="id", unique=true, onDelete="CASCADE")}
* )
*/
private $addresses;

/**
* One User can have several Phonenumbers.
* @ORM\ManyToMany(targetEntity="Phone", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\JoinTable(name="contact_phones",
* joinColumns={@ORM\JoinColumn(name="contact_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="phone_id", referencedColumnName="id", unique=true, onDelete="CASCADE")}
* )
*
* @Assert\Valid()
*
*/
private $phoneNumbers;


<?php

namespace EPI\PlatformBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\ Validator\Constraints\UniqueEntity;

/**
* Phone
*
* @ORM\Table(name="phone")
* @ORM\Entity(repositoryClass="EPI\PlatformBundle\Repository\PhoneRepository")
*
*
* @UniqueEntity(fields={"countryCode","phoneNb"}, message="ce numéro de téléphone existe déjà")
*/

class Phone
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var string
*
* @ORM\Column(name="country_code", type="string", length=4)
*/
private $countryCode;

/**
* @var string
*
* @ORM\Column(name="phone_nb", type="string", length=9)
*/
private $phoneNb;

@Assert\Valid() 注释是要求 Symfony 强制验证私有(private) $phoneNb 变量引用的“子对象”的方法。

关于php - 具有多对多关系的 Symfony 3 uniqueEntity 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46368680/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com