[英]Loading data from Database with Atk4
To be hones working with Atk4 is a great adventure for such a rookie as I am. 对于像我这样的菜鸟来说,与Atk4一起工作是一种伟大的冒险。 and now i have really a problem i can't solve by myself.
现在我真的有一个我自己无法解决的问题。 I have two tables in MySQL database.
我在MySQL数据库中有两个表。 The first one is named user (id, username, email) and the second one is named trips (id, user_id, name).
第一个命名为用户(id,用户名,电子邮件),第二个命名为trips(id,user_id,name)。 I have made a login and register form for users.
我已经为用户制作了一个登录和注册表格。 I want a logged user to be able to see it's own trips.
我希望登录的用户能够看到它自己的行程。 I used to make this thing for its profile information using the following code:
我以前使用以下代码将其用作配置文件信息:
<?php
class page_userprofile extends Page{
function init(){
parent::init();
$this->api->auth->check();
$model = $this->add('Model_user');
$this->add('FormAndSave')->setModel($model)->loadData($this->api->auth->get('id'));
}
}
I have to do something similar with Model_trips but I do not know what. 我必须对Model_trips做类似的事情,但我不知道是什么。 i have tried with that example from Atk4 website:
我已经尝试过从Atk4网站获取该示例:
// Traverse foreign keys. Automatically loads proper model and data
$company=$emp->getRef('company_id');
This is the last code I have written: 这是我最后编写的代码:
<?php
class page_mytrips extends Page{
function init(){
parent::init();
$this->api->auth->check();
$model = $this->add('Model_trips');
$this->add('FormAndSave')->setModel($model)->loadData($this->getRef('user_id'));
}
}
You are very close: 您非常接近:
$model = $this->add('Model_trips');
$model->setMasterField('user_id', $this->api->auth->get('id'));
Afterwards you can use model inside CRUD, MVCGrid, MVCForm or MVCLister, the following rule will apply: 之后,您可以在CRUD,MVCGrid,MVCForm或MVCLister中使用模型,将适用以下规则:
Sometimes I add function: 有时我添加功能:
class Model_User extends Model_Table {
function getTrips(){
return $this->add('Model_trips')
->setMasterField('user_id',$this->get('id'));
}
}
Then you can make use the following. 然后,您可以使用以下内容。
$model = $this->add('Model_user')->loadData($user_id)->$getTrips();
Handy if you want to see other users trips. 如果您想查看其他用户的旅行,则非常方便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.