ZF model example

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
<?php
class PhotoComments extends Zend_Db_Table {
protected $_name;
protected $_primary = 'cid';
public function __construct()
{
$this->_name = DB_PREFIX . 'photos_comments';
parent::__construct();
}
public function saveComment($values)
{
try {
if (isset($values['comment_id']) && (int)$values['comment_id']) {
$res = $this->update(array('content' => $values['vcontent']), 'cid = ' . (int)$values['comment_id']);
return $res;
}
$data = array();
$data['content'] = $values['vcontent'];
$data['external_id'] = $values['external_id'];
$data['parent_id'] = isset($values['parent_id']) ? (int)$values['parent_id'] : 0;
$data['owner'] = (int)$values['owner'];
$data['username'] = $values['name'];
$data['useremail'] = $values['email'];
$data['adddate'] = new Zend_Db_Expr('NOW()');
$insertID = $this->insert($data);
}catch (Exception $e) {
throw new Exception($e);
}
return $insertID;
}
public function EditComment($values, $commentID) {
try {
}catch (Exception $e) {
throw new Exception($e);
}
return $res;
}
public function getComments($inID)
{
try {
$select = $this->getDefaultAdapter()
->select()
->from(array('pc' => $this->_name))
->joinLeft(array('u' => DB_PREFIX . 'users'), 'u.uid = pc.owner', array('u.ulogin', 'u.email', 'u.avatar'))
->where("pc.external_id = $inID")
;
$comments = $this->getDefaultAdapter()->fetchAll($select);
if (!$comments) {
return array();
}
}catch (Exception $e) {
throw new Exception($e);
}
$data = array();
foreach ($comments as $row) {
$data[$row['parent_id']][$row['cid']] = $row;
}
// No comments
if (empty($data)) {
return array();
}
$result = array();
// Prepare array
Utils::PrepareThreeArray($data, $result);
return $result;
}
}