用于创建数据库条目的 yii 按钮 [英] yii button to create db entry

查看:33
本文介绍了用于创建数据库条目的 yii 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个按钮,只需单击一下即可在我的数据库中填充表格.

I would like to create a button that can be used to populate a table in my db with a single click.

我只是不确定我需要在这里做什么才能实现这一点.我可以指定一个按钮执行的方法吗?或者只是在我的控制器中获取了值?下面是类似于我想要执行的但通过按钮执行的操作.

I am just not sure what I need to do here to make this happen. Can I assign a method to be executed by a button? Or just have values picked up in my controller? Below is something like what I want to execute but through a button.

public function addInterest($interest)
    {
        $interest->UserId=Yii::app()->user->id;
        $interest->ItemId=$this->ItemId;
        return $interest->save();
    }

**回复 Jaison Justus 的其他详细信息

在这个实现中,我使用了来自模型 A (ItemId) 的控制器和视图,按钮将在其中显示.然后是模型 B(用户 ID).从模型 A (ItemId) 和模型 B (UserId) 获取信息,我想在单击按钮时使用该 ItemId 和 UserId 填充模型 C ($interest).看起来 CJuiButton 可能会提供一种构建它的方法,因为我可以在选择一次后禁用/隐藏该按钮.我只是不熟悉使用按钮而不是在收集用户输入的表单上,作为链接,或提供弹出消息.

With this implementation I am using controller and view from Model A (ItemId) where the button is to be displayed. Then there is Model B (UserId). Taking the info from Model A (ItemId) and Model B (UserId) I want to populate Model C ($interest) with that ItemId and UserId upon clicking a button. Looks like CJuiButton might provide a means to build it from being as then I can disable/hide the button after selected once. I am just not familiar with using buttons other than on a form where user input in collected, as links, or to provide pop up messages.

以上代码目前位于模型 A 模型中.如果我使用表单并收集输入,使用模型 A 控制器中的以下代码,一切都可以填充模型 C.由于我不需要任何输入,然后从用户那里选择按钮,因此表单没有任何内容可放入其中,因此我知道我不能使用 if(isset($_POST['Interest'])) 如下所示.

The code above currently sits in Model A model. With the code below in Model A controller everything works to populate Model C if I use a form and collect input. Since I do not require any input other then selecting the button from the user the form has nothing to put into it and therefore I know I can not use if(isset($_POST['Interest'])) as I have below.

public function actionView($id) {
        $items=$this->loadModel($id);
        $interest=$this->newInterest($items);

        $this->render('view', array(
            'model' => $items,
            'interest' => $interest,
        ));
    }

protected function newInterest($items)
    {
        $interest=new Interest;
        if(isset($_POST['Interest']))
        {
            $interest->attributes=$_POST['interest'];
            if($items->addInterest($interest))
                $this->refresh();
        }
        return $interest;
    }

回应 VarioN

In response to VarioN

这是使用ajax的尝试.但是,这不起作用并在运行时给出错误 500.我的控制器操作是否适合我在此处尝试执行的操作?

Here is an attempt at using ajax. However this does not work and gives an Error 500 when ran. Is my controller action appropriate for what I am trying to do here?

控制器

public function actionAddInterest() {
        $connection = yii::app()->db;
        $sql1 = "INSERT INTO interest (UserId, ItemId)
                    VALUES(".Yii::app()->user->id.",".$this->ItemId.")";
        $connection->createCommand($sql1)->execute();
    }

查看

<?php 
        echo CHtml::ajaxLink(
            'Add Interest',
            array('/item/addInterest'), 
            array('update'=>'#req_res')
        );
    ?>

推荐答案

看着你的问题,我发现你不明白 Yii 中的 MVC 是如何工作的.

Looking at your question I see that you don't understand how MVC in Yii works.

观看这个 15 分钟的截屏视频(Yii Tour - 第三站 - CRUD 县),之后您将能够以您需要的任何方式创建此类按钮(尝试使用 Gii,而不是以您的方式自定义它 - 这是最简单的方法).

Look at this 15 minutes screencast (Yii Tour - 3rd Stop - CRUD County) and after you will be able to create such button in any way you need (try use Gii and than customize it in your way - it's the easiest way).

更新:

看来您需要 AJAX 请求.您可以在视图中添加 CHtml::ajaxButton().

Seems that you need AJAX request. You can add CHtml::ajaxButton() in your view.

它会这样工作:

  1. 用户按下按钮,按钮向您的用户发出请求(使用 JavaScript)网站无需重新加载页面并且对用户不可见.

  1. User push the button, button do request (with JavaScript) to your site without reloading the page and invisible for user.

您的控制器操作将满足此请求:它可以执行一些操作(例如,将数据保存到数据库)并输出您的 JavaScript 可能会向用户显示的数据.

Your controller action will serve this request: it can make some things (for ex., save data to db) and output data that your JavaScript possibly will display to user.

比你的 JavaScript 得到答案并且可以在页面上做一些改变(例如,隐藏按钮或显示从请求中获取的文本).

Than your JavaScript get answer and can make some changes on the page (for example, hide button or show text got from request).

您可以在 此处查看 ajax 的简单示例

如果您不需要使用按钮提交表单信息,您可以使用 ajaxLink.它的例子是这里

If you needn't to submit form info with your button you can user ajaxLink. Example for it is here

在互联网和 yii 论坛上有很多关于 ajax 和 yii 的例子.尝试找到它们可能会很有帮助.

There are a lot of examples with ajax and yii in the internet and at yii forum. Try to find them it may be very helpful.

如果您有任何问题,请提出问题.

Ask questions if you would have any.

第二次更新:

首先,尝试更简单地进行 sql 查询:"插入兴趣 (UserId, ItemId) VALUES (1, 2)"

First, try to do your sql query simplier: "INSERT INTO interest (UserId, ItemId) VALUES (1, 2)"

启用 mysql 查询日志记录:在 config/main.php 中将trace"添加到levels"

Than enable logging of mysql queries to log: at config/main.php add "trace" to "levels"

'components'=>array(
    'log'=>array(
        'class'=>'CLogRouter',
        'routes'=>array(
            array(
                'class'=>'CFileLogRoute',
                'levels'=>'error, warning, trace',
            ),

现在您可以尝试按下 AJAX 链接并查看 protected/runtime/log.txt 并确定问题.

Now you can try to press an AJAX link and look at the protected/runtime/log.txt and determine the problem.

AJAX 请求的附加信息

所有输出 ajax 脚本的内容都可以通过浏览器的功能查看:在 Chrome 上:按 F12,转到网络,按 ajax 链接并查看请求响应.在带有插件Firebug"的 Firefox 中.

All that outputs your ajax scripts can be viewed by browser's features: At Chrome: press F12, go to Network, press an ajax-link and look at request response. At Firefox with addon "Firebug".

通过它您可以确定请求是否完成.

With this you can determine whether a request is done or not.

这篇关于用于创建数据库条目的 yii 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆