我正在使用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然后[UPDATED]
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
任何想法?
提前致谢。
答案 0 :(得分:1)
两件事:
在您的架构中,确保在关系附近有正确的缩进:
autoincrement: false
relations:
OhrmUser:
local: user
foreign: emp_number
type: many
查询应如下所示:
$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()
条件,则应使用->orWhere()
或where
。然而,在处理学说中的命名关系时,你不必明确告诉doctrine它应该在哪些列上进行连接 - 它已经知道这要归功于模式文件。
此外,如果您需要扩展联接条件,您可以使用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();