ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

DMZ 1.7.1 (DataMapper OverZealous Edition)

March 14, 2010 11:43pm

Subscribe [104]
  • #736 / Nov 15, 2010 1:50pm

    theprodigy

    653 posts

    @theprodigy,

    Not entirely sure what you’re trying to archieve. Datamapper validation rules are meant to be processed before it saves an object. You can also set rules to perform transformations when you read an object.
    For me, a login process has to do with input validation, not with database validation.

    I told you about the login page, because it seems to work just fine there. My problem is on the profile page (sorry, forgot to mention the fact that I was talking about two different aspects). I’m allowing them to edit their information (username, password, email, etc). When I pull the password info, it is not running what I specified in the get_rules() array. It is not decrypting the password field. I am expecting it to when I am populating the password field on the Edit Profile view.

  • #737 / Nov 15, 2010 2:00pm

    WanWizard

    4475 posts

    Ok, I get it now.

    But I can’t reproduce it here. I’ve taken a model from one of my applications, and added ‘decrypt’ to a get_rule, and a dummy ‘decrypt’ function:

    /**
     * record validation rules
     */
    var $validation = array(
        'template_params' => array(
            'rules' => array('serialize_field'),
            'get_rules' => array('unserialize_field', 'decrypt')
        )
    );
    function _decrypt($field) { die('decrypt was called'); }

    When I call a page that uses this model, the _decrypt() function is called. Can you add some debug code in your function, to see if it’s called?

  • #738 / Nov 15, 2010 7:21pm

    tdktank59

    322 posts

    Well awesome, sorry I fell off the grid for a bit there.

    Glad to see someone took over, ill lend a hand when ever I can.
    Starting a new job here in a few days but in the mean time ive been working on getting my sites going.

    Will see what I can do after I settle down at the new job.

  • #739 / Nov 15, 2010 8:56pm

    theprodigy

    653 posts

    Can you add some debug code in your function, to see if it’s called?

    I added an echo statement to my decrypt function, and it is being called, but somehow the decrypt isn’t working correctly. It’s very strange.

    Here is my code, maybe someone can spot where I’ve gone wrong.

    Controller:

    class Profile extends MY_Controller {
    
        public function  __construct() {
            parent::__construct();
        }
    
        public function index()
        {
            //[removed due to length]
        }
    
        public function manage()
        {
            $user = new User($this->session->userdata('user_id'));
    
            if($this->input->post('submit'))
            {
                
                $user->fname = $this->input->post('first_name');
                $user->lname = $this->input->post('last_name');
                $user->email_address = $this->input->post('email');
                
                if($user->save())
                {
                    redirect('profile');
                }
            }
    
            $user->group->get();
            $lists = array();
            foreach($user->maillist->get() as $list)
            {
                $scope = ($list->private == 1)?"private":"public";
                $lists[] = $list->name . ' (' . $scope . ')';
            }
    
            if(count($lists) == 0)
            {
                $lists[] = '<em>none</em>';
            }
            $this->data['lists'] = $lists;
            $this->data['user'] = $user;
        }
    }

    Model:

    class User extends DataMapper {
    
        var $has_one = array('group');
    
        var $has_many = array('maillist','email','sent_email','signature');
    
        var $validation = array(
            //[other fields]
            'password' => array(
                'rules' => array('xss_clean','trim','encrypt','required', 'max_length' => 255),
                'label' => 'Password',
                'get_rules' => array('decrypt')
            ),
            //[other fields]
        );
        
        var $default_order_by = array('group_admin','fname', 'lname');
    
        function __construct($id = NULL)
        {
            parent::__construct($id);
    
            $this->pass_key = "[removed]";
        }
        
        public static function check_login($username, $password)
        {
            $return = false;
    
            $user = new User();
            $user->where('username',$username)->get();
    
            if($user->password == $password)
            {
                $return = $user->id;
            }
    
            return $return;
        }
        
        function _encrypt($field)
        {
            if (!empty($this->{$field}))
            {
                $CI = get_instance();
                $CI->load->library('encrypt');
                $this->{$field} = $CI->encrypt->encode($this->{$field}, $this->pass_key);
            }
        }
    
        function _decrypt($field)
        {
            if (!empty($this->{$field}))
            {
                $CI = get_instance();
                $CI->load->library('encrypt');
                $this->{$field} = $CI->encrypt->decode($this->{$field}, $this->pass_key);
            }
        }
    }

    View:

    <div>
        <h2>Manage Your Profile</h2>
    <p>    <?php<br />
        foreach ($user->error->all as $e)<br />
        {<br />
            echo '<div class="error">'.$e . '</div>';<br />
        }<br />
        ?><br />
        <?php echo form_open('profile/manage','',array('user_id'=>$user->id)); ?><br />
            <br />
                <label for="first_name">First Name</label><br />
                <input type="text" name="first_name" value="<?php echo $user->fname;?>" ><br />
            <br />
            <br />
                <label for="last_name">Last Name</label><br />
                <input type="text" name="last_name" value="<?php echo $user->lname;?>" ><br />
            <br />
            <br />
                <label for="password">Password</label><br />
                <?php $pass = $user->password; ?><br />
                <input type="password" name="password" value="<?php echo $pass; ?>" /><br />
            <br />
            <br />
                <label for="pass_conf">Password Confirmation</label><br />
                <input type="password" name="pass_conf" value="" /><br />
            <br />
            <br />
                <label for="email">Email</label><br />
                <input type="text" name="email" value="<?php echo $user->email_address; ?>" ><br />
            <br />
            <br />
                <label for="group">Group</label><br />
                <input readonly="readonly" type="text" name="group" value="<?php echo $user->group->name; ?>" ><br />
            <br />
            <br />
                <label for="group_admin">Group Admin</label><br />
                <input type="radio" name="group_admin" value="1"<?php echo (($user->group_admin == 1)?' checked="checked"':'disabled="disabled"') ?>> True<br />
                <input type="radio" name="group_admin" value="0"<?php echo (($user->group_admin == 1)?'':' checked="checked" disabled="disabled"') ?>> False<br />
            <br />
            <br />
                <input type="submit" name="submit" value="Submit" /><br />
                <input type="reset" name="reset" value="Reset" /><br />
                </p>
    
    <p><br />
                <input type="button" name="cancel"><br />
            <br />
        </form><br />
    </div>

    Also, I have attached a screenshot of my browser with firebug open, so you can see what I’m talking about.

  • #740 / Nov 16, 2010 3:19am

    WanWizard

    4475 posts

    Which version of CI? I’ve read somewhere that 2.0 has an issue with the encryption library.

    If you replace the encrypt/decrypt with something simple, like strtolower() and strtoupper(), does that work? If so, it’s not a Datamapper issue, but a CI issue. I don’t see anything obviously wrong with your code.

  • #741 / Nov 16, 2010 4:56am

    RagaDaga

    2 posts

    I have 2 classes - Students and Groups with many-to-many relationship. On a student page, I want to show all his details and list all groups he belongs to, delimited by comma. This is my Students controller:

    class Students extends Controller {
    
      function  __construct() {
          parent::__construct();
      }
    
      function index() {
          $this->get_all_students();
      }
    
      function get_all_students() {
          $s = new Student();
          $data['students'] = $s->select('id, name, email')->get();
    
          $this->load->view('students', $data);
      }
    
      function view($id) {
          $s = new Student();
          $s->get_by_id($id);
          $s->groups->get();
    
          $data['student'] = $s;
    
          $this->load->view('student_view', $data);
      }
    }

    I can get student’s details like this in student_view:

    Name: <?php echo $student->name; ?>
    E-mail: <?php echo $student->email; ?>
    Groups: 
    <?php foreach ($student->groups as $group) : ?>
      <?php echo anchor("/groups/$group->id", $group->name) ?>
    <?php endforeach; ?>

    So, how can I list groups delimited by comma? I tried adding group names to an array in the controller and then just

    <?php echo implode(', ', $groups); ?>

    in the view. But this way I cannot make a link using group IDs.

  • #742 / Nov 16, 2010 11:01am

    theprodigy

    653 posts

    Which version of CI? I’ve read somewhere that 2.0 has an issue with the encryption library.

    If you replace the encrypt/decrypt with something simple, like strtolower() and strtoupper(), does that work? If so, it’s not a Datamapper issue, but a CI issue. I don’t see anything obviously wrong with your code.

    It may be that. I am using CI 2.0. It’s just weird that it has no problem with the login, which is comparing what they type in, with the decrypted version of what is in the database, but when going to output it once logged in, it does this. I guess I could revert back to 1.7.2 for this project, or just keep going an eye out and see if there is any update to CI before rolling out live.

    I’ll run a few simple checks when I get home tonight (doing the strtolower and/or another simple function). I’ll let you know what I come up with.

  • #743 / Nov 16, 2010 4:42pm

    WanWizard

    4475 posts

    @RagaDaga,

    You want the links separated by a comma? Why not simply do

    <?php $first = TRUE;
    foreach ($student->groups as $group)
    {
        if ( ! $first ) echo ", ";
        echo anchor("/groups/$group->id", $group->name);
        $first = FALSE;
    endforeach; ?>
  • #744 / Nov 16, 2010 5:37pm

    RagaDaga

    2 posts

    oh, thanks!
    any ideas why there is one space added after group name and thus before the comma? I checked, there is no spaces in database.

  • #745 / Nov 16, 2010 5:54pm

    Chuck Liddell

    57 posts

    A new version of Datamapper ORM has just been committed to Bitbucket.
    Just click on “get source” to download the lastest version.

    This version introduces a new extension to work with nested sets (used to store tree structures in a database). Not all planned functionality is present yet, but it’s very usable. You can find the manual page for this extension here.

    You’ll find the complete changelog here.

    Note that this version has been tested with today’s version of CI 2.0. Although still no guarantees, we haven’t found any problem.

    This definitely piqued my interest. I have a custom nested set implementation at the moment that is part of a larger DB schema that I was going to roll over to DMZ. With a little luck I might be able to do the entire thing in DMZ now…

  • #746 / Nov 16, 2010 6:37pm

    WanWizard

    4475 posts

    oh, thanks!
    any ideas why there is one space added after group name and thus before the comma? I checked, there is no spaces in database.

    Probably because of a newline somewhere, all consecutive whitespace is reduced by the browser to a single space.

  • #747 / Nov 16, 2010 6:40pm

    WanWizard

    4475 posts

    This definitely piqued my interest. I have a custom nested set implementation at the moment that is part of a larger DB schema that I was going to roll over to DMZ. With a little luck I might be able to do the entire thing in DMZ now…

    Let me know what you think. The extension is still fairly untested, so if you find any issues, post them here.

    I’ll currently working on symlinks, so you can have a treenode point to another node (in another tree if needed). It’s a feature I use in ExiteCMS, which support multiple sites, and every site is a tree in the database. It allows you to link section of a website into another website.

  • #748 / Nov 18, 2010 10:29am

    mikelexp

    9 posts

    Is it possible to force an id when saving a new object? I’m building an app that caches Facebook objects and I’d like to build all the relationships using Facebook’s own IDs.

  • #749 / Nov 18, 2010 11:36am

    OverZealous

    1030 posts

    @mikelexp
    It’s in the manual 😉

  • #750 / Nov 18, 2010 12:06pm

    mikelexp

    9 posts

    @mikelexp
    It’s in the manual 😉

    Thank you! I’ve read the entire manual but this line led me to believe that it wasn’t possible:

    Every table must have a primary numeric key named id that is automatically generated.
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases