gpt4 book ai didi

php - 使用 with 语句时,使用 AR findAll 函数只返回一个对象

转载 作者:可可西里 更新时间:2023-10-31 23:41:53 26 4
gpt4 key购买 nike

我在安装 Yii 时遇到问题,我试图返回一个相当基本的查询,但我没有得到在线教程说我应该得到的结果。我有 2 个模型,大致如下所示:

定价:

class Pricing extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Pricing the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}

/**
* @return string the associated database table name
*/
public function tableName()
{
return 'pricing';
}

/**
* @return string the primary key
*/
public function primaryKey(){
return 'ID';
}

...

/**
* @return array relational rules.
*/
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(
'xpricing_routes' => array(self::HAS_MANY, 'PricingRoutes', 'ID_pricing'),
);
}

和定价路线:

class PricingRoutes extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return PricingRoutes the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}

/**
* @return string the associated database table name
*/
public function tableName()
{
return 'pricing_routes';
}

/**
* @return string the primary key
*/
public function primaryKey(){
return 'ID';
}
...
/**
* @return array relational rules.
*/
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(
'xpricing' => array(self::BELONGS_TO, 'Pricing', 'ID_pricing'),
);
}

然后在 Controller 中我们有:

$criteria = new CDbCriteria;
$criteria->with = array('xpricing_routes');
$criteria->together=true;

$pricing_records = Pricing::model()->findAll($criteria);
$pricing_records_arr = CHtml::listData($pricing_records, 'id', 'name');

echo '<pre>';
print_r($pricing_records);
print_r($pricing_record_arr);
echo '</pre>';

您可能已经知道,我们有 2 个名为 pricing 和 pricing_routes 的表。定价路由表有一个名为 ID_pricing 的外键,它转到定价表中的 ID 字段。定价表有一个条目,pricing_routes 表有 4 个条目,它们都在 ID_pricing 字段中具有定价表中一项的主键。所以我们应该为我们正在运行的查询获得 4 个结果,当我运行 Yii 使用 AR 生成的查询时,这就是我得到的结果。

我们遇到的问题是 $pricing_records 变量是一个只有一个 Pricing 对象的数组。该对象包含我们需要的数据,但并不是真正可用的数据。 $pricing_records_arr 变量只是一个空数组。我找到的文档似乎建议我们应该获取一组定价对象,每个对象也包含来自 pricing_routes 表的信息。我们知道使用 AR 可能不是获取这些数据的最佳方式,但我们有理由让它发挥作用,因此我们将不胜感激关于如何做到这一点的任何想法。

编辑:

事实证明,这最终是对我要返回的内容的误解。对这个问题的评论给了我需要的信息。

最佳答案

如果你打电话

$pricing_records = Pricing::model()->findAll($criteria);

您只会获得一个事件记录,其属性填充了表“定价”中的值。如果你想从属于这个特定“定价”的“pricing_routes”中获取所有记录,你必须调用

$pricing_records->xpricing_routes

其中“xpricing_routes”是您在模型中正确定义的关系的名称。它返回 PricingRoutes 数组,如果没有匹配的记录则返回 null。

关于php - 使用 with 语句时,使用 AR findAll 函数只返回一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14587848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com