2012-02-01 25 views
1

我是新的代碼點火器數據映射器。我有一個名爲user的表,我試圖從數據庫表中檢索數據並將它們顯示給用戶。 這裏是我的模型:如何在DataMapper Code Igniter中顯示get_by_user_id()返回的數據?

$u=new User(); 
$results=$u->get_by_user_id($id); 
    //$results here will be set to huge bunch of none sense data(which also includes the  row that I am looking for as well) 

if ($u->exists()) 
{ 

foreach ($results->all as $row){ 
     $data['user']['first_name']=($row->user_first); //this where I am stuck .. 
    $data['user']['last_name']=($row->user_last);//this is also where I am stuck.. 

} 

我不知道如何對待結果得到一個必填字段我要找並將它們儲存在我傳遞給用戶來查看數據$ 。 謝謝!

回答

1

當你在模型調用get_by_x(),字段將被填充數據,你可以像這樣訪問他們:

$u = new User(); 
$u->get_by_user_id($id); 
if($u->exists()) 
{ 
    // you can access the table columns as object fields 
    $data['user']['first'] = $u->first; 
    $data['user']['last'] = $u->last; 
} 
else 
{ 
    $data['error'] = 'No such user!'; 
} 

看一看這是非常有用的文檔:看Get並獲得通過。

此外,DataMapper預計所有表格都有一個id列:請參閱Table Naming Rules。如果您的專欄名稱爲id,則應該撥打$u->get_by_id($id)而不是$u->get_by_user_id($id)

+0

非常感謝它的作品@Armon Toubman! – 2012-02-01 20:01:28