Advertisement
sathyashrayan

Cakephp basic

Jul 21st, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.06 KB | None | 0 0
  1. [app/views/users/add.ctp]
  2.  
  3. <div class="users form">
  4. <?php echo $this->Form->create('User');?>
  5.     <fieldset>
  6.         <legend><?php __('Add User'); ?></legend>
  7.     <?php
  8.         echo $this->Form->input('username');
  9.         echo $this->Form->input('password');
  10.         echo $this->Form->input('group_id');
  11.     ?>
  12.     </fieldset>
  13. <?php echo $this->Form->end(__('Submit', true));?>
  14. </div>
  15. <div class="actions">
  16.     <h3><?php __('Actions'); ?></h3>
  17.     <ul>
  18.  
  19.         <li><?php echo $this->Html->link(__('List Users', true), array('action' => 'index'));?></li>
  20.         <li><?php echo $this->Html->link(__('List Groups', true), array('controller' => 'groups', 'action' => 'index')); ?> </li>
  21.         <li><?php echo $this->Html->link(__('New Group', true), array('controller' => 'groups', 'action' => 'add')); ?> </li>
  22.         <li><?php echo $this->Html->link(__('List Posts', true), array('controller' => 'posts', 'action' => 'index')); ?> </li>
  23.         <li><?php echo $this->Html->link(__('New Post', true), array('controller' => 'posts', 'action' => 'add')); ?> </li>
  24.     </ul>
  25. </div>
  26.  
  27. [app/controller/users_controller.php]
  28.  
  29. <?php
  30. class UsersController extends AppController {
  31.  
  32.     var $name = 'Users';
  33.  
  34.     function index() {
  35.         $this->User->recursive = 0;
  36.         $this->set('users', $this->paginate());
  37.     }
  38.  
  39.     function view($id = null) {
  40.         if (!$id) {
  41.             $this->Session->setFlash(__('Invalid user', true));
  42.             $this->redirect(array('action' => 'index'));
  43.         }
  44.         $this->set('user', $this->User->read(null, $id));
  45.     }
  46.  
  47.     function add() {
  48.         if (!empty($this->data)) {
  49.             $this->User->create();
  50.             if ($this->User->save($this->data)) {
  51.                 $this->Session->setFlash(__('The user has been saved', true));
  52.                 $this->redirect(array('action' => 'index'));
  53.             } else {
  54.                 $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
  55.             }
  56.         }
  57.         $groups = $this->User->Group->find('list');
  58.         $this->set(compact('groups'));
  59.     }
  60.  
  61.     function edit($id = null) {
  62.         if (!$id && empty($this->data)) {
  63.             $this->Session->setFlash(__('Invalid user', true));
  64.             $this->redirect(array('action' => 'index'));
  65.         }
  66.         if (!empty($this->data)) {
  67.             if ($this->User->save($this->data)) {
  68.                 $this->Session->setFlash(__('The user has been saved', true));
  69.                 $this->redirect(array('action' => 'index'));
  70.             } else {
  71.                 $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
  72.             }
  73.         }
  74.         if (empty($this->data)) {
  75.             $this->data = $this->User->read(null, $id);
  76.         }
  77.         $groups = $this->User->Group->find('list');
  78.         $this->set(compact('groups'));
  79.     }
  80.  
  81.     function delete($id = null) {
  82.         if (!$id) {
  83.             $this->Session->setFlash(__('Invalid id for user', true));
  84.             $this->redirect(array('action'=>'index'));
  85.         }
  86.         if ($this->User->delete($id)) {
  87.             $this->Session->setFlash(__('User deleted', true));
  88.             $this->redirect(array('action'=>'index'));
  89.         }
  90.         $this->Session->setFlash(__('User was not deleted', true));
  91.         $this->redirect(array('action' => 'index'));
  92.     }
  93.    
  94.     function login()
  95.     {
  96.          if ($this->Session->read('Auth.User'))
  97.          {
  98.              $this->Session->setFlash('You are logged in!');
  99.              $this->redirect('/', null, false);
  100.          }
  101.     }
  102.    
  103.     function logout()
  104.     {
  105.           $this->Session->setFlash('Good-Bye');
  106.           $this->redirect($this->Auth->logout());
  107.  
  108.     }
  109.    
  110.      function beforeFilter()
  111.      {
  112.         parent::beforeFilter();
  113.         $this->Auth->allow(array('*'));
  114.      }
  115. }
  116.  
  117. [app/models/user.php]
  118. <?php
  119. class User extends AppModel {
  120.     var $name = 'User';
  121.     var $validate = array(
  122.         'username' => array(
  123.             'notempty' => array(
  124.                 'rule' => array('notempty'),
  125.                 //'message' => 'Your custom message here',
  126.                 //'allowEmpty' => false,
  127.                 //'required' => false,
  128.                 //'last' => false, // Stop validation after this rule
  129.                 //'on' => 'create', // Limit validation to 'create' or 'update' operations
  130.             ),
  131.         ),
  132.         'password' => array(
  133.             'notempty' => array(
  134.                 'rule' => array('notempty'),
  135.                 //'message' => 'Your custom message here',
  136.                 //'allowEmpty' => false,
  137.                 //'required' => false,
  138.                 //'last' => false, // Stop validation after this rule
  139.                 //'on' => 'create', // Limit validation to 'create' or 'update' operations
  140.             ),
  141.         ),
  142.         'group_id' => array(
  143.             'numeric' => array(
  144.                 'rule' => array('numeric'),
  145.                 //'message' => 'Your custom message here',
  146.                 //'allowEmpty' => false,
  147.                 //'required' => false,
  148.                 //'last' => false, // Stop validation after this rule
  149.                 //'on' => 'create', // Limit validation to 'create' or 'update' operations
  150.             ),
  151.         ),
  152.     );
  153.     //The Associations below have been created with all possible keys, those that are not needed can be removed
  154.  
  155.     var $belongsTo = array(
  156.         'Group' => array(
  157.             'className' => 'Group',
  158.             'foreignKey' => 'group_id',
  159.             'conditions' => '',
  160.             'fields' => '',
  161.             'order' => ''
  162.         )
  163.     );
  164.  
  165.     var $hasMany = array(
  166.         'Post' => array(
  167.             'className' => 'Post',
  168.             'foreignKey' => 'user_id',
  169.             'dependent' => false,
  170.             'conditions' => '',
  171.             'fields' => '',
  172.             'order' => '',
  173.             'limit' => '',
  174.             'offset' => '',
  175.             'exclusive' => '',
  176.             'finderQuery' => '',
  177.             'counterQuery' => ''
  178.         )
  179.     );
  180.    
  181.    
  182.    
  183.     var $actsAs = array('Acl' => array('type' => 'requester'));
  184.    
  185.     function parentNode()
  186.     {
  187.         if (!$this->id && empty($this->data))
  188.         {
  189.             return null;
  190.         }
  191.         if (isset($this->data['User']['group_id']))
  192.         {
  193.             $groupId = $this->data['User']['group_id'];
  194.         }
  195.         else
  196.         {
  197.             $groupId = $this->field('group_id');
  198.         }
  199.         if (!$groupId)
  200.         {
  201.             return null;
  202.         }
  203.         else
  204.         {
  205.             return array('Group' => array('id' => $groupId));
  206.         }
  207.    }
  208.     function bindNode($user)
  209.     {
  210.         return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);
  211.     }
  212. }
  213.  
  214. [app/models/group.php]
  215.  
  216. <?php
  217. class Group extends AppModel {
  218.     var $name = 'Group';
  219.     var $actsAs = array('Acl' => array('type' => 'requester'));
  220.     var $validate = array(
  221.         'name' => array(
  222.             'notempty' => array(
  223.                 'rule' => array('notempty'),
  224.                 //'message' => 'Your custom message here',
  225.                 //'allowEmpty' => false,
  226.                 //'required' => false,
  227.                 //'last' => false, // Stop validation after this rule
  228.                 //'on' => 'create', // Limit validation to 'create' or 'update' operations
  229.             ),
  230.         ),
  231.     );
  232.     //The Associations below have been created with all possible keys, those that are not needed can be removed
  233.  
  234.     var $hasMany = array(
  235.         'User' => array(
  236.             'className' => 'User',
  237.             'foreignKey' => 'group_id',
  238.             'dependent' => false,
  239.             'conditions' => '',
  240.             'fields' => '',
  241.             'order' => '',
  242.             'limit' => '',
  243.             'offset' => '',
  244.             'exclusive' => '',
  245.             'finderQuery' => '',
  246.             'counterQuery' => ''
  247.         )
  248.     );
  249.     function parentNode()
  250.     {
  251.      return null;
  252.     }
  253.  
  254. }
  255. [sql schema]
  256.  
  257. -- phpMyAdmin SQL Dump
  258. -- version 3.2.4
  259. -- http://www.phpmyadmin.net
  260. --
  261. -- Host: localhost
  262. -- Generation Time: Jul 21, 2011 at 11:36 AM
  263. -- Server version: 5.1.41
  264. -- PHP Version: 5.3.1
  265.  
  266. SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
  267.  
  268. --
  269. -- Database: `cake_v1`
  270. --
  271.  
  272. -- --------------------------------------------------------
  273.  
  274. --
  275. -- Table structure for table `acos`
  276. --
  277.  
  278. CREATE TABLE IF NOT EXISTS `acos` (
  279.   `id` int(10) NOT NULL AUTO_INCREMENT,
  280.   `parent_id` int(10) DEFAULT NULL,
  281.   `model` varchar(255) DEFAULT NULL,
  282.   `foreign_key` int(10) DEFAULT NULL,
  283.   `alias` varchar(255) DEFAULT NULL,
  284.   `lft` int(10) DEFAULT NULL,
  285.   `rght` int(10) DEFAULT NULL,
  286.   PRIMARY KEY (`id`)
  287. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
  288.  
  289. --
  290. -- Dumping data for table `acos`
  291. --
  292.  
  293. INSERT INTO `acos` (`id`, `parent_id`, `model`, `foreign_key`, `alias`, `lft`, `rght`) VALUES
  294. (1, NULL, NULL, NULL, 'controllers', 1, 2);
  295.  
  296. -- --------------------------------------------------------
  297.  
  298. --
  299. -- Table structure for table `aros`
  300. --
  301.  
  302. CREATE TABLE IF NOT EXISTS `aros` (
  303.   `id` int(10) NOT NULL AUTO_INCREMENT,
  304.   `parent_id` int(10) DEFAULT NULL,
  305.   `model` varchar(255) DEFAULT NULL,
  306.   `foreign_key` int(10) DEFAULT NULL,
  307.   `alias` varchar(255) DEFAULT NULL,
  308.   `lft` int(10) DEFAULT NULL,
  309.   `rght` int(10) DEFAULT NULL,
  310.   PRIMARY KEY (`id`)
  311. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
  312.  
  313. --
  314. -- Dumping data for table `aros`
  315. --
  316.  
  317. INSERT INTO `aros` (`id`, `parent_id`, `model`, `foreign_key`, `alias`, `lft`, `rght`) VALUES
  318. (1, NULL, 'Group', 2, NULL, 1, 10),
  319. (2, NULL, 'Group', 3, NULL, 11, 14),
  320. (3, NULL, 'Group', 4, NULL, 15, 18),
  321. (4, 1, 'User', 1, NULL, 2, 3),
  322. (5, 2, 'User', 2, NULL, 12, 13),
  323. (6, 1, 'User', 3, NULL, 4, 5),
  324. (7, 1, 'User', 4, NULL, 6, 7),
  325. (8, 1, 'User', 5, NULL, 8, 9),
  326. (9, 3, 'User', 6, NULL, 16, 17);
  327.  
  328. -- --------------------------------------------------------
  329.  
  330. --
  331. -- Table structure for table `aros_acos`
  332. --
  333.  
  334. CREATE TABLE IF NOT EXISTS `aros_acos` (
  335.   `id` int(10) NOT NULL AUTO_INCREMENT,
  336.   `aro_id` int(10) NOT NULL,
  337.   `aco_id` int(10) NOT NULL,
  338.   `_create` varchar(2) NOT NULL DEFAULT '0',
  339.   `_read` varchar(2) NOT NULL DEFAULT '0',
  340.   `_update` varchar(2) NOT NULL DEFAULT '0',
  341.   `_delete` varchar(2) NOT NULL DEFAULT '0',
  342.   PRIMARY KEY (`id`),
  343.   UNIQUE KEY `ARO_ACO_KEY` (`aro_id`,`aco_id`)
  344. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
  345.  
  346. --
  347. -- Dumping data for table `aros_acos`
  348. --
  349.  
  350. INSERT INTO `aros_acos` (`id`, `aro_id`, `aco_id`, `_create`, `_read`, `_update`, `_delete`) VALUES
  351. (1, 1, 1, '-1', '-1', '-1', '-1'),
  352. (2, 2, 1, '-1', '-1', '-1', '-1');
  353.  
  354. -- --------------------------------------------------------
  355.  
  356. --
  357. -- Table structure for table `groups`
  358. --
  359.  
  360. CREATE TABLE IF NOT EXISTS `groups` (
  361.   `id` int(11) NOT NULL AUTO_INCREMENT,
  362.   `name` varchar(100) NOT NULL,
  363.   `created` datetime DEFAULT NULL,
  364.   `modified` datetime DEFAULT NULL,
  365.   PRIMARY KEY (`id`)
  366. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
  367.  
  368. --
  369. -- Dumping data for table `groups`
  370. --
  371.  
  372. INSERT INTO `groups` (`id`, `name`, `created`, `modified`) VALUES
  373. (2, 'administrators', '2011-06-15 19:44:50', '2011-06-15 19:45:06'),
  374. (3, 'managers', '2011-06-15 19:45:16', '2011-06-15 19:45:16'),
  375. (4, 'users', '2011-06-15 19:45:25', '2011-06-15 19:45:25');
  376.  
  377. -- --------------------------------------------------------
  378.  
  379. --
  380. -- Table structure for table `posts`
  381. --
  382.  
  383. CREATE TABLE IF NOT EXISTS `posts` (
  384.   `id` int(11) NOT NULL AUTO_INCREMENT,
  385.   `user_id` int(11) NOT NULL,
  386.   `title` varchar(255) NOT NULL,
  387.   `body` text,
  388.   `created` datetime DEFAULT NULL,
  389.   `modified` datetime DEFAULT NULL,
  390.   PRIMARY KEY (`id`)
  391. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  392.  
  393. --
  394. -- Dumping data for table `posts`
  395. --
  396.  
  397.  
  398. -- --------------------------------------------------------
  399.  
  400. --
  401. -- Table structure for table `users`
  402. --
  403.  
  404. CREATE TABLE IF NOT EXISTS `users` (
  405.   `id` int(11) NOT NULL AUTO_INCREMENT,
  406.   `username` varchar(255) NOT NULL,
  407.   `password` char(40) NOT NULL,
  408.   `group_id` int(11) NOT NULL,
  409.   `created` datetime DEFAULT NULL,
  410.   `modified` datetime DEFAULT NULL,
  411.   PRIMARY KEY (`id`),
  412.   UNIQUE KEY `username` (`username`)
  413. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
  414.  
  415. --
  416. -- Dumping data for table `users`
  417. --
  418.  
  419. INSERT INTO `users` (`id`, `username`, `password`, `group_id`, `created`, `modified`) VALUES
  420. (1, 'sathya', 'a0dafbf8db589b2f90c09798b24e3b2732325ebc', 2, '2011-06-17 18:17:12', '2011-06-17 18:17:12'),
  421. (2, 'sathya_two', 'a0dafbf8db589b2f90c09798b24e3b2732325ebc', 3, '2011-06-17 18:18:01', '2011-06-17 18:18:01'),
  422. (3, 'raju', 'a0dafbf8db589b2f90c09798b24e3b2732325ebc', 2, '2011-06-18 12:20:46', '2011-06-18 12:20:46'),
  423. (4, 'raju_2', '8b73d35d607c405f8f74f715a031beeb57f5e4a4', 2, '2011-06-18 12:21:27', '2011-06-18 12:21:27'),
  424. (5, 'sathyashrayan', '579383613f4ac6328a621080fc61a69b32ee618b', 2, '2011-06-23 12:35:22', '2011-06-23 12:35:22'),
  425. (6, 'sathyashrayan.n@ameexusa.com', 'e1e9d096378712b99d1ef7abe33405260fabda63', 4, '2011-07-21 11:22:58', '2011-07-21 11:23:55');
  426.  
  427. -- --------------------------------------------------------
  428.  
  429. --
  430. -- Table structure for table `widgets`
  431. --
  432.  
  433. CREATE TABLE IF NOT EXISTS `widgets` (
  434.   `id` int(11) NOT NULL AUTO_INCREMENT,
  435.   `name` varchar(100) NOT NULL,
  436.   `part_no` varchar(12) DEFAULT NULL,
  437.   `quantity` int(11) DEFAULT NULL,
  438.   PRIMARY KEY (`id`)
  439. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  440.  
  441. --
  442. -- Dumping data for table `widgets`
  443. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement