gpt4 book ai didi

yii - yii 中的 ActiveRecord 和 model() 之间的区别?

转载 作者:行者123 更新时间:2023-12-04 11:11:58 26 4
gpt4 key购买 nike

YII 中的 ActiveRecord 和模型之间有什么关系或区别?

我试图登录 is_object(CActiveRecord::model('Project'));并期待 false但它返回 true ;

由于日志记录表明它是一个对象,我认为它代表表中的一行,但我找不到任何代表列的属性。

还有 http://www.yiiframework.com/doc/api/1.1/CActiveRecord#model-detail声明它正在返回 CActiveRecord 的实例class ,但我在该对象中找不到表行的任何值。

最佳答案

答案在您的文档链接中,model() 是一个类级方法,它:

Returns the static model of the specified AR class. The model returned is a static instance of the AR class. It is provided for invoking class-level methods (something similar to static class methods.)



假设您这样做: $model=CActiveRecord::model('Project'); ,然后使用该 $model 您可以调用 CActiveRecord 的所有类级方法,例如:
$allModels = $model->findAll(); // will give you all the models of Project
$someModel = $model->findByPk('pkValue'); // will give you the row with primary key value = pkValue
$model->deleteAll(); // will delete all the records of Project
// and so on

编辑:

论坛中的 this post 也说:(类级别和静态方法之间的区别)

Class Level Methods are effectively Static Methods BUT with the benefit of being able to use inheritance. That is, you can override the operation of a Class Level Method in a subclass, whereas if you used a static method you would not be able to override it. .... So, in general, you should use class level methods, not static methods, as it gives you the benefit of inheritance although it might feel a little weird. Then you call them using $class::model()->method().

关于yii - yii 中的 ActiveRecord 和 model() 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11131649/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com