Codeigniter - 获取相关数据行作为结构/对象,而不是数组 [英] Codeigniter - get related data rows as structures/objects, not array
问题描述
我是CodeIgniter的新手。
我已经为Pizza表和Ingredients表创建了模型。表格由另外的Pizza_Ingredients表格(其多对多关系)连接。
我的模型:
<?php
class Pizza extends DataMapper {
var $ table ='Pizza';
var $ has_many =
array(
'ingredients'=>
array(
'class'=>'ingredients',
'join_table'=>'pizza_ingredients'
)
);
}
类成分extends DataMapper {
var $ table ='Ingredients';
var $ has_many =
array(
'pizza'=>
array(
'class'=>'pizza',
'join_table '=>'pizza_ingredients'
)
);
}
?>
当我使用该代码获取数据时:
$ pm = new Pizza();
$ pm-> include_related('ingredients');
$ array = $ pm-> get();
获取重复了Pizza值的数组(看起来像是sql查询结果) >
margherita |奶酪
vegetariana |蔬菜1
vegetariana |我不能简单地在foreach上在数组上生成html表,像这样。 p>
我想获得对象,结构如下:
margherita :
- cheese
vegetariana:
- vegetable 1
- vegetable 2
允许我在披萨循环中使用其他foreach循环(含有成分)进行foreach循环(使用比萨)。
任何建议?
因此,搜索CI Datamapper DOC,我发现了一些东西这将有助于(http://datamapper.wanwizard.eu/pages/getadvanced.html),看看第一个例子!
翻译你的问题,应该像这是:
//创建披萨
$ pm = new Pizza ();
//不要使用include_related成分这里!
// Datamapper会为你做这个!
//记住以单数形式声明你的Ingredients类,而不是复数
//所以你的类应该是单数 - >成分
//你的数据库表应该是复数 - >成分
//作为CI Datamapper指南教授:)
//获取所有比萨饼
$ pm-> get();
//循环所有比萨
foreach($ pm as $ pizza)
{
//打印实际的披萨名称
echo $ pizza-> name。< br />;
//获取当前的比萨饼配料< - 这里是魔法来了!
$ pizza-> ingredient-> get();
//打印所有当前的比萨饼的成分
foreach($ pizza->成分作为$成分)
{
echo $ ingredient->名称。< br />;
}
}
这对我来说很好,
我真的很感激如果一些解决方案使用CI Datamapper花费较少的请求到DB。
如果你找到,请分享!
Im a newbie in CodeIgniter.
I have created model for Pizza table and Ingredients table. Tables are joined by additional Pizza_Ingredients table (its many to many relation).
My model:
<?php
class Pizza extends DataMapper {
var $table = 'Pizza';
var $has_many =
array(
'ingredients' =>
array(
'class' => 'ingredients',
'join_table' => 'pizza_ingredients'
)
);
}
class Ingredients extends DataMapper {
var $table = 'Ingredients';
var $has_many =
array(
'pizza' =>
array(
'class' => 'pizza',
'join_table' => 'pizza_ingredients'
)
);
}
?>
When I get data using that code:
$pm = new Pizza();
$pm->include_related('ingredients');
$array = $pm->get();
im getting array which has duplicated Pizza values (it looks like just sql query result).
margherita | cheese
vegetariana | vegetable 1
vegetariana | vegetable 2
I can't simple generate html table in "foreach" on array like this.
i want to get object, that has structure like this:
margherita:
- cheese
vegetariana:
- vegetable 1
- vegetable 2
that allows me to make foreach loop (with pizzas) with other foreach loop (with ingredients) inside "pizza loop".
I have to write this in PHP, or CodeIgniter/DataMapper can do something more for me?
Any advice?
解决方案 I'm just stuck in the same answer...
So, searching the CI Datamapper DOCs, I found something that will help (http://datamapper.wanwizard.eu/pages/getadvanced.html), look at the first example!
Translating to your problem, should be like this:
// Create pizza
$pm = new Pizza();
// DO NOT USE include_related to Ingredients Here !!!
//Datamapper will DO THIS for you!
// Remember to declare your Ingredients class in singular, not plural
// So your class should be in singular -> Ingredient
// And your DB Table should be in plural -> Ingredients
// As CI Datamapper Guide teaches :)
// Get all pizzas
$pm->get();
// Loop through all pizzas
foreach ($pm as $pizza)
{
// Print the actual pizza name
echo $pizza->name."<br/>";
// Get the current pizza's Ingredients <- Here is the magic comes!
$pizza->ingredient->get();
// Print all current pizza's Ingredients
foreach ($pizza->ingredient as $ingredient)
{
echo $ingredient->name."<br/>";
}
}
This work very well to me, but it continues to hit the db a lot of times, actually, one time for each pizza that your DB contains.
I really appreciate if some solution using CI Datamapper took less requests to DB.
Please share if you find it!
这篇关于Codeigniter - 获取相关数据行作为结构/对象,而不是数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文