ZF2 TableGateway how to get Primary key

为君一笑 提交于 2019-12-13 18:39:33

问题


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 ?

Within AbstractTableGateway child:

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

getting

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

回答1:


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

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);        
}

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

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);
}

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

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

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

Cheers



来源:https://stackoverflow.com/questions/23428786/zf2-tablegateway-how-to-get-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!