Yii - 从不同模型的属性值设置一个模型的属性

时间:2015-08-20 21:20:44

标签: php yii

我正在尝试(尝试不同的方法)获取一个模型的属性,以使用它们来设置不同模型的属性。

我有一个“节点”模型,它具有进入“跳出日志”模型所需的一些属性。但是我不断收到错误“试图获取非对象的属性”。我不明白这个错误,我的印象是$ node实际上是一个对象,当时正在使用它。

以下是代码:

NodeBouncerController.php

//..
public function actionConfirm($id) {
        $node = new Nodes("nodesearch");
        $node->unsetAttributes();
        $model = Nodes::model()->findAllByPk($id);
        $node = $model;

        $logmodel = new BounceLog("logsearch");
        $logmodel->unsetAttributes();  // clear any default values
        print_r($_GET);
        if (isset($_GET['BounceLog']))
            $logmodel->attributes = $_GET['BounceLog'];
        if (!isset($node, $logmodel)) {
            throw new NotFoundHttpException("The node was not found.");
        }
        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($logmodel);

//This is where I tried various methods to see what would work

        if (isset($_POST['BounceLog'])) {
            $logmodel->attributes = $_POST['BounceLog'];
            $logmodel->setAttribute('node_Geolocation', $node->url);
            $logmodel->node_Name = (string)$node->name;
            $logmodel->node_Type = (string)$node->node_type;
            $logmodel->whenBounced = time();
            $logmodel->whoBounced = "Test User";
            if ($logmodel->save())
                $this->redirect(array('log'));
        }
        $this->render('view', array('model' => $this->loadModel($id), "logmodel" => $logmodel));
    }
//...

模型

Nodes.php属性

//...
public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'name' => 'Name',
            'url' => 'Url',
            'description' => 'Description',
            'node_type' => 'Node Type',
            'last_bounced' => 'Last Bounced',
            'isonline' => 'Online',
        );
    }
//...

BounceLog.php属性

//...
public function attributeLabels()
    {
        return array(
            'id' => '#',
            'node_Name' => 'Name',
            'node_Type' => 'Type',
            'node_Geolocation' => 'Geolocation',
            'whoBounced' => 'Who Bounced',
            'whenBounced' => 'When Bounced',
            'reasonBounced' => 'Reason Bounced',
        );
    }
//...

请注意我的公司使用Yii v1.1.8.r3324

我仍然是Yii和PHP的新手,但我对它的掌握比几周前更好。我只是不明白为什么这个特殊需要没有按照我的预期运作。

1 个答案:

答案 0 :(得分:0)

Trying to get property of non-object这意味着您可以获得其他类型的变量而不是对象。

  1. 您将获得方法findAllByPk的节点,这些节点返回对象数组,如果没有结果则返回空数组。如果包含节点的表具有唯一的PK,则必须使用findByPk,它将在空或对象上返回null。
  2. 然后你检查变量是否!isset($node, $logmodel)。此函数仅确定变量是否已设置且不为NULL。当$ node获取空数组或带有对象的数组时,$ logmodel总是对象,这个检查是不可用的。
  3. 解决方案1 ​​

    public function actionConfirm($id){
        $node = Nodes::model()->findAllByPk($id);
        if ( empty( $node ) ) { // empty will determines that returned array isn't empty
            throw new NotFoundHttpException('The nodes was not found.');
        }
    }
    

    解决方案2.(截至你指出node not found不是节点,并且有关于非对象访问的错误)

    public function actionConfirm($id){
        $node = Nodes::model()->findByPk($id);
        if ( $node === null ) {
            throw new NotFoundHttpException('The nodes was not found.');
        }
    }
    
相关问题