{"id":1134,"date":"2014-04-22T01:57:59","date_gmt":"2014-04-21T16:57:59","guid":{"rendered":"https:\/\/ecoop.net\/memo\/?p=1134"},"modified":"2014-04-22T01:57:59","modified_gmt":"2014-04-21T16:57:59","slug":"php-yii-update-timestamp-automatically-of-model","status":"publish","type":"post","link":"https:\/\/www.ecoop.net\/memo\/archives\/php-yii-update-timestamp-automatically-of-model.html","title":{"rendered":"[PHP][Yii] \u30e2\u30c7\u30eb\u3067\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u3092\u81ea\u52d5\u66f4\u65b0\u3059\u308b"},"content":{"rendered":"<p>Yii 1.1 \u306e\u30e2\u30c7\u30eb(CModel)\u3067\u81ea\u52d5\u66f4\u65b0\u3059\u308b\u65b9\u6cd5\u306e\u30e1\u30e2\u3002<\/p>\n<p>\u53c2\u8003: <a href=\"http:\/\/www.yiiframework.com\/wiki\/10\/how-to-automate-timestamps-in-activerecord-models\/\">Yii 1.1: How to automate timestamps in ActiveRecord models<\/a><\/p>\n<h3>CActiveRecord::rules()\u3067\u8a2d\u5b9a\u3059\u308b<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n\/**\n * @return array validation rules for model attributes.\n *\/\npublic function rules()\n{\n    return array(\n        \/\/ \u7565\n        array(&#039;modified&#039;,&#039;default&#039;,\n              &#039;value&#039;=&gt;new CDbExpression(&#039;NOW()&#039;),  \/\/ PHP\u306e\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u3092\u4f7f\u3046\u306a\u3089date(&#039;Y-m-d H:i:s&#039;)\u306a\u3069\n              &#039;setOnEmpty&#039;=&gt;false,&#039;on&#039;=&gt;&#039;update&#039;),\n        array(&#039;created,modified&#039;,&#039;default&#039;,\n              &#039;value&#039;=&gt;new CDbExpression(&#039;NOW()&#039;),\n              &#039;setOnEmpty&#039;=&gt;false,&#039;on&#039;=&gt;&#039;insert&#039;)\n    );\n}\n<\/pre>\n<h3>CActiveRecord::beforeSave()\u3067\u66f4\u65b0\u3059\u308b<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\npublic function beforeSave() {\n    if ($this-&amp;gt;isNewRecord)\n        $this-&gt;created = new CDbExpression(&#039;NOW()&#039;);\n\n&lt;pre&gt;&lt;code&gt;$this-&amp;gt;modified = new CDbExpression(&#039;NOW()&#039;);\n\nreturn parent::beforeSave();\n&lt;\/code&gt;&lt;\/pre&gt;\n\n}\n<\/pre>\n<h3><a href=\"http:\/\/www.yiiframework.com\/doc\/api\/1.1\/CTimestampBehavior#setUpdateOnCreate-detail\">CTimestampBehavior<\/a>\u3092\u4f7f\u3046<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n    \/\/ CActiveRecord::behaviors()\n    public function behaviors(){\n        return array(\n            &#039;CTimestampBehavior&#039; =&amp;gt; array(\n                &#039;class&#039; =&amp;gt; &#039;zii.behaviors.CTimestampBehavior&#039;,\n                &#039;createAttribute&#039; =&gt; &#039;created&#039;, \/\/ \u4f5c\u6210\u65e5\u6642\u306e\u30ab\u30e9\u30e0\u540d(\u30c7\u30d5\u30a9\u30eb\u30c8: create_time)\n                &#039;updateAttribute&#039; =&gt; &#039;modified&#039;, \/\/ \u66f4\u65b0\u65e5\u6642\u306e\u30ab\u30e9\u30e0\u540d(\u30c7\u30d5\u30a9\u30eb\u30c8: update_time)\n                &#039;setUpdateOnCreate&#039; =&gt; true,    \/\/ \u4f5c\u6210\u6642\u306bupdate\u30ab\u30e9\u30e0\u3082\u66f4\u65b0\u3059\u308b\u304b(\u30c7\u30d5\u30a9\u30eb\u30c8: false)\n            )\n        );\n    }\n<\/pre>\n<p>\u3069\u308c\u3092\u4f7f\u3046\u304b\u306f\u304a\u597d\u307f\u3067\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yii 1.1 \u306e\u30e2\u30c7\u30eb(CModel)\u3067\u81ea\u52d5\u66f4\u65b0\u3059\u308b\u65b9\u6cd5\u306e\u30e1\u30e2\u3002 \u53c2\u8003: Yii 1.1: How to automate timestamps in ActiveRecord models CActiveRecord &hellip; <a href=\"https:\/\/www.ecoop.net\/memo\/archives\/php-yii-update-timestamp-automatically-of-model.html\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">[PHP][Yii] \u30e2\u30c7\u30eb\u3067\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u3092\u81ea\u52d5\u66f4\u65b0\u3059\u308b<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89],"tags":[32,131],"class_list":["post-1134","post","type-post","status-publish","format-standard","hentry","category-web-dev","tag-php","tag-yii"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/1134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/comments?post=1134"}],"version-history":[{"count":0,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/1134\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/media?parent=1134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/categories?post=1134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/tags?post=1134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}