【问题标题】:Yii $this not working in modelYii $this 在模型中不起作用
【发布时间】:2012-08-23 04:23:57
【问题描述】:

我对 Yii 还很陌生,有一个小问题,但无法解决。问题是,当我在我的一个模型(活动记录)中调用 Yii 时,$this->attributes = 'something';我收到错误“未定义属性“SiteController.attributes”。”

我在控制器中有这个:

public function actionIndex()
{
    // Create new clients active record
    $client = new Clients;

    // Check if user send some request
    if (isSet($_POST)){
        switch($_POST["action"]){
            case 'newClient':
                $registered = $client::addClient($_POST);
        }
    }

    // render the view
    $this->render('landing',array(
        // Objects
        'client' => $client,
        // Variables
        'registered' => $registered,
    ));

    return true;
}

模型中的这个:

public function addClient($data){
    // Set data
    $this->attributes = $data["Clients"];
    $this->password = self::generatePassword(6);

    // Proceed
    $this->setScenario('insert');

    return true;
}

功能当然不完整,但这是我得到错误的地方。我到底做错了什么?谢谢

【问题讨论】:

    标签: php object activerecord yii


    【解决方案1】:

    您将函数作为静态方法调用。

    $registered = $client::addClient($_POST);

    应该是

    $registered = $client->addClient($_POST);
    

    【讨论】:

      【解决方案2】:

      当这样的事情发生在我身上时,我会使用 var_dump()。也许this不是你想的那个this

      否则我建议检查 Yii 生成的代码。几乎不熟悉该框架,但请检查您或您的 IDE 是否没有弄乱任何 C 样式的 cmets。检查拼写错误、代码、元数据、表格。还要检查并确保您还没有同名的实体,SiteController 听起来有点笼统。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多