|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* class for commenting. Model for comments. |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Anax\CommentDb; |
|
6
|
|
|
|
|
7
|
|
|
class CommentsInDb extends \Anax\MVC\CDatabaseModel |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Initialize model. |
|
12
|
|
|
* |
|
13
|
|
|
* @return array |
|
14
|
|
|
*/ |
|
15
|
|
|
public function init() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->db->setVerbose(); |
|
|
|
|
|
|
18
|
|
|
$this->db->dropTableIfExists('commentsindb')->execute(); |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
$this->db->createTable( |
|
|
|
|
|
|
21
|
|
|
'commentsindb', |
|
22
|
|
|
[ |
|
23
|
|
|
'id' => ['integer', 'primary key', 'not null', 'auto_increment'], |
|
24
|
|
|
'content' => ['varchar(1024)'], |
|
25
|
|
|
'q_or_a' => ['char(1)'], |
|
26
|
|
|
'q_or_a_id' => ['int'], |
|
27
|
|
|
'user_id' => ['int'], |
|
28
|
|
|
'created' => ['datetime'], |
|
29
|
|
|
] |
|
30
|
|
|
)->execute(); |
|
31
|
|
|
$this->db->insert( |
|
|
|
|
|
|
32
|
|
|
'commentsindb', |
|
33
|
|
|
['content', 'q_or_a', 'q_or_a_id', 'user_id', 'created'] |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
$now = time(); |
|
37
|
|
|
|
|
38
|
|
|
$this->db->execute([ |
|
|
|
|
|
|
39
|
|
|
'Lorem ipsum dolor sit amet, ad nam graeci **dissentias**, te verear utroque per. Doming intellegat mea id, mel ei dicta iudico. Dicunt fabulas usu ad. Per nemore possim commune ut, eu probo dicta has. ', |
|
40
|
|
|
'q', |
|
41
|
|
|
1, |
|
42
|
|
|
1, |
|
43
|
|
|
$now |
|
44
|
|
|
]); |
|
45
|
|
|
|
|
46
|
|
|
$this->db->execute([ |
|
|
|
|
|
|
47
|
|
|
'Accusam eleifend qui ex. Has duis iuvaret salutatus id, dico illud porro ea mei, id oblique tibique eos. Ne eam meis equidem admodum, eos nisl maluisset id. Ancillae lucilius persecuti no sed.', |
|
48
|
|
|
'a', |
|
49
|
|
|
1, |
|
50
|
|
|
1, |
|
51
|
|
|
$now |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
$this->db->execute([ |
|
|
|
|
|
|
55
|
|
|
'Sea te vocibus dolores pertinax, quodsi insolens appellantur sit an, dicam definitionem sed ne.', |
|
56
|
|
|
'a', |
|
57
|
|
|
1, |
|
58
|
|
|
1, |
|
59
|
|
|
$now |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
/** |
|
64
|
|
|
* Count number of questions each user has asked. |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
// TODO: Move function CDatabaseModel and set count column as parameter. |
|
69
|
|
|
// move from all others as well. |
|
70
|
|
|
public function countByUser() |
|
71
|
|
|
{ |
|
72
|
|
|
// SELECT user_id, COUNT(*) AS Cnt FROM cquestions GROUP BY user_id ORDER BY user_id ASC; |
|
73
|
|
|
$this->db->select("user_id, COUNT(*) AS Cnt") |
|
|
|
|
|
|
74
|
|
|
->from($this->getSource()) |
|
75
|
|
|
->groupby("user_id") |
|
76
|
|
|
->orderby('user_id ASC') |
|
77
|
|
|
->execute(); |
|
78
|
|
|
$this->db->execute(); |
|
|
|
|
|
|
79
|
|
|
$this->db->setFetchModeClass(__CLASS__); |
|
|
|
|
|
|
80
|
|
|
return $this->db->fetchAll(); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public static function humanTiming($time) |
|
84
|
|
|
{ |
|
85
|
|
|
|
|
86
|
|
|
$time = time() - $time; // to get the time since that moment |
|
87
|
|
|
$time = ($time<1)? 1 : $time; |
|
88
|
|
|
$tokens = array ( |
|
89
|
|
|
31536000 => 'år', |
|
90
|
|
|
2592000 => 'månader', |
|
91
|
|
|
604800 => 'veckor', |
|
92
|
|
|
86400 => 'dagar', |
|
93
|
|
|
3600 => 'timmar', |
|
94
|
|
|
60 => 'minuter', |
|
95
|
|
|
1 => 'sekunder' |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
foreach ($tokens as $unit => $text) { |
|
99
|
|
|
if ($time < $unit) { |
|
100
|
|
|
continue; |
|
101
|
|
|
} |
|
102
|
|
|
$numberOfUnits = floor($time / $unit); |
|
103
|
|
|
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?' ':''); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.