【问题标题】:CodeIgniter DataMapper include_related only including one related modelCodeIgniter DataMapper include_related 只包含一个相关模型
【发布时间】:2014-05-23 13:27:10
【问题描述】:

我有一个 Product_faq 类,它与 Product 类有 $has_many 关系。我有一个 Product 类,它与 Product_faq 类有 $has_many 关系。这两个类使用product_faqs_products 表连接。

我正在尝试使用以下代码来检索特定的 Product_faq 以及与之关联的产品:

$faqs = new Product_faq();
faqs->include_related('product', null, true, true)->get_by_id(25);

foreach($faqs->product as $product){
    echo $product->id.PHP_EOL;
}

与此product_faq 相关的产品有3 个,但此代码仅输出第一个。这是预期的功能吗?有什么方法可以让我在此查询中包含所有相关产品?

【问题讨论】:

    标签: php codeigniter codeigniter-datamapper


    【解决方案1】:

    get_by_id 函数返回一行,因为得到 product_faqs_products.id 主键是 25。

    尝试where 子句和foreign_key 如下:

    $faqs->include_related('product', null, true, true)->where('product_id', 25);
    

    【讨论】:

    • 嗨波拉。我不认为这是正确的。此处使用get_by_id 是因为我只得到一个Product_faq,但是,我希望该对象包含一个“产品”属性,该属性是与此Product_faq 相关的三个产品对象的数组。例如,这样做可以:$faqs = new Product_faq();$faqs->get_by_id(25);$faqs->product->get();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    相关资源
    最近更新 更多