0

我试图生成一个简单的 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 抛出此错误:

带有消息“未知关系别名 fn_provincia”的 Doctrine_Table_Exception。

教义版本是 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'
      )
    );
  }

}

?>
4

2 回答 2

2

从模型类中可以看出,fn_comuna&之间的关系fn_provincia称为pro

$this->hasOne('Fn_provincia as pro', array(
    'local' => 'pro_id', 
    'foreign' => 'pro_id'
  )
);

所以你在处理join时必须使用这个名字:

$q = Doctrine_Query::create()
    ->select('c.com_id')
    ->from('fn_comuna c')
    ->innerJoin('c.pro p')
    ->where('p.pro_nombre=?', $namepro);
于 2013-01-04T14:04:48.813 回答
0

改变:

p.pro_id = (SELECT p2.pro_id FROM etc..

至:

p.pro_id IN(SELECT p2.pro_id FROM etc..

不知道为什么你首先需要子查询,为什么不把它弄松并替换为:

where pro_nombre = 'namepro'
于 2013-01-03T21:17:47.763 回答