0

可嵌入的 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

不过,如果有人知道如何在不更改 Symfony 供应商代码的情况下使 Embedable 工作,请您写信。

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

4

1 回答 1

1

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

在Camel Case中编写变量、类和文件夹也被认为是一种好习惯。

于 2016-11-17T16:00:15.613 回答