は500

2016-09-01 8 views
0

種のモデルは、私はちょうどprint_r($species)、それは内部のサンプル値を示した場合、すべてのサンプルは、1種は500

$species= Species::model()->with('samples')->findAll(array('condition'=>'tax_id = :no','params'=>array(':no'=>$taxno))); 
print_r($species); 
$samples=$species->samples; //error here 
print_r($samples); 

に属し取得したい

public function relations() 
{ 
    // NOTE: you may need to adjust the relation name and the related 
    // class name for the relations automatically generated below. 
    return array(
     'samples' => array(self::HAS_MANY, 'Sample', 'species_id'), 
    ); 
} 

含まれています。しかし、それは$samples$samples=$species->samples;で渡すことはできませんそれはerror 500を示しています。

+0

を提出しますか? – aslawin

+0

あなたはテーブル構造を投稿することができます。 –

答えて

0

findAllを使用してデータを取得しています。結果は配列形式になります。ループの場合は、値にアクセスする場合はを使用してください。 を使用して

foreach ($species as $specie) { 
    echo $specie->samples; 
} 

は、1つのレコードだけが、のfindAllは、複数のレコードを与えるを与える見つけます。

0

Species.phpモデルであなたの関係を変更することができます。

Species.phpモデル

public function relations() 
{ 
    // NOTE: you may need to adjust the relation name and the related 
    // class name for the relations automatically generated below. 
    return array(
     'samples' => array(self::BELONGS_TO, 'Sample', 'species_id'), 
    ); 
} 

ControllerName.phpは、このエラーには何のメッセージ

$species= Species::model()->with('samples')->findAll(array('condition'=>'tax_id = :no','params'=>array(':no'=>$taxno))); 
echo "<pre>"; 
print_r($species); 
//$samples=$species->samples; // You can not used direct object 
//print_r($samples); // 


foreach ($species as $key => $value) { 
    echo "<pre>"; 
    echo "Species Object"; 
    print_r($value->attributes); 
    echo "Samples Object"; 
    print_r($value->samples->attributes); 
} 
exit;