错误`名称包含非法字符`,在Doctrine 2中嵌入Symfony 2,8形式

时间:2016-11-17 15:36:45

标签: symfony doctrine-orm annotations embeddable

Doctrine 2注释嵌入在Windows上的Symfony 2.8中不起作用。

@ORM \ Embeddable无效,我收到错误:名称“address.addr_line_1”包含非法字符。名称应以字母,数字或下划线开头,并且只包含字母,数字,数字,下划线(“_”),连字符(“ - ”)和冒号(“:”)。该错误来自供应商... \ src \ Symfony \ Component \ Form \ FormConfigBuilder.php。

似乎原因是自动生成的名称中的点:“address.addr_line_1”。

我尝试禁用自动前缀,并提供自己的名称。 Doctrine生成带有列名的表,如我所写:addr_line_1。

但生成的表单包含点“address.addr_line_1”的字段,虽然错误为The name xxx contains illegal characters

如果我在表单中使用addr_line_1,我收到错误,该名称不存在。

<?php

namespace Learn\MySQLBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
//use Doctrine\ORM\Mapping\Embeddable;

use Learn\MySQLBundle\Entity\Embeddable\AddressEmb;
use Learn\MySQLBundle\Entity\Embeddable\TelEmb;

/**
 * @ORM\Table( name="te12AuthorEmb" )
 * @ORM\Entity()
 */
class e12AuthorEmb
{
     /**
     * @ORM\Id
     * @ORM\Column(type="string", length=36)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="Learn\MySQLBundle\Doctrine\Generator6")
     */
    protected $id;
    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string")
     */
    protected $name;

    // columnPrefix = "address_"
    /** @ORM\Embedded( class="Learn\MySQLBundle\Entity\Embeddable\AddressEmb", columnPrefix=false ) */
    protected $address;

    /** @var string @ORM\Column(type="string", length=200) */
    protected $email = '';

    // if there is a \Learn.. that cmd does not generate entities and crud, columnPrefix = "tel_" 
    /** @ORM\Embedded( class="Learn\MySQLBundle\Entity\Embeddable\TelEmb", columnPrefix=false ) */
    protected $tel = '';    

    public function __construct()
    {
        $this->address = new AddressEmb();
        $this->tel = new TelEmb();
    }


    /**
     * Set id
     *
     * @param string $id
     *
     * @return e5Author
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get id
     *
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return e5Author
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }


    /**
     * Set email
     *
     * @param string $email
     *
     * @return e10AuthorEmb
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set address
     *
     * @param \Learn\MySQLBundle\Entity\Embeddable\AddressEmb $address
     *
     * @return e10AuthorEmb
     */
    public function setAddress(\Learn\MySQLBundle\Entity\Embeddable\AddressEmb $address)
    {
        $this->address = $address;

        return $this;
    }

    /**
     * Get address
     *
     * @return \Learn\MySQLBundle\Entity\Embeddable\AddressEmb
     */
    public function getAddress()
    {
        return $this->address;
    }

    /**
     * Set tel
     *
     * @param \Learn\MySQLBundle\Entity\Embeddable\TelEmb $tel
     *
     * @return e10AuthorEmb
     */
    public function setTel(\Learn\MySQLBundle\Entity\Embeddable\TelEmb $tel)
    {
        $this->tel = $tel;

        return $this;
    }

    /**
     * Get tel
     *
     * @return \Learn\MySQLBundle\Entity\Embeddable\TelEmb
     */
    public function getTel()
    {
        return $this->tel;
    }
}

,,

<?php

namespace Learn\MySQLBundle\Entity\Embeddable;

use Doctrine\ORM\Mapping as ORM;

/**
 * Description of AddressEmb
 *
 * @author gintare
 */
/** @ORM\Embeddable */
class AddressEmb 
{
    /**  @ORM\Column( name="addr_line_1", type = "string", nullable=true) */
    protected $addr_line_1 = "";
    /**  @ORM\Column( name="addr_line_2", type = "string", nullable=true) */
    protected $addr_line_2 = "";
    /** @ORM\Column( name="addr_town", type = "string", nullable=true) */
    protected $addr_town = "";
    /**  @ORM\Column( name="addr_state", type = "string", nullable=true) */
    protected $addr_state = "";
    /**  @ORM\Column( name="addr_postcode", type = "string", nullable=true) */
    protected $addr_postcode = "";
    /* may be relation to other entity @var \Entities\Country * */
    /**  @ORM\Column( name="addr_country", type = "string", nullable=true) */
    protected $addr_country;

    public function __construct( $line_1 = null, $line_2 = null, $town = null, 
                                 $state = null, $postcode = null, $country=null )
    {
        $this->addr_line_1 = $line_1;
        $this->addr_line_2 = $line_2;
        $this->addr_town = $town;
        $this->addr_state = $state;
        $this->addr_postcode = $postcode;
        $this->addr_country = $country;
    }
}


<?php

namespace Learn\MySQLBundle\Entity\Embeddable;

use Doctrine\ORM\Mapping as ORM;
//use Doctrine\ORM\Mapping\Embeddable;

/**
 * TelEmb - value object to represent telephone code with country
 *
 * @author gintare
 */
/** @ORM\Embeddable */
class TelEmb 
{
    /** @ORM\Column( name="tel_number", type = "string", nullable=true) */
    protected $tel_number = "";
    /* *@ORM\Column( name="tel_countrycode", type = "string", nullable=true) */
    protected $tel_countrycode = "";
    /** @ORM\Column( name="tel_country", type = "string", nullable=true) */
    protected $tel_country;
    /** @ORM\Column( name="tel_type", type = "string", nullable=true) */
    protected $tel_type; //mobile, landline,
    /** @ORM\Column( name="tel_costinfo", type = "string", nullable=true) */
    protected $tel_costinfo; // information about cost
    /** @ORM\Column( name="tel_accessinfo", type = "string", nullable=true) */
    protected $tel_accessinfo; // information abotu accessability

    public function __construct( $number = null, $countrycode = null, $country = null,
                                $accessinfo = null, $costinfo = null, $type = null )
    {
        $this->tel_number = $number;
        $this->tel_countrycode = $countrycode;
        $this->tel_country = $country;
        $this->tel_accessinfo = $accessinfo;
        $this->tel_costinfo = $costinfo;
        $this->tel_type = $type;
    }
}

其中一个解决方案是允许在C:\Bitnami\wampstack-5.6.20-0\apache2\htdocs\sym\LearnDB\vendor\symfony\symfony\src\Symfony\Component\Form\FormConfigBuilder.php中使用点。

return '' === $name || null === $name || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\.\-:]*$/D', $name);   

这样可以生成新的表单,但我还没有继续进行,因为我需要在嵌入式中生成特殊的getter和setter。似乎最好的解决方案是使用此处描述的特征:http://russellscottwalker.blogspot.co.uk/2013/11/entities-vs-value-objects-and-doctrine-2.html

然而,如果有人知道如何使Embedable工作,而不更改Symfony供应商代码,请你写一下。

也许它可以用于直接查询     SELECT o FROM Order o WHERE o.address.addr_line_1 =:line1     http://welcometothebundle.com/persist-the-money-doctrine-value-object/ 它不适用于Symfony形式。

1 个答案:

答案 0 :(得分:1)

新版本的Doctrine不允许使用下划线等特殊字符。你试过没有他们吗?

Camel Case中编写变量,类和文件夹也被认为是一种很好的做法。