开发者

Is there any automagic issue in zend framework like created and modified date in mysql table?

In cakephp there can be two columns for mysql tables one is "created" and other is "modified".

So when a post is created or updated the two columns are automatically updated.

Is there any issue like that for zend framework ?

Any a开发者_StackOverflowutomagic issue ?


Zend doesn't do any magic. Though you can ask MySQL to do it ;) with the help of TIMESTAMP and TRIGGERS .

Here is a few code that may help .

SET NAMES utf8;
SET foreign_key_checks = 0;
SET time_zone = 'SYSTEM';
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';

DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

INSERT INTO `test` (`id`, `name`, `updated`, `created`) VALUES
(1, 'test', '2011-08-28 13:47:21',  '0000-00-00 00:00:00'),
(2, 'hello',    '0000-00-00 00:00:00',  '0000-00-00 00:00:00'),
(3, 'hari', '2011-08-28 13:48:48',  '0000-00-00 00:00:00'),
(4, 'test', '2011-08-28 13:50:12',  '2011-08-23 08:19:47');

DELIMITER ;;

CREATE TRIGGER `on-insert` BEFORE INSERT ON `test` FOR EACH ROW
SET NEW.created = UTC_TIMESTAMP;;

DELIMITER ;

Or you can also do without trigger like either on inserting or updating pass the NOW() with the data and make either update or created timestamp DEFAULT or UPDATE . As you cannot do two things ( only for update or insert ). The code above is an inspiration from some of the comments in MySQL

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜