2010-06-29 105 views
1

我有一個對象時,通過print_rPHP訪問對象的屬性

Array 
    (
     [178] => My_Model_Category Object 
     (
      [autoGenerateURLNameIfNotSupplied] => 1 
      [id] => 178 
      [name] => Just for Kids 
      [date_created] => 2010-04-06 16:08:40 
      [last_updated] => 2010-06-29 10:29:50 
      [user_id_updated] => 0 
      [_table] => 
      [_aliases] => Array 
       (
        [id] => 178 
        [name] => Just for Kids 
        [date_created] => 2010-04-06 16:08:40 
        [date_updated] => 2010-06-29 10:29:50 
        [user_id_updated] => 0 
        [parent_id] => 
        [url_name] => just-for-kids 
        [description] => 
        [image_id] => 
        [image_id_teaser] => 109 
        [cat_usage] => recipes 
        [rank] => 
        [note] => 
       ) 

      [_nonDBAliases] => Array 
       (
       ) 

      [_default] => Array 
       (
       ) 

      [_related] => Array 
       (
        [_related] => Array 
         (
          [image] => stdClass Object 
           (
            [key] => image 
            [group] => _related 
            [foreignKey] => image_id_teaser 
            [indexName] => id 
            [tableName] => jm_asset 
            [objectName] => Common_Model_Asset 
            [userFieldlyColName] => name 
            [criteria] => id='{%image_id_teaser%}' 
            [sqlPostfix] => ORDER BY rank ASC 
            [populateOnLoad] => 1 
            [objects] => Array 
             (
              [109] => Common_Model_Asset Object 
               (
                [id] => 109 
                [name] => 
                [date_created] => 2010-03-29 15:07:25 
                [last_updated] => 2010-03-29 15:07:25 
                [user_id_updated] => 0 
                [_table] => 
                [_aliases] => Array 
                 (
                  [id] => 109 
                  [name] => 
                  [date_created] => 2010-03-29 15:07:25 
                  [date_updated] => 2010-03-29 15:07:25 
                  [user_id_updated] => 0 
                  [asset_usage] => 
                  [url] => /x/img/dyn/recipe/my-recipe-26-image.jpg 
                  [type] => recipe_image 
                  **[filename] => my-recipe-26-image.jpg** 
                  [fileext] => .jpg 
                  [filesize] => 
                  [width] => 250 
                  [height] => 250 
                  [scale] => 
                  [rank] => 
                  [note] => 
                 ) 


) 

我試圖訪問圖像文件名(見星號的條目),我已經試過這個輸出看起來像這樣目前無濟於事,

print_r($this->recipeCategories->_related->_related->images); 
+0

它看起來像來自ORM的對象。相應的文檔應該揭示如何正確訪問屬性。我認爲你不應該直接訪問它們。 – 2010-06-29 10:05:50

回答

1
$this->recipeCategories->_related['_related']['image'] 

或(很難說什麼是什麼恰好你貼什麼):

$this->recipeCategories[178]->_related['_related']['image'] 

_related是一個數組,你不能像對象一樣訪問它。請仔細按照您在print_r輸出中看到的內容。如果它說Object,您需要訪問->的兒童,如果它說Array,請使用[]

1

這是數組和對象的混合物。爲了得到image對象的objects屬性,請使用

嘗試

print_r($this->recipeCategories[178]->related["_related"]["image"]->objects);