使用 Atk4 从数据库加载数据
说实话,对于我这样的菜鸟来说,使用 Atk4 是一次伟大的冒险。现在我真的遇到了一个我自己无法解决的问题。 我在 MySQL 数据库中有两个表。第一个名为 user (id, username, email),第二个名为 trips (id, user_id, name)。我已经为用户制作了登录和注册表单。我希望登录的用户能够看到自己的行程。我曾经使用以下代码为其配置文件信息制作此东西:
<?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'));
}
}
我必须对 Model_trips 执行类似的操作,但我不知道是什么。我尝试过 来自 Atk4 网站的示例:
// Traverse foreign keys. Automatically loads proper model and data
$company=$emp->getRef('company_id');
这是我编写的最后一个代码:
<?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'));
}
}
To be hones working with Atk4 is a great adventure for such a rookie as I am. and now i have really a problem i can't solve by myself.
I have two tables in MySQL database. The first one is named user (id, username, email) and the second one is named 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. i have tried with
that example from Atk4 website:
// 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'));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您非常接近:
之后您可以在 CRUD、MVCGrid、MVCForm 或 MVCLister 中使用模型,将应用以下规则:
有时我添加功能:
然后您可以使用以下功能。
如果您想查看其他用户的行程,这会很方便。
You are very close:
Afterwards you can use model inside CRUD, MVCGrid, MVCForm or MVCLister, the following rule will apply:
Sometimes I add function:
Then you can make use the following.
Handy if you want to see other users trips.