Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组中与CodeIgniter DataMapper相关的对象_Php_Codeigniter_Codeigniter Datamapper - Fatal编程技术网

Php 数组中与CodeIgniter DataMapper相关的对象

Php 数组中与CodeIgniter DataMapper相关的对象,php,codeigniter,codeigniter-datamapper,Php,Codeigniter,Codeigniter Datamapper,我有一个Product\u faq类,它与Product类有$has\u许多关系。我有一个产品类,它与产品faq类有很多关系。这两个类使用product\u faqs\u products表连接 我试图将所有Product\u faq对象作为数组,包括每个对象上的“Product”属性,其中包含一个与该Product\u faq相关的Product对象数组。我现在是这样做的: $faqs = new Product_faq(); $faqs->where('deleted', 0)->

我有一个Product\u faq类,它与Product类有$has\u许多关系。我有一个产品类,它与产品faq类有很多关系。这两个类使用product\u faqs\u products表连接

我试图将所有Product\u faq对象作为数组,包括每个对象上的“Product”属性,其中包含一个与该Product\u faq相关的Product对象数组。我现在是这样做的:

$faqs = new Product_faq();
$faqs->where('deleted', 0)->get();

$data['faqs'] = array();

foreach($faqs as $faq){
        $faq_array = $faq->to_array();

        $products = new Product();
        $faq_array['product'] = $products->where_related($faq)->where_join_field($faq, 'deleted', 0)->get()->all_to_array();

        $data['faqs'][] = $faq_array;
}

print_r($data['faqs']);

我觉得使用DataMapper的内置功能可能有更好的方法来实现这一点,但我想不出来。有谁知道更有效的方法来实现这一点吗?

您是否考虑过在prodct\u faq类中设置属性auto\u populate\u很多?它会自动加载特定类上的所有关系,这很方便,但如果您有大量数据和许多关系,当然会消耗更多的资源。此外,关系表的标准名称应为product\u product\u faq

我可能会这样做:

class Product_faq extends DataMapper {
    ...
    $auto_populate_has_many = TRUE;
    ...
}

$faqs = (new Product_faq)->where('deleted',0)->get();

print_r($faqs->all_to_array());
如果你不想总是迫不及待地加载你的关系,你可以这样做:

$faqs = (new Product_faq)->where('deleted',0)->get();

$faqs->product->get();

print_r($faqs)->all_to_array());

谢谢jonixj。据我所知,
all_to_array()。它仅包括父模型中的数据库字段。你见过别的吗?我相信您对表名的看法是错误的。标准联接表名称应该是这些类的复数版本的组合。在我的例子中,正确的答案是
product\u faqs\u products