0

私の見解では、次のようなHTMLテーブルが必要です。

     COUNTRY         TOWN
      france         paris

これは私の質問です:

$foo=$this->country->find('all', array(
    'contain' => array(
            'Town' => array(
                'conditions' => array("Town.country_id = country.id"),
                'fields' => array('id','name')
            )
        )
    )
);

このように自分のビューに表示したい:

 line6        <?php foreach ($diponibilite as $f): ?>
 line7  
 line8            <tr>
 line9                <td><?php echo $f['country']['name'];?></td>
 line10             <td><?php echo $f['town']['name'];?></td>
 line11         
 line12            </tr>
 line13        <?php endforeach; ?>

モデル「国」と「町」は関連付けられています。

country hasmany town and town belongsto country

残念ながら、エラー:

注意(8):未定義のインデックス:名前[APP \ View \ index \ index.ctp、10行目]

なんで?

4

1 に答える 1

2

問題は、Country hasmany Town関係があるため、1 つの国に複数の町が存在する可能性があることです (debug( $f )ショーの出力のように)。

すべての町を印刷するには、別のループが必要です。

<?php foreach( $diponibilite as $f ): ?>
    <tr>
        <td><?php echo $f[ 'country' ][ 'name' ]; ?></td>
        <td><?php 
            foreach( $f[ 'town' ] as $town ) {
                echo $town[ 'name' ].' ';
            }
        ?></td>
    </tr>
<?php endforeach; ?>

または、最初のものだけが必要な場合は、を使用します$f[ 'town' ][ 0 ][ 'name' ]

補足: 関連付けが適切に設定されている場合、検索に条件は必要ありません。あなたはただすることができます

$foo = $this->country->find( 'all', array(
    'contain' => array(
            'Town.id',
            'Town.name'
        )
    )
);
于 2012-04-27T09:27:08.513 に答える