我尝试生成一个简单的SQL选择:
SELECT c.com_id, c.pro_id, c.com_nombre
FROM bd_fn.fn_comuna c
inner join bd_fn.fn_provincia p
on (c.pro_id = p.pro_id)
where p.pro_nombre = 'namepro';
但是DQL抛出了这个错误:
Doctrine_Table_Exception',消息'未知关系别名fn_provincia。
学说版本是1.XX,持久性是由Visual Paradigm创建的。 DQL是这样的:
$q = Doctrine_Query::create()
->select('c.com_id')
->from('fn_comuna c')
->innerJoin('c.fn_provincia p')
->where('p.pro_nombre=?',$namepro);
班级fn_comuna.php
<?php
/**
* "Visual Paradigm: DO NOT MODIFY THIS FILE!"
*
* This is an automatic generated file. It will be regenerated every time
* you generate persistence class.
*
* Modifying its content may cause the program not work, or your work may lost.
*/
class Fn_comuna extends Doctrine_Record {
public function setTableDefinition() {
$this->setTableName('bd_fn.fn_comuna');
$this->hasColumn('com_id', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'unsigned' => false,
'notnull' => true,
'primary' => true,
'autoincrement' => false,
)
);
$this->hasColumn('pro_id', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'unsigned' => false,
'notnull' => true,
)
);
$this->hasColumn('com_nombre', 'string', 100, array(
'type' => 'string',
'length' => 100,
'fixed' => false,
'notnull' => true,
)
);
}
public function setUp() {
parent::setUp();
$this->hasOne('Fn_provincia as pro', array(
'local' => 'pro_id',
'foreign' => 'pro_id'
)
);
$this->hasMany('Fn_institucion as fn_institucion', array(
'local' => 'com_id',
'foreign' => 'com_id'
)
);
$this->hasMany('Fn_replegal as fn_replegal', array(
'local' => 'com_id',
'foreign' => 'com_id'
)
);
}
}
?>
答案 0 :(得分:2)
从模型类中可以看出,fn_comuna
&amp; fn_provincia
被称为pro
。
$this->hasOne('Fn_provincia as pro', array(
'local' => 'pro_id',
'foreign' => 'pro_id'
)
);
因此,在处理加入时必须使用此名称:
$q = Doctrine_Query::create()
->select('c.com_id')
->from('fn_comuna c')
->innerJoin('c.pro p')
->where('p.pro_nombre=?', $namepro);
答案 1 :(得分:0)
变化:
p.pro_id = (SELECT p2.pro_id FROM etc..
要:
p.pro_id IN(SELECT p2.pro_id FROM etc..
不确定为什么首先需要子查询,为什么不松开它并将其替换为:
where pro_nombre = 'namepro'