Php Symfony 2多对多和自定义sql
我的自定义sql存在问题,需要使用多对多关系 我有几张桌子: -属性产品 -属性 -产品 -属性组 和三个实体 -属性 -产品 -属性组 现在我创建查询:Php Symfony 2多对多和自定义sql,php,sql,sql-server,symfony,Php,Sql,Sql Server,Symfony,我的自定义sql存在问题,需要使用多对多关系 我有几张桌子: -属性产品 -属性 -产品 -属性组 和三个实体 -属性 -产品 -属性组 现在我创建查询: 'SELECT a.name, ag.name as "group" FROM produkt p' . ' JOIN attributeprodukt ap ON (p.id = ap.produkt_id)' . ' JOIN attribute a ON (a.id = ap.attrib
'SELECT a.name, ag.name as "group" FROM produkt p'
. ' JOIN attributeprodukt ap ON (p.id = ap.produkt_id)'
. ' JOIN attribute a ON (a.id = ap.attribute_id)'
. ' JOIN attributegroup ag ON (ag.id = a.group_id)'
. ' WHERE p.id = :id'
. ' ORDER BY ag.name ASC, a.name ASC';
当我在Windows(XAMPP)上测试它时,一切正常,但当我将文件发送到服务器(LINUX)时。我得到一个错误:
找不到基表或视图:1146表
“attributeprodukt”不存在
我的短代码。有人能帮我吗?(我检查了数据库,得到了表attributeproduct
class attribute
{
/**
* @ORM\ManyToMany(targetEntity="product", inversedBy="attributes", cascade={"persist"})
* @ORM\JoinTable(name="attributeproduct")
*/
protected $products;
/**
* @ORM\ManyToOne(targetEntity="attributegroup", inversedBy="attributes")
*/
protected $group;
}
class produkt
{
* @ORM\ManyToMany(targetEntity="attribute", mappedBy="products", cascade={"persist"})
*/
protected $attributes;
}
class attributegroup
{
/**
* @ORM\OneToMany(targetEntity="attribute", mappedBy="group", cascade={"persist"}, orphanRemoval=true)
* @OrderBy({"name" = "ASC"})
*/
protected $attributes;
}
您正在尝试对生成的表名进行联接,同时应该对生成表的实体类应用联接,因为dql只识别实体类,而不识别生成的表
'SELECT a.name, ag.name FROM YourBundle:produkt p'
' JOIN YourBundle:attribute a ON (p.id = a.products)'
' JOIN YourBundle:attributegroup ag ON (ag.id = a.group)'
' WHERE p.id = :id'
' ORDER BY ag.name ASC, a.name ASC';
我不擅长dql查询,但我的基本意思是,您应该使用实体类参数而不是生成的表的参数。您如何使用查询?createQuery()、createNativeQuery()或使用EntityManager的连接发出您自己准备好的语句?