Rss Feed
  1. I have Profile table in this registering user details will save and User table where credentials will save..
    To make define Model in User as

    var $hasOne = array(
    'Profile' => array(
    'className' => ' Profile ',
    'foreignKey' => ' profile_id',
     'dependent'    => true
    )
    );

        and in Profile model add

     var $belongsTo = array(
     'User' => array(
    'className' => 'User',
    'foreignKey' => 'user_id',
    'conditions' => '',
    'fields' => '',
    'order' => ''
    )
    );


    In user_controller add

     function addmerchant() {
            {   
                if(!empty($this->data))
                 {  
                    $this->User->create();
    $this->data['User']['role_id'] = 4; // Registered
    $this->data['User']['activation_key'] = md5(uniqid());
    $this->data['User']['status'] = 0;
    $this->data['User']['username'] = htmlspecialchars($this->data['User']['username']);
    $this->data['User']['website'] = htmlspecialchars($this->data['User']['website']);
    $this->data['User']['name'] = htmlspecialchars($this->data['User']['name']); // assigning form details to User
    $user = $this->User->save($this->data) ;
                     if (!empty($user)) {
                          $this->data[' Profile ']['user_id'] = $this->User->id; // to get current save record id
                         $this->data[' Profile ']['postalcode'] = $this->data['User']['postalcode'] ; // assigning form details to Profile 
                         //$this->User->Merchantmaster->save($this->data);
                         if($this->User->Merchantmaster->save($this->data)){
                    $this->Session->setFlash(__('You have successfully registered an account. An email has been sent with further instructions.', true), 'default', array('class' => 'success'));
    $this->redirect(array('action' => 'login'));
                                
    } else {
    $this->Session->setFlash(__('The User could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
    }
                 
                }
                 }
                 }
                 }


    Link to follow!

        

  2. CAKE PHP!!! To get last insert id

    Wednesday, 19 September 2012

    To  retrive id from last inserted record use
     $this->ModelName->getInsertID()


  3. You can get all validation errors from the $this->validationErrors variable on the view. Then feel free to iterate through them and display as you like. They will be organized by model, like so:
    array(
      'User' => array(
        'username' => 'This field cannot be empty.',
        'password' => 'This field cannot be empty.'
      )
    );
    
    Then you can iterate through them on the view and display them as such. This example displays them as an unordered list:
    $errors = '';
    foreach ($this->validationErrors['User'] as $validationError) {
      $errors .= $this->Html->tag('li', $validationError);
    }
    echo $this->Html->tag('ul', $errors);
    
    Lastly, you can hide the form helper's automatic error messages by hiding them with CSS or setting the FormHelper defaults to not show them.
    CSS
    .input.error {
      display: none;
    }
    
    or
    in the view
    $this->Form->inputDefaults(array(
      'error' => false
    ));

  4. Here table used is merchantmaster, model name is Merchantmaster , controller name is Merchantmasters.

    In Model add

       function validateLogin($data)
        {
          //$uname = $this -> query("select username, password from merchantmasters where username = '$u';");
           //print_r($uname);

            $user = $this->find(array('username' => $data['username'], 'password' => $data['password']), array('id', 'username'));
            //print_r($user);
            if(empty($user) == false)
                return $user['Merchantmaster'];
            return false;
        }

    In controller add

    function beforeFilter() {
            parent::beforeFilter();
           $this->__validateLoginStatus();
     }
       
        function login()
        {
            if(empty($this->data) == false)
            {
                if(($user = $this->Merchantmaster->validateLogin($this->data['Merchantmaster'])) == true)
                {
                    $this->Session->write('merchantmaster', $user);
                    $this->Session->setFlash('You\'ve successfully logged in.');
                    $this->redirect('index');
                    exit();
                }
                else
                {
                    $this->Session->setFlash('Sorry, the information you\'ve entered is incorrect.');
                    $this->redirect('login');
                    exit();
                }
            }
        }
       
        function logout()
        {
            $this->Session->destroy('merchantmaster');
            $this->Session->setFlash('You\'ve successfully logged out.');
            $this->redirect('login');
        }


      
        function __validateLoginStatus()
      {
            if($this->action != 'login' && $this->action != 'logout')
            {
                if($this->Session->check('merchantmaster') == false)
                {
                    $this->redirect(array('action' =>'login'));
                    $this->Session->setFlash('The URL you\'ve followed requires you login.');
                }
            }
        }

    In view add

    <div class="row">
                <div class="span12">
                    <?php
                      echo $this->Layout->sessionFlash();
                    ?>
                </div>
            </div>
            <?php
        echo $form->create('Merchantmaster', array('action' => 'login'));
        echo $form->input('username');
       echo $form->input('password');
       echo $form->end('Login');
      
    ?>