0

私はオブジェクト指向のゲストブックを開発し、 Class を持っていますGuestBookEntry:

class GuestBookEntry {
protected $id;
protected $entryDate;
protected $authorName;
protected $authorMail;  
protected $entryText; 

// getters, setters and specific functions are omitted
}

mysql列と同じ名前のテーブルもあります。

さて、ゲストブックのエントリを取得すると、動作しているように見えますが、ゲッターによって返されたIDとのみが表示されます。Date他のものはテンプレートに戻りません。

コードは次のようになります。

public function showEntries() { 
    $query = mysql_query('SELECT * FROM dng_entries ORDER BY id DESC');     
    while($entry = @mysql_fetch_object($query, 'GuestBookEntry')) {
        if(empty($entry)) {
        echo '<font color="white">Keine Eintr&auml;ge vorhanden...</font>';
        } else {    
        var_dump($entry);
        echo '<table class="table table-bordered entryfield" align="right">
                <thead>
                    <tr>
                        <td rowspan="2">'.$entry->getId().'</td>
                        <td width="20%">Datum:</td>
                        <td>Name:</td>
                        <td>Email:</td>
                    </tr>
                    <tr>
                        <td>'.$entry->getEntryDate().'</td>
                        <td>'.$entry->getAuthorName().'</td>
                        <td><a href="mailto:'.$entry->getAuthorMail().'">'.$entry->getAuthorMail().'</a></td>
                </thead>
                <tbody>
                    <tr>
                    <td width="10%" valign="middle">Eintrag:</td>
                    <th colspan="3" valign="top" height="100px">'.$entry->getEntryText().'</td>
                    </tr>
                </tbody>
            </table>';
        }
    }
}

これvar_dumpは例えばオブジェクトです:

object(GuestBookEntry)#2 (5) { ["id":protected]=> string(1) "2" ["entryDate":protected]=> int(1344696811) ["authorName":protected]=> NULL ["authorMail":protected]=> NULL ["entryText":protected]=> NULL }

更新: GuestBookEntry クラスの残りの部分は次のとおりです。

public function __construct($authorName, $authorMail, $entryText) {
       $this->authorName    = $authorName;
       $this->authorMail    = $authorMail;
       $this->entryDate     = time();
       $this->entryText     = $entryText;
    }

    public function getId() {
       return $this->id;
    }
    public function getAuthorName() {
       return (String) $this->authorName;
    }
    public function getAuthorMail() {
       return (String) $this->authorMail;
    }
    public function getEntryDate() {
       return date('d.n.Y', $this->entryDate);
    }
    public function getEntryText() {
       return $this->entryText;
    }

    public function setAuthorName($authorName) {
       $this->authorName=$authorName;
    }   
    public function setAuthorMail($authorMail) {
       $this->authorMail=$authorMail;
    }
    public function setEntryDate($entryDate) {
       $this->entryDate=$entryDate;
    }
    public function setEntryText($entryText) {
       $this->entryText=$entryText;
    }
4

1 に答える 1

3

問題は大文字と小文字の区別です。MySQL の列名はキャメル ケースを処理しませんが、PHP は $entryDate と $entrydate などの大文字と小文字に基づいてプロパティの違いを認識します。視覚的な区切りが必要な場合は、アンダースコア付きの小文字に固執してください。機能する理由idは、すべて小文字だからです。

テーブル列にマップされるはずのすべてのプロパティ名を単純に小文字にすると、すべてが機能すると思います。

ところで...列名のキャメルケースの問題は、SQLサーバーを実行しているOSによって異なる場合があります。

于 2012-08-11T15:07:08.310 に答える