代码里面的findByAttributes正常,findAllByAttributes报错

controllers的代码如下:




    public function actionTest()

    {

        $invoice = new Invoice();

        $invoice->code = "";

        $invoice->productId = 17;

        $invoice->buyId = 13;

        $invoice->price = "50.00";

        $invoice->qty = 10;

        $invoice->preapprovalKey = "11111111";

        $invoice->approvedStatus = 1;

        $invoice->status = "OK";

        $invoice->senderEmail = "test@test.com";

        $invoice->payStatus = 0;

        $invoice->save();

        echo $invoice->id;


        Invoice::model()->isNewRecord = true;


        $loopInvoice = Invoice::model()->findByAttributes(

            array(

                "payStatus"=>0,

                "productId"=>17,

                "status"=>"OK",

                "approvedStatus"=>1,

                "preapprovalKey"=>"11111111",

            )

        );

        echo $loopInvoice->id;

    }



这个时候,代码正常,

但是清空数据表,把上面的findByAttributes() 改成 findAllByAttributes(),就报错了。




PHP Error

Description


Trying to get property of non-object

Source File


/media/MyDocuments/htdocs/tippple/protected/controllers/PaypalController.php(130)


00118: 

00119:         Invoice::model()->isNewRecord = true;

00120: 

00121:         $loopInvoice = Invoice::model()->findAllByAttributes(

00122:             array(

00123:                 "payStatus"=>0,

00124:                 "productId"=>17,

00125:                 "status"=>"OK",

00126:                 "approvedStatus"=>1,

00127:                 "preapprovalKey"=>"11111111",

00128:             )

00129:         );

00130: echo $loopInvoice->id;

00131:         //CVarDumper::dump($loopInvoice, 10, true);

00132:     }

00133: 


Stack Trace


#0 /media/MyDocuments/htdocs/tippple/framework/web/actions/CInlineAction.php(32): PaypalController->actionTest()

#1 /media/MyDocuments/htdocs/tippple/framework/web/CController.php(300): CInlineAction->run()

#2 /media/MyDocuments/htdocs/tippple/framework/web/CController.php(278): PaypalController->runAction()

#3 /media/MyDocuments/htdocs/tippple/framework/web/CController.php(257): PaypalController->runActionWithFilters()

#4 /media/MyDocuments/htdocs/tippple/framework/web/CWebApplication.php(320): PaypalController->run()

#5 /media/MyDocuments/htdocs/tippple/framework/web/CWebApplication.php(120): CWebApplication->runController()

#6 /media/MyDocuments/htdocs/tippple/framework/base/CApplication.php(135): CWebApplication->processRequest()

#7 /media/MyDocuments/htdocs/tippple/index.php(12): CWebApplication->run()






是bug吗?还是我哪里操作错误了?

findAllByAttributes是返回一个数组-array(CActiveRecord, CActiveRecord)

findByAttributes返回一个CActiveRecord对象。

所以你在使用findAllByAttribute方法返回的结果上,访问其属性,会得到一个non-object的错误!

同时,你清空数据表,返回的是array().

你想访问空数组的id property吗?

debug的信息已经很明显了。

谢谢你的回答~