首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >yii CActiveRecord findAll出现错误500

yii CActiveRecord findAll出现错误500
EN

Stack Overflow用户
提问于 2016-09-01 18:04:07
回答 2查看 111关注 0票数 0

物种模型包含

代码语言:javascript
复制
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'),
    );
}

我想让所有的样本都属于一个物种

代码语言:javascript
复制
$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);

当我执行print_r($species)时,它会显示里面的样本值。但它不能传递给$samples=$species->samples;中的$samples,它显示的是error 500

EN

回答 2

Stack Overflow用户

发布于 2016-09-01 20:03:02

您正在使用findAll获取数据。因此结果将是数组格式。应该使用for loop或foreach来访问这些值。

代码语言:javascript
复制
foreach ($species as $specie) {
    echo $specie->samples;   
}

使用find将只显示一条记录,但findAll会显示多条记录。

票数 0
EN

Stack Overflow用户

发布于 2016-09-01 20:06:16

您可以在Species.php模型中更改您的关系。

Species.php模型

代码语言:javascript
复制
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文件

代码语言:javascript
复制
$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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39268549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档