行数が 0 の場合、これは簡単なはずです。メッセージをエコーしたい。ここに私が持っているものがあります:
public function getClubComment($id) {
$id = (int) $id;
$row = $this->fetchRow('club_id = ' . $id);
if (!$row) {
echo 'No comments';
}
return $row->toArray();
var_dump($row);
}
行数が 0 の場合、これは簡単なはずです。メッセージをエコーしたい。ここに私が持っているものがあります:
public function getClubComment($id) {
$id = (int) $id;
$row = $this->fetchRow('club_id = ' . $id);
if (!$row) {
echo 'No comments';
}
return $row->toArray();
var_dump($row);
}
fetchRow は、結果がなくてもオブジェクトを返します。そのため、if ステートメントの条件は常に false です。これを試してください。
if (!count($row))
たぶん次のようなものを試してください:
//not sure if this will work as I don't do this kind of request anymore
public function getClubComment($id) {
$id = (int) $id;
$row = $this->fetchRow('club_id = ?', $id);
if (!$row) {echo 'No comments';}
return $row->toArray();
var_dump($row);
}
このようなことをする方が幸せになると思います。ほとんどの推測作業が必要です。
public function getClubComment($id) {
$id = (int) $id;
//create instance of Zend_Db_Select object
$select = $this select();
$select->where('club_id = ?', $id);
//fetchRow using Zend_Db_Select object
$row = $this->fetchRow($select);
//fetchRow() returns NULL so may as well test for that.
if ($row === NULL) {
throw new Zend_Db_Table_Exception();
}
return $row->toArray();
var_dump($row);
}
Zend_Db_Selectは、通常、値を適切にクォートし、任意の SQL を使用してかなり複雑な SQL クエリを簡単に作成できるため、モデルで使用すると非常に便利です。この例では、select() の各部分に個別の行を使用しましたが、それらをすべてつなぎ合わせるのも簡単でした。個人的には、簡単に変更したりトラブルシューティングしたりできるように、各行を別々にするのが好きです。