2013-04-15 1 views
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) 

같은 메시지 ...

많은 오류를 더 얻을.

내 테이블에는 심포니의 내 정의에 대한 적절한 관계가 없기 때문에 생각합니다. (이미 내 DB와 관련되어 있습니다).

나는이 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 

에 대한 정의가

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 

후 [업데이트]] 내 schema.yml 파일 필드에이 줄을 추가하는 참조가 필드 "emp_number"(나는이 방법으로 올바른 것 같아요) 테이블 "OhrmUser"테이블에 "사용자"입니다, 문제는 여전히 같은 오류가 누구나 이것의 생각을 가지고 있습니까? 내가해야하는 다른 것?

나는 내가 단지 OhrmTrainningSubmit의 데이터에 액세스 할 수 있습니다

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; 
} 

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

쿼리를 실행하는 것이 추가,하지만 난 때 내가 얻을 사용자의 필드에 접속을 한 후 내부 오류.

나는 $ 세부 쿼리의 반환 값으로 변수이 코드

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 
} 

에서 오류가 발생합니다. 라고 내가 다른 오류가

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

내가 할

,

어떤 생각 emp_number

알 수없는 재산?

미리 감사드립니다.

+0

가 쿼리를 생성하는 코드를 게시 할 수 있습니다 : 오브젝트의 사용에서 데이터를 검색하려면

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

편집

:에

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

번역합니다 관계를 추가 한 테이블의 전체 스키마? –

+0

감사합니다. 일부 게시물을 내 게시물에 추가했습니다. 확인해주세요. – jpganz18

+0

'OhrmUser'필드에 액세스하려는 코드를 게시하십시오. 또한'select ('TS. *, U. *)'를 사용하여 사용자 필드의 지연로드를 방지 할 수 있습니다. –

답변

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); 
    

요일 doctrine에서 조인을 지정하면 ORM에게 관계가 어디서 왔는지 알려 주어야합니다. 따라서 OhrmUser U 대신 T.OhrmUser U이옵니다. 이것은 T 모델의 OhrmUser 관계를 사용하고 있다고 교리에 알려줍니다.

->where() 두 번 이상 사용 된 것은 두 번째가 첫 번째 것을 덮어 쓰므로 잘못 사용 된 것입니다. 둘 이상의 where 조건을 추가하려면 ->andWhere() 또는 ->orWhere()을 사용해야합니다. 그럼에도 불구하고 교리에서 명명 된 관계를 처리 할 때 교착 상태를 명시 적으로 알려주지 않아도됩니다. 스키마 파일 덕택에 이미 알고 있습니다.

더 많은 경우, 참여 조건을 확장해야한다면 WITH을 사용하여 쉽게 할 수 있습니다.

//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(); 
+0

답변 해 주셔서 감사합니다 !! 그 좋은 설명, 당신이 말한 짓을했지만 계속 같은 오류가 발생, 쿼리 괜찮아요,하지만 내가 조인 된 테이블의 필드에 액세스하려고하면 오류가 500 그리고 더 이상 아무것도 작동하지 .. 어떤 생각 될 수 있을까 ?? > 선택 ('*') \t \t \t \t - -> ('OhrmTrainningSubmit TS')에서 \t \t \t \t 나는이 \t \t \t \t $ Q = Doctrine_Query :: 생성() \t \t \t \t 같은 것을했다 -> innerJoin ('TS.OhrmUserU') \t \t \t \t -> addWhere ("TS.활동 등 = $ 훈련 ") \t \t \t \t -> addWhere ("TS.user = $ 직원 "); \t \t \t \t $은 = $ Q-결과> (실행) – jpganz18

+0

어떻게 당신이의 필드에 액세스 할 (당신은 당신의 포스트에 편집 할 수있다) 또한'where' 부분에서 매개 변수를 사용해야한다 :'where ("TS.training =? ', $ training)'where ("TS.training = $ training ")' –

+0

고마워! 내 업데이트를 확인해 주시겠습니까? 동일한 오류가 계속 발생했습니다. 첫 번째 테이블의 정보에는 액세스 할 수 있지만 조인 된 테이블의 정보에는 액세스 할 수 없습니다. – jpganz18