从模型示例YII中获取模型名称

bnlyeluc  于 2022-11-09  发布在  其他
关注(0)|答案(5)|浏览(192)

如何从模型示例中获取模型名称?例如:
$model=新状态;
这里,State is model $model is State模型示例。
我想从$model即模型示例中获取模型名称即状态。

eh57zj3b

eh57zj3b1#

将此方法添加到状态类

public function getModelName()
{
    return __CLASS__;
}

并这样命名它:

$model = new State();
echo $model->getModelName();
2jcobegt

2jcobegt2#

get_class()-返回对象的类的名称
字符串get_class([对象$对象])
因此,您可以这样使用它:$模型名称=获取_类($模型示例);

  • 〉它返回一个字符串。
clj7thdc

clj7thdc3#

使用此PHP方法:get_class

print get_class($object);
wvyml7n5

wvyml7n54#

<?php

class Item extends CActiveRecord
{

    public function getBaseModelName()
    {
        return __CLASS__;
    }

    public function getCalledClassName()
    {
        return get_called_class();
    }
}

class Product extends Item {}

class Service extends Item {}

class ProductController extends CController
{
    $model = new Product;
    echo $model->baseModelName; // Item
}

class ServiceController extends CController
{
    $model = new Service;

    echo $model->calledClassName; // Service 
    echo get_class($model); // Service 
}
ktca8awb

ktca8awb5#

若要将State作为所询问的问题,您可以执行以下操作:-

basename(get_class($model))

相关问题