2014-06-09 51 views
0

我涉及的Symfony2 2幾個對象與方法Symfony的2如何訪問對象(stdClass的)

\Doctrine\Common\Util\Debug::dump($article); 

我有

array(1) { [0]=> object(stdClass)#267 (19) { ["__CLASS__"]=> string(30) "obbex\AdsBundle\Entity\AdsList" ["id"]=> int(33) ["username"]=> string(7) "aviator" ["email"]=> string(17) "[email protected]" ["telephone"]=> string(4) "5161" 
["displayPhone"]=> string(3) "non" ["title"]=> string(14) "aviator jacket" ["description"]=> string(14) "aviator jacket" ["country"]=> string(6) "France" 
["region"]=> string(13) "Ile de France" ["department"]=> string(5) "Paris" ["address"]=> string(15) "15, rue Dantzig" ["city"]=> string(5) "Paris" ["zipCode"]=> 
string(5) "75015" ["statusPro"]=> string(3) "oui" ["creationtime"]=> string(8) "DateTime" ["updatetime"]=> string(8) "DateTime" ["publication"]=> bool(true) ["photos"]=> string(8) "Array(1)" } } 

\Doctrine\Common\Util\Debug::dump($article[0]); 
變量

我有訪問所述第一陣列時隙[0]

object(stdClass)#267 (19) { ["__CLASS__"]=> string(30) "obbex\AdsBundle\Entity\AdsList" ["id"]=> int(33) ["username"]=> string(7) "aviator" ["email"]=> string(17) 
"[email protected]" ["telephone"]=> string(4) "5161" ["displayPhone"]=> string(3) "non" ["title"]=> string(14) "aviator jacket" ["description"]=> string(14) "aviator jacket" ["country"]=> string(6) "France" ["region"]=> string(13) "Ile de France" 
["department"]=> string(5) "Paris" ["address"]=> string(15) "15, rue Dantzig" ["city"]=> string(5) "Paris" ["zipCode"]=> string(5) "75015" ["statusPro"]=> string(3) "oui" ["creationtime"]=> object(stdClass)#365 (3) { ["__CLASS__"]=> string(8) "DateTime" ["date"]=> string(25) "2014-06-06T21:54:00+02:00" ["timezone"]=> string(12) "Europe/Paris" } 
["updatetime"]=> object(stdClass)#366 (3) { ["__CLASS__"]=> string(8) "DateTime" ["date"]=> string(25) "-001-11-30T00:00:00+00:09" ["timezone"]=> string(12) "Europe/Paris" } ["publication"]=> bool(true) ["photos"]=> array(1) { [0]=> string(29) "obbex\AdsBundle\Entity\Photos" } } 

如何訪問的第一個對象(stdClass的) 'obbex \ AdsBundle \實體\ AdsList' 我試過

\Doctrine\Common\Util\Debug::dump($article[0]->{'obbex\AdsBundle\Entity\AdsList'}); 

\Doctrine\Common\Util\Debug::dump($article[0]->'obbex\AdsBundle\Entity\AdsList'); 

沒有成功。

回答

1

我認爲你的問題在於你如何(去)序列化你的對象。我假設你想要一個obbex\AdsBundle\Entity\AdsList -object而不是一個stdClass -object。

你可以做的是,檢查__CLASS__,然後創建從經過stdClass -object在構造一個新的對象來傳遞數據:

$rawData = (array) $article[0]; // Convert stdClass to array for easier handling 
$className = $rawData['__CLASS__']; 
// TODO Maybe check, that class name is valid 
$object = new $className($rawData); 

你的類,那麼只需要填充它的屬性在構造函數中的數組:

namespace obbex\AdsBundle\Entity; 

class AdsList 
{ 
    protected $username; 
    protected $email; 
    ... 

    public function __construct(array $data = array()) 
    { 
     if (empty($data)) { 
      return; 
     } 

     // TODO Check only valid properties/values are used 
     $this->username = $data['username']; 
     ... 
     // OR 
     foreach ($data as $name => $value) { 
      $this->{$name} = $value; 
     } 
    } 
} 

你可以通過確保您檢索實際的對象,而不是stdClass,例如保存自己的一些麻煩通過確保serialize()deserialize(string $serialized)被實現/使用。