【问题标题】:how can I make symfony see my new relations between tables?如何让 symfony 看到我的表之间的新关系?
【发布时间】:2013-04-15 19:50:11
【问题描述】:

我正在为一个项目使用 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

有什么想法吗?

提前致谢。

【问题讨论】:

  • 您能否发布生成查询的代码以及添加关系的表的整个架构?
  • 谢谢,我在帖子中添加了一些版本,请您检查一下吗?
  • 请在您尝试访问OhrmUser 字段的位置发布代码。您还可以使用select('TS.*, U.*) 来避免用户字段的延迟加载。
  • 谢谢!我会试试的,我不知道懒惰模式。我写的代码有错误。
  • 抱歉问了这么多,现在选择两个表时出现新错误,未知属性 emp_number,它是我的外键.. 请检查我的更新

标签: php orm symfony-1.4 doctrine-1.2


【解决方案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。这告诉学说您正在使用来自T 模型的OhrmUser 关系。

更重要的是,您使用了两次-&gt;where(),这是错误的,因为第二次覆盖了第一次。如果要添加多个 where 条件,则应使用 -&gt;andWhere()-&gt;orWhere()。然而,在处理原则中的命名关系时,您不必明确告诉原则应该在哪些列上进行连接 - 由于架构文件,它已经知道这一点。

更重要的是,如果您需要扩展连接条件,您可以使用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();

【讨论】:

  • 谢谢你的回答!!这是一个很好的解释,我做了你所说的,但我一直收到同样的错误,查询正常,但是当我尝试访问连接表的字段时,我得到一个错误 500 并且没有任何工作了.. 知道什么可能??我做了这样的事情 $q = Doctrine_Query::create() ->select('*') ->from('OhrmTrainningSubmit TS') ->innerJoin('TS.OhrmUser U') ->addWhere("TS.trainning = $training") ->addWhere("TS.user = $employee"); $result = $q->execute();
  • 如何访问连接表的字段? (你可以编辑你的帖子)同样在where部分你应该使用参数:where('TS.training = ?', $training)而不是where("TS.training = $training")
  • 谢谢!你能检查我的更新吗?我一直有同样的错误,我可以访问我的第一个表的信息,但不能访问连接表的信息
  • 再问一个问题,如果我想再加入一次,我还可以使用“加入”或 addJoin 之类的东西吗?
  • 您可以使用join() 函数添加其他连接。只有where() 才行不通。
猜你喜欢
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 1970-01-01
相关资源
最近更新 更多