2015-12-29 67 views
0

我是新来pimcore并创建了一个对象类 - 这里的代码片段保存记录时将获得场“称号”:pimcore对象类:获取所有数据

class MagentoBaseProduct extends Concrete { 

    public function getTitle() { 
     $preValue = $this->preGetValue("title"); 
     if($preValue !== null && !\Pimcore::inAdmin()) { 
      return $preValue; 
     } 
     $data = $this->title; 
     return $data; 
    } 
} 

我想知道如果有没有得到整个对象,以便我将一个数组中的所有字段(而不是分别获取每个字段)?

感谢

+0

出于好奇,这将是对这个用例? – GNi33

回答

1

你可以使用PHP的自省能力,以获得在对象吸气的名单,然后依次访问每个吸气得到的值,并建立从这个数组。记住值可能不是简单的字符串 - 它们可能是其他对象,字段集合或Pimcore允许的其他内容。

$myObj = \Object\MagentoBaseProduct::getById(123); 
$reflection = new \ReflectionClass($myObj); 
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); 

foreach ($methods as $method) { 
    $methodName = $method->getName(); 
    if (substr($methodName, 0, 3) == 'get') { 
     // do stuff to add to array here 
    } 
} 

http://php.net/manual/en/book.reflection.php

1

下应该做的伎俩很容易:

$data = []; 
$myObj = \Object\MagentoBaseProduct::getById(123); 
foreach($myObj->getClass()->getFieldDefinitions() as $fieldDefionition) { 
    $data[$fieldDefinition->getName()] = $myObj->getValueForFieldName($fieldDefinition->getName()); 
}