使用 Atk4 从数据库加载数据

发布于 2024-12-09 08:31:41 字数 1023 浏览 0 评论 0原文

说实话,对于我这样的菜鸟来说,使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞觅青森が 2024-12-16 08:31:41

您非常接近:

$model = $this->add('Model_trips');
$model->setMasterField('user_id', $this->api->auth->get('id'));

之后您可以在 CRUD、MVCGrid、MVCForm 或 MVCLister 中使用模型,将应用以下规则:

  • 列出时,仅显示属于当前用户的行程
  • 添加时,user_id 将设置为当前用户的 id

有时我添加功能:

class Model_User extends Model_Table {
    function getTrips(){
         return $this->add('Model_trips')
              ->setMasterField('user_id',$this->get('id'));
    }
}

然后您可以使用以下功能。

$model = $this->add('Model_user')->loadData($user_id)->$getTrips();

如果您想查看其他用户的行程,这会很方便。

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:

  • When listing, only trips belonging to current user will be shown
  • When adding, user_id will be set to current user's id

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文