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...
Maybe you should use ` in table and field names (in annotations)?
Quote from Doctrine documentation:
You cannot use non [a-zA-Z0-9_]+ characters, they will break several SQL statements.
Having problems with these kind of column names? Many databases support all CRUD operations on views that semantically map to certain tables. You can create views for all your problematic tables and column names to avoid the legacy quoting nightmare.
So the space in column name "Item No." is the source of problem encountered.
Or more specifically, here is the line of code prevent join column from having space " " character.
So create views for those tables as suggested by @Karol and @xhostar is the recommended way.