0

おそらく、例が私の問題を最もよく説明するでしょう:

スキーマ:

Referral:
  actAs:                            { timestampable: ~ }
  columns:
    id:                             { type: integer, primary: true, notnull: true, autoincrement: true, unique: true }  
    other_stuff:                    { type: string }  
    reasonCode:                     { type: integer }    
  relations:   
    ReasonCode:                     { local: reasonCode, foreign: id, foreignAlias: ReasonCodes }  

ReasonCode:
  columns:
    id:                             { type: integer, primary: true, notnull: true, autoincrement: true, unique: true }
    description:                    { type: string }

クエリ(referralTable.class.php):

    public function getObjectByReferralId($id){
        $q = Doctrine_Query::create()
            ->select('*')
            ->from('referral_submissions')
            ->where('referral_id=?', $id)
            ->fetchOne();
        return $q;   
    }

テンプレートを呼び出す:

<?php 
$id = <source of id>;
echo Doctrine_Core::getTable('referral')->getObjectByReferralId($id)->getReasonCode();
 ?>

理由コードを取得するためのテンプレートでの上記の呼び出しは、Referralテーブルに格納されているIDではなく、ReasonCodeテーブルに格納されている「説明」を返します。結合された説明ではなく、実際のIDが必要です。私は何が欠けていますか?

4

1 に答える 1

0

外部キーにリレーションの名前を付けたため、混乱を招きます。したがって、キーを取得していると思うときは、リレーションをフェッチします。ReasonCodeまた、テーブルにはフィールドが1つしかないため、Doctrineは主キーを取得しないと思います。そのため、descriptionフィールドが返されます。

試してみてください:

Doctrine_Core::getTable('referral')
  ->getObjectByReferralId($id)
  ->get('reasonCode');

ちなみに、リレーションを使用してIDを取得することもできます。

Doctrine_Core::getTable('referral')
  ->getObjectByReferralId($id)
  ->getReasonCode()
  ->getId();

reason_code_id外部キーは、の代わりに:のように定義する必要があると思いますreason_code。次に、スキーマは次のようになります。

Referral:
  actAs:                            { timestampable: ~ }
  columns:
    id:                             { type: integer, primary: true, notnull: true, autoincrement: true, unique: true }  
    other_stuff:                    { type: string }  
    reasonCode_id:                  { type: integer }    
  relations:   
    ReasonCode:                     { local: reasonCode_id, foreign: id, foreignAlias: ReasonCodes } 

そして、以下を使用してIDを取得できるようになります。

Doctrine_Core::getTable('referral')
  ->getObjectByReferralId($id)
  ->getReasonCodeId();
于 2012-07-12T14:04:54.670 に答える