duan4369 2014-11-14 12:11
浏览 35

Doctrine 2:Sql Server表与字段名称中的空格的错误映射关系

This is the stage:

Linux debian with Symfony2.5.6/Doctrine2 connected to Sql Server via (FreeTDS DB-lib).

intellectsoft-uk/MssqlBundle Driver installed and running ok.

Two SQL Server related tables with spaces in Table/Field names (sorry, i can't post images):

´
dbo.MYCOMPANY$Item
    No.         - varchar(32)
    Description - varchar(32)

dbo.[MYCOMPANY$Item Variant]      
    Code         - varchar(32)    
    Item No.     - varchar(32)
´

Note: Yes... it's a horrible naming style, Dynamics Nav and microsoft are the offenders.


Two Doctrine's entities mapping this tables with a OneToMany relation pointing to Item Variant entity from Item entity:

/**
 * @ORM\Table(name="dbo.MYCOMPANY$Item")
 * @ORM\Entity
 */
class Item
{
    /* @ORM\Id 
     * @ORM\Column(name="[No_]", type="string", length=32) 
     */
    private $no;

    /**
    *  @ORM\Column(name="Description", type="string", length=128) 
     */
    private $description;

    /* @ORM\OneToMany(
     *     targetEntity="DymamicsBundle\Entity\ItemVariant", 
     *     mappedBy="item", cascade={"persist","remove"}) 
     */
    private $itemVariants;
}

/**
 * @ORM\Table(name="dbo.[MYCOMPANY$Item Variant]")
 * @ORM\Entity
 */
class ItemVariant
{
    /* @ORM\Id 
     * @ORM\Column(name="Code", type="string", length=32, nullable=false) 
     */
    private $code;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(
     *    targetEntity="DymamicsBundle\Entity\Item", 
     *    inversedBy="itemVariants")
     * @ORM\JoinColumn(
     *     name="[Item No_]", 
     *     referencedColumnName="[No_]", onDelete="CASCADE")
     */
    private $item;
}

In a controller i query for an item XXXXXX:

/** @Template() */
public function helloAction($name)
{
    $item = $em->getRepository("DynamicsBundle:Item")->findByNo('XXXXXX');
    return array('item' => $item);
}

In the index.html.twig template i print the fields without problems:

{% block content %}
    {{ item.no }}
    {{ item.description }}
{% endblock %}

... And now the problem:

If i try to access to the member itemVariants...

{% block content %}
    {{ item.no }}
    {{ item.description }}

    This item has {{ item.itemVariants.count }} variants
    {% for v in item.itemVariants %}
        <p>{{ v.code }}</p>
    {% endfor %}

{% endblock %}

I get this fatal error:

> 
> An exception has been thrown during the rendering of a template
> ("Unrecognized field: t0.[Item No_]") in
> .../DynamicsBundle/Resources/views/Demo/index.html.twig at line 2.
> 

One tested solution (non-viable for me, since i can't modify Dynamics Nav database fields):

Renaming the Item No. field in the table dbo.[MYCOMPANY$Item Variant] by... i.e. ItemNo... problem resolved:

class ItemVariant
{
    ...
     * @ORM\JoinColumn(
     *     name="ItemNo", 
    ...
    private $item;
}

The output:

This item has 2 variants
red
blue

Accordingly... the problem seems to be the Item No. field name

I tried to change this parameters in the SQL Server database:

ANSI_NULLS ON
ANSI_PADDING ON
ANSI_WARNINGS ON
ARITHABORT ON
QUOTED_IDENTIFIER ON
CONCAT_NULL_YIELDS_NULL ON
NUMERIC_ROUNDABORT OFF 

... but nothing

¿Any suggestion? Thx...

  • 写回答

2条回答 默认 最新

  • douguiyan9164 2014-11-14 12:30
    关注

    Maybe you should use ` in table and field names (in annotations)?

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?