0

我正在为一个项目使用 symfony 1.4(已经开始),现在我正在更改一些东西,当我查询两个表之间的一些信息时,我得到了一个关系错误,一个已经存在,另一个是新的,应该有一个指向第一个表的外键。

我收到这样的错误消息

exception 'Doctrine_Table_Exception' with message 'Unknown relation alias ' in /var/www/testorange/symfony/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Relation/Parser.php:237 

Stack trace: #0 /var/www/testorange/symfony/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Relation/Parser.php(235): Doctrine_Relation_Parser->getRelation('', false)

...

还有更多错误。

所以,我认为这是因为我的表在我在 symfony 中的定义上还没有正确的关系(它们已经在我的数据库中相关)。

我将此行添加到我的 schema.yml 然后 [更新]

 OhrmTrainningSubmit:
  connection: doctrine
  tableName: ohrm_trainning_submit
  columns:
    id_trainning_submit:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    trainning:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    trainning_detail:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    answer:
      type: string(250)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    state:
      type: integer(1)
      fixed: false
      unsigned: false
      primary: false
      default: '0'
      notnull: true
      autoincrement: false
    user:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
  relations:
    OhrmUser:
      local: user
      foreign: emp_number
      type: many
    OhrmTrainning:
      local: trainning
      foreign: id_trainning
      type: many

这是 OhrmUser 的定义

OhrmUser:
  connection: doctrine
  tableName: ohrm_user
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
    user_role_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: true
      autoincrement: false
    emp_number:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    user_name:
      type: string(40)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    user_password:
      type: string(40)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    deleted:
      type: integer(1)
      fixed: false
      unsigned: false
      primary: false
      default: '0'
      notnull: true
      autoincrement: false
    status:
      type: integer(1)
      fixed: false
      unsigned: false
      primary: false
      default: '1'
      notnull: true
      autoincrement: false
    date_entered:
      type: timestamp(25)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    date_modified:
      type: timestamp(25)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    modified_user_id:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
    created_by:
      type: integer(4)
      fixed: false
      unsigned: false
      primary: false
      notnull: false
      autoincrement: false
  relations:
    HsHrEmployee:
      local: emp_number
      foreign: emp_number
      type: one
    OhrmUserRole:
      local: user_role_id
      foreign: id
      type: one
    HsHrMailnotifications:
      local: id
      foreign: user_id
      type: many
    OhrmLeaveAdjustment:
      local: id
      foreign: created_by_id
      type: many
    OhrmLeaveComment:
      local: id
      foreign: created_by_id
      type: many
    OhrmLeaveEntitlement:
      local: id
      foreign: created_by_id
      type: many
    OhrmLeaveRequestComment:
      local: id
      foreign: created_by_id
      type: many
    OhrmTimesheetActionLog:
      local: id
      foreign: performed_by
      type: many

在我要引用的表上,将引用的字段是“用户”到表“OhrmUser”到字段emp_number(我认为应该这样正确),问题是仍然是同样的错误,有人知道吗?还有什么我应该做的吗?

在我添加之后我运行这个

php symfony cc
php symfony doctrine:build-model
php symfony orangehrm:publish-assets
php symfony cc

查询

try{
$q = Doctrine_Query::create()
 ->select('*')
->from('OhrmTrainningSubmit TS')
->innerJoin('TS.OhrmUser U')
->addWhere("TS.trainning = $training")
->andWhere("TS.user = $employee");
$result = $q->execute();
    return $result;
}catch(Exception $e){
    print_r ($e->getMessage());
    return null;
}

我只能访问 OhrmTrainningSubmit 的数据,但是当我尝试访问 User 的字段时,我得到了内部错误。

我收到此代码的错误

foreach ($detail as $det){
    echo $det['answer']; // This is printed with no problem
    //echo $det['user_name']; <-- this one comes from the table OhrmUser, I get server error with this one
}

其中 $detail 是具有查询返回值的变量。

当我做

->select('TS.*, U.*')

我得到其他错误,说

未知属性 emp_number

任何的想法?

提前致谢。

4

1 回答 1

1

两件事情:

  1. 在您的架构中,确保您在关系附近有正确的缩进:

      autoincrement: false
    relations:
      OhrmUser:
        local: user
        foreign: emp_number
        type: many
    
  2. 查询应如下所示:

    $q = Doctrine_Query::create()
        ->from('TrainningSubmit T')
        ->innerJoin('T.OhrmUser U')
        ->where('T.id_trainning = ?', $id);
    

当您在原则中指定连接时,您必须告诉 ORM 关系来自哪里 - 因此T.OhrmUser U而不是OhrmUser U. 这告诉学说您正在使用模型中的OhrmUser关系T

更重要的是,您使用->where()了两次这是错误的,因为第二次覆盖了第一次。如果要添加多个条件,则应使用->andWhere()or 。然而,在处理学说中的命名关系时,您不必明确告诉学说它应该连接哪些列 - 它已经知道这要归功于模式文件。->orWhere()where

更重要的是,如果您需要扩展连接的条件,您可以使用 轻松做到这一点WITH,例如

 ->join('T.OhrmUser U WITH U.name LIKE ?', 'Michal')

将转化为:

 JOIN OhrmUser U ON U.emp_number = T.user AND U.name LIKE 'Michal'

编辑

要从对象中检索数据,请使用:

//if you return a plain array as a result of the query:
echo $det['answer'];
echo $det['OhrmUser']['user_name'];

//if you return an array of objects:
echo $det->getAnswer();
echo $det->getOhrmUser()->getUserName();
于 2013-04-16T15:02:43.127 回答