Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)

[eluser]jgdovin[/eluser]
EDIT : Ok Im stupid... after reading the datamapper.php a little closer, I saw the line about to_array and needing the extension array enabled. I never saw anything in the documentation, so I just assumed that it would work out of the box Smile Got it to work now, thanks guys. Cant wait to try this out now.

I assume this is still the support area for Datamapper ORM, so im gonna post this up. Im having some problems with the example application. I am trying to get a real feel for Datamapper ORM, and here are my problems.

When I went to run the example script I was getting call to unknown function apache_setenv()

After some looking around turns out that with me running CentOS with cpanel, and then running it with mod_suphp by default, that function isnt available. No problems, I commented it out, got a few errors about ob_flush() having nothing to flush during database install, but all looked to install ok...

Now when I went to create my admin user, this is the error I get...

Code:
Fatal error: Uncaught exception 'Exception' with message 'Unable to call the method
"from_array" on the class User' in /home/josh/public_html/clients/temp/application
/libraries/datamapper.php:1248 Stack trace: #0 [internal function]:
DataMapper->__call('from_array', Array) #1 /home/josh/public_html/clients/temp/application
/controllers/admin.php(164): User->from_array(Array, Array) #2 [internal function]:
Admin->init('save') #3 /home/josh/public_html/clients/temp/system/codeigniter/CodeIgniter.php(236):
call_user_func_array(Array, Array) #4 /home/josh/public_html/clients/temp/index.php(115):
require_once('/home/josh/publ...') #5 {main} thrown in
/home/josh/public_html/clients/temp/application/libraries/datamapper.php on
line 1248

Any help would be greatly appreciated. I really wanna learn Datamapper ORM, it looks to be pretty good, even if the people in #php complained about it bein messy Smile

[eluser]OverZealous[/eluser]
Regarding this forum:

WanWizard, if you get the chance to start a new forum topic, I'll make a link to it from the start of this topic.

I'll go ahead and update the links on the initial post.

[eluser]WanWizard[/eluser]
@OverZealous,

I was planning to do that when I release 1.8.0, which will happen as soon as I've had time to test the nested sets extension (within a week or so).

[eluser]WanWizard[/eluser]
@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.

[eluser]WanWizard[/eluser]
@jgdovin,

Which version of PHP are you using? Which version of CentOS?

This is the only place where apache_setenv is used
Code:
try {
    // force disabling of g-zip so output can be streamed
    apache_setenv('no-gzip', '1');
} catch(Exception $e) { /* ignore */ }
as you see it's setup to catch any errors.

My staging server runs CentOS 5.5 (with PHP 5.1.6), which has no problem with the datamapper code. Note that, as per the manual, Datamapper requires PHP 5.1.2+.

[eluser]theprodigy[/eluser]
[quote author="WanWizard" date="1289861366"]@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.[/quote]

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.

[eluser]WanWizard[/eluser]
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:
Code:
/**
* 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?

[eluser]tdktank59[/eluser]
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.

[eluser]theprodigy[/eluser]
[quote author="WanWizard" date="1289865619"]Can you add some debug code in your function, to see if it's called?[/quote]

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

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

[eluser]WanWizard[/eluser]
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.




Theme © iAndrew 2016 - Forum software by © MyBB