2013-12-22 41 views
-2

出於某種原因,我不能爲我的生活找出/理解爲什麼這不能正常工作......我所要做的就是顯示在照片頁面發表評論。我在測試這個照片有2個評論,但我得到的是這樣的:注意:試圖獲取非對象的屬性 - 也不是foreach正常循環

Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/photo_gallery/public/photo.php on line 47 
NULL @ :: 

photo.php頁面簡單地調用由照片的身份證的意見,然後在foreach循環顯示它們:

<?php 
$photo = Photograph::findByID($_GET['id']); 
$comments = $photo->comments(); 
printr($comments); 
?> 
<div class="comments"> 
<?php if ($comments): ?> 
    <?php foreach ($comments as $comment): ?> 
    <p style="margin-bottom: 15px;"> 
     <?php echo htmlentities($comment->author); ?> @ <?php echo strip_tags($comment->created, '<strong><em><p>'); ?> :: <?php echo $comment->body; ?> 
    </p> 
    <?php endforeach; ?> 
<?php else: ?> 
    <p>No comments found.</p> 
<?php endif; ?> 
</div> 

當我的print_r對$意見的結果,只有第一個評論對象數組中顯示出來..照片類的

部分:

在MySQLiDatabase類
class Photograph extends DatabaseObject { 
    public $id; 

    function comments() { 
     return Comment::findCommentsOn($this->id); 
    } 
} 

部分:

public static function findByPrepare($sql = "", $params) { 
    global $db; 
    $result = $db->query($sql, $params); 
    foreach ($result as $row) { 
     printr($result); 
     return self::instantiate($row); 
    } 
} 

private static function instantiate($record) { 
    $className = get_called_class(); 
    $obj = new $className; 

    foreach ($record as $attribute => $val) { 
     if ($obj->hasAttribute($attribute)) { 
      $obj->$attribute = $val; 
     } 
    } 
    return $obj; 
} 
private function hasAttribute($key) { 
    // get_object_vars returns an assoc array with all attributes as keys and their current values as the value 
    $objVars = $this->attributes(); 
    return array_key_exists($key, $objVars); 
} 

protected function attributes() { 
    // return an array of attribute keys and their values 
    $attributes = array(); 
    foreach(static::$dbFields as $field) { 
     if (property_exists($this, $field)) { 
      $attributes[$field] = $this->$field; 
     } 
    } 
    return $attributes; 
} 

好像它是從findByPrepare()函數的foreach制止......

當我printr($結果)它顯示兩個評論在一個數組罰款,但是當我printr($行)它只顯示數組中的第一個評論......所以不知何故我需要獲得$行來循環每個結果。我曾嘗試循環數組中的自我::實例化($行),但隨後拋出另一個錯誤:致命錯誤:調用一個成員函數評論()非對象

* 在$ print_r的上評論(這只是想出個結果時,有實際2條評論)*

Comment Object 
(
    [id] => 1 
    [photo_id] => 1 
    [created] => 2013-12-20 16:37:02 
    [author] => Nate 
    [body] => This is a cool pic! 
) 

* 的var_dump($ comment->作者)顯示*

NULL @ :: 

* 的var_dump($評論)顯示*

object(Comment)#4 (5) { ["id"]=> int(1) ["photo_id"]=> int(1) ["created"]=> string(19) "2013-12-20 16:37:02" ["author"]=> string(4) "Nate" ["body"]=> string(19) "This is a cool pic!" } 

被請求的其餘代碼:

評論類:

class Comment extends DatabaseObject { 
    protected static $tableName = "comments"; 
    protected static $dbFields = array('id', 'photo_id', 'created', 'author', 'body'); 
    public $id; 
    public $photo_id; 
    public $created; 
    public $author; 
    public $body; 

    public static function findCommentsOn($photo_id = 0) { 
     $params = func_get_args(); 
     $sql = "SELECT * FROM ". self::$tableName ." WHERE photo_id = ? ORDER BY created ASC"; 
     $results = parent::findByPrepare($sql, $params); 
     return $results; 
    } 
} 

MySQLiDatabase類:

class MySQLiDatabase { 
     public $conn; 
     public $lastQuery; 
     public $stmt; 
     public $id; 
     public $username; 
     public $password; 
     public $first_name; 
     public $last_name; 
     private $params; 

     public function __construct() { 
      $this->connection(); 
     } 

     public function connection() { 
      $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME); 
      if ($this->conn->connect_errno) { 
       return "Failed to connect to MySQL: (" . $this->conn->connect_errno . ") " . $this->conn->connect_error; 
      } 
     } 

     public function query($sql, $params) { 
      $this->lastQuery = $sql; 
      $result = $this->prepareQuery($sql, $params); 
      if ($params) { $this->bindParams($params, $this->stmt); } 
      $this->stmt->execute(); 
      $results = $this->bindResults($this->stmt); 

      return $results; 
     } 

     private function prepareQuery($sql, $params) { 
      $this->stmt = $this->conn->prepare($sql); 
      $this->confirmQuery($this->stmt); 
      return $this->stmt; 
     } 

     public function bindParams($params, $stmt) { 
      $types = ""; 
      for($i = 0; $i < sizeof($params); $i++) { 
       $types .= "s"; 
      } 

      $array = array_merge(array($types), $params); 
      return call_user_func_array(array($this->stmt, 'bind_param'), $this->refValues($array)); 
     } 

     public function bindResults($stmt) { 
      if ($stmt->affected_rows === -1) { // SELECT 
       $meta = $this->stmt->result_metadata(); 
       $params = array(); 
       while ($field = $meta->fetch_field()) { 
        $params[] = &$row[$field->name]; 
       } 
       call_user_func_array(array($this->stmt, 'bind_result'), $params); 

       $results = array(); 
       while ($stmt->fetch()) { 
        $x = array(); 
        foreach ($row as $key => $val) { 
         $x[$key] = $val; 
        } 
        $results[] = $x; 
       } 
      } else { // INSERT UPDATE DELETE 
       $results = $stmt->affected_rows; 
      } 
      return $results; 
     } 
+0

您是否提供了註釋的var_dump或print_r? –

+0

當然,現在更新問題。 – Nate

+0

它在我看來像你迭代索引,ID,photo_id等,而不是實際的評論。這與您的評論相關,只有一條評論正在顯示。我不確定你使用的數據庫層是什麼,但我相當確定這裏的問題是數據庫被告知返回一行而不是所有行。 –

回答

0

我已經想通了!

首先,loushou提到的是正確的,但是,它還有一點點。

我在我的findByID()方法中缺少一個array_shift(我沒有在原始問題中發佈這個,所以很抱歉)。

public static function findByID($id) { 
    $params = func_get_args(); 
    $result = self::findByPrepare("SELECT * FROM ". static::$tableName ." WHERE id = ? LIMIT 1", $params); 
    return !empty($result) ? array_shift($result) : false; 
} 

沒有陣列移,它是把在一個數組照片對象,因此導致該「致命錯誤:調用一個成員函數評論()上的非對象」錯誤。一旦我添加了陣列移位,它似乎修復了一切!感謝所有的反饋。

2

這是錯誤的:

public static function findByPrepare($sql = "", $params) { 
    global $db; 
    $result = $db->query($sql, $params); 
    foreach ($result as $row) { 
    printr($result); 
    return self::instantiate($row); 
    } 
} 

你的回報永遠只能返回一行。假設您想要返回所有結果的數組。你需要做的是這樣的:

public static function findByPrepare($sql = "", $params) { 
    global $db; 
    $final = array(); 
    $result = $db->query($sql, $params); 
    foreach ($result as $row) { 
    $final[] = self::instantiate($row); 
    } 
    return $final; 
} 

一旦你做出的改變,你的$comments = $photo->comments();行應返回的意見數組或一個空數組。這將使您的if ($comments):實際上按照您的預期工作,然後您的foreach ($comments as $comment):將遍歷實際數組而不是對象。

希望這會有所幫助。

+0

欣賞反饋!這是我曾經嘗試過的,但是那是當這個錯誤發生時,而不是「致命的錯誤:調用一個成員函數的評論()在非線對象在這是哪一行」$ comments = $ photo-> comments();「在photo.php上 – Nate

相關問題