SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Model Mapper issue with dynamic data.

    Hi.

    In the mapper I ended up with this code:

    Code PHP:
    public function save(Model_UserModel $user)
        {
            $data = array(
                'role_id'   => $user->getRole_id(),
                'uname'     => $user->getUname(),
                'surname'   => $user->getSurname(),
               // 'username'  => $user->getUserName(),
                'password'  => $user->getPassword(),
                'email'     => $user->getEmail(),
                'created'   => $user->getCreated(),
                'active'    => $user->getActive(),
            );
     
            if (null === ($id = $user->getId())) {
                unset($data['id']);
                $lastInsertId= $this->getDbTable()->insert($data);
                $data = array('username'  => 'tt_user_'.$lastInsertId);
                $this->getDbTable()->update($data, array('id = ?' => $lastInsertId));
     
            } else {
                $this->getDbTable()->update($data, array('id = ?' => $id));
            }
        }

    Is there a way to manage the username
    (string + lastId) model side ?

    For instance for the password in the model I managed
    this code:

    Code PHP:
    public function setPassword($password)
        {
            if(null === $password){
              $password = $this->getRandomPassword();  
            }
            $this->_password = (string) $password;
            return $this;
        }

    so in the controller I can make a think like
    this
    Code PHP:
    if ($request->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $extra = array('role_id'=>'1','username'=>'piphfpof','password'=>null ,'created'=>time(),'active'=>1);              $data = array_merge($form->getValues(), $extra);
                    $model = $this->_getModel($data); 
                    $model->save();
                    return $this->_helper->redirector('index');
                }
            }


    Can you help me, please ?

    Bye.

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I worked out separating the method save
    into two methods and than
    PHP Code:
    public function insert()
        {
            
    $lastInsertId $this->getMapper()->insert($this);
            
    $this->setId($lastInsertId);
            
    $this->setUsername($lastInsertId);
            
    $this->getMapper()->update($this);
        } 
    Not so difficult after all

  3. #3
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you need the username field for if it's based on the ID? That's just duplicating data. Maybe you could have a method on your object like this:

    PHP Code:
    method getUsername() {
       return 
    'tt_user_' $this->id;


  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the point.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •