本文介绍了ZF2 TableGateway 如何获取主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MetadataFetature 确定表的主键.但是,数据在受保护的 $sharedData 内,没有任何访问方法.如何访问它们?我是否创建新类只是为了添加getPrimary"方法?

I'm trying to determine primary key of table using MetadataFetature. However, the data are within protected $sharedData without any access method. How to acceess them ? Do I have create new class just to add "getPrimary" method ?

在 AbstractTableGateway 子项中:

Within AbstractTableGateway child:

$metadata = $this->getFeatureSet()->getFeatureByClassName('Zend\Db\TableGateway\Feature\MetadataFeature');
        die(vardump($this->sharedData));

得到

Invalid magic property access in Zend\Db\TableGateway\AbstractTableGateway::__get()

推荐答案

你只需要声明一个 FeatureSet 的后代,以及一个 MetadataFeature 的后代.

You just have to declare a descendant of FeatureSet, and a descendant of MetadataFeature.

在您自己的FeatureSet中,覆盖函数canCallMagicGet.这是我如何做到的一个例子

In your own FeatureSet, override function canCallMagicGet.Here's an example of how I did it

public function canCallMagicGet($property)
{
    if ($property == 'metadata'){
        $result = $this->getMetadataFeature();
        return ($result!==FALSE);
    }
    else return parent::canCallMagicGet($property);
}

在我的例子中,getMetadataFeature 返回在从 FeatureSet 类继承的受保护属性 $features 中找到的第一个 MetadataFeature 实例 (instanceof).

In my case, getMetadataFeature return first occurrence of MetadataFeature instance (instanceof) found in protected property $features inherited from the FeatureSet class.

您也可以覆盖第二种方法,callMagicGet.下面是一个示例:

You may also override this second method, callMagicGet.Here's an example how:

public function callMagicGet($property)
{
    if ($property == 'metadata'){
        $metadataFeature = $this->getMetadataFeature();
        return $metadataFeature->metadata;
    }
    else
        return parent::callMagicGet($property);
}

最后,您可以在自己的 MetadataFeature 中声明一个魔法方法 __get.您的 __get 可能会直接从受保护的属性 $sharedData 返回 metadata.

Finally you can declare a magic method __get in your own MetadataFeature. Your __get may return metadata directly from protected property $sharedData.

总而言之,一旦您声明了自己的FeatureSetMetadataFeature 后代,您就可以使用它们代替当前的后代.然后你也许可以打电话

In conclusion, once you have declared your own FeatureSet and MetadataFeature descendants, you may use them instead of current ones.Then you may be able to call

$tableGatewayInstance->metadata

您可以使用MetadataFeature收集的元数据.

and the metadata collected by MetadataFeature will be available to you.

干杯

这篇关于ZF2 TableGateway 如何获取主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 13:05