EllisLab text mark
Advanced Search
47 of 63
47
   
DataMapper ORM v1.8.2
Posted: 07 September 2012 02:33 AM   [ Ignore ]   [ # 691 ]   [ Rating: 0 ]
Joined: 2012-08-13
26 posts

I don’t think that would work because it’s only getting a user if their status is active .. otherwise the user object is blank I believe

 
Posted: 07 September 2012 02:36 AM   [ Ignore ]   [ # 692 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-12
420 posts

Wouldnt you need a User/Account object to see what the status of it is?

If not you can always do it if it is an empty object on the default value.

Lets say $user = ‘inactive’ by default, then you can use that? And for all others you need to get the account first?

 Signature 

- Simon

 
Posted: 07 September 2012 02:36 AM   [ Ignore ]   [ # 693 ]   [ Rating: 0 ]
Joined: 2012-08-13
26 posts

edit: Although the suggested solution did work I’ve reworked the code here is my new login logic should anyone find it useful at some point in time. Inactive = can’t log in, Suspended = can’t log in, expired = can log in sets flash data asking to renew

function login() {
        $username 
$this->username;
        
$u = new User();
        
$u->where('username'$username)->where('account_status !=''inactive')->get();
        
$this->salt $u->salt;
        
$this->validate()->get();
        if (
$this->exists()) {
            
if ($u->account_status == 'suspended'{
                $this
->error_message('login'$this->lang->line('error_invalid_login_suspended'));
                
$this->username $username;
                return 
FALSE;
            
else if ($u->account_status == 'expired'{
                $CI 
= & get_instance();
                
$CI->session->set_flashdata('login_message'$this->lang->line('error_account_expired'));
                return 
TRUE;
            
else {
                
return TRUE;
            
}
        } 
else {
            $this
->error_message('login'$this->lang->line('error_invalid_login'));
            
$this->username $username;
            return 
FALSE;
        
}
    } 
 
Posted: 07 September 2012 02:38 AM   [ Ignore ]   [ # 694 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-12
420 posts

Great smile

And on the flipside, it is quite quite early. smile

 Signature 

- Simon

 
Posted: 10 September 2012 05:26 AM   [ Ignore ]   [ # 695 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-12
420 posts

Alrighty self relationships 101. I go from this page on this: http://datamapper.wanwizard.eu/pages/advancedrelations.html

I have a very ‘simple’ self relationship. Never done those before in Datamapper and thus, issues.

It is simple. I have a class ACCategory and it should have a has_many with more categories who are children of it, kinda like directories. Then it should also know its parent.

class ACCategory extends DataMapper {
 
var $auto_populate_has_many TRUE;
 var 
$table 'ac_categories';

 var 
$has_many = array(
   
'parentcategory' => array(
     
'class' => 'accategory',
     
'other_field' => 'category',
     
'join_self_as' => 'category',
     
'join_other_as' => 'parentcategory',
     
'join_table' => 'ccj_ac_category_parentcategory'     
   
),   
   
'categories' => array(
     
'class' => 'accategory',
     
'other_field' => 'parentcategory',
     
'join_self_as' => 'parentcategory',
     
'join_other_as' => 'category',
     
'join_table' => 'ccj_ac_category_parentcategory'     
   
),
  ); 

That is where I am at the moment.

The database is like this:

TABLEccj_ac_category_parentcategory id|category_id|parentcategory_id 

I am kind of stuck with the tablenaming conventions so that is a given.

I get the usual ‘category is not a valid parent relationship for ACCategory’ error.

EDIT: Now that I think about it, the parent_category should probably be only one, you can only have one parent in this case.

 Signature 

- Simon

 
Posted: 10 September 2012 07:57 AM   [ Ignore ]   [ # 696 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

‘other_field’ has to point to the relation from the other side, so in your first definition that should be ‘categories’, not ‘category’.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 11 September 2012 09:53 PM   [ Ignore ]   [ # 697 ]   [ Rating: 0 ]
Avatar
Joined: 2009-03-19
103 posts

Hey man….

Is there a way to use a custom field as the ID field ?

I mean… every table that datamapper will use must have a id field, is there a way to use a field with a diferent name, like: custom_id ?

I need this because I am using a already existing database that I cannot modify its structure….

 
Posted: 12 September 2012 02:30 AM   [ Ignore ]   [ # 698 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

No, the use of ‘id’ as primary key is hardcoded in Datamapper. And in the current codebase it’s virtually impossible to change that without a large rewrite of the code.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 12 September 2012 05:39 PM   [ Ignore ]   [ # 699 ]   [ Rating: 0 ]
Joined: 2012-03-28
71 posts

How would i run an include_related() to include a related table but still return the row if there is no relating record in the related table?

 
Posted: 13 September 2012 02:47 AM   [ Ignore ]   [ # 700 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

You can’t. If you ask for included data, Datamapper runs an INNER JOIN, to make sure every parent has a child that can be included. There is currently no option to change that.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 14 September 2012 09:01 PM   [ Ignore ]   [ # 701 ]   [ Rating: 0 ]
Avatar
Joined: 2012-03-29
50 posts

library/auth

/**
  * login
  * 
  * @access public
  * @param  string  $username
  * @param  string  $password
  * @return bool
  */
 
public function login($username$password)
 
{
  
// Create a new temporary user object
  
$user = new User();  
  
  if (
$user->login($username$password))
  
{
   
// Prepare session data 
   
$userdata = array(
    
'user_id' =>  $user->id,
    
'username' => $user->username,
    
'activated' => $user->active === TRUE FALSE,
    
'logged_in' => TRUE
   
);
   
   
// Set sesssion data
   
$this->session->set_userdata($userdata);  
   
   
$this->messages->set('success''login_success'); 
   return 
TRUE;
  
}
  
else 
  
{
   $this
->messages->set('error''login_failed');
   return 
FALSE
  
}
 } 

model/user

/**
   * login
   * 
   * @param  string  $username
   * @param  string  $password
   * @return bool
   */ 
  
function login($username$password)
  
{
   $this
->where('username'$username);
  
$this->where('password'self::_encrypt($password));
  
$this->get();
  
  if (
$this->exists())
  
{
   $this
->ip_address $this->session->userdata('ip_address');
   
$this->last_login  time();
   
$this->save();
   
   return 
TRUE;
  
}
  } 

error

Fatal errorCall to a member function userdata() on a non-object in C:\wamp\www\codeigniter\application\models\User.php on line 68 

Line 68

$this->ip_address $this->session->userdata('ip_address'); 

What am I doing wrong here?

 
Posted: 15 September 2012 05:49 AM   [ Ignore ]   [ # 702 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

Assuming this is a Datamapper model, and not a CI model, you don’t have access to the CI superobject via $this.

In Datamapper models you have to use the “library way”:

$CI =& get_instance();
$CI->session->userdata('ip_address'); 
 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 16 September 2012 10:03 AM   [ Ignore ]   [ # 703 ]   [ Rating: 0 ]
Joined: 2012-09-16
6 posts

Hello to all,
I am just a new user of DATAMAPPER and I would like to ask you if is posibble do this code in datamapper:

$ordering $this->db->select("article_ordering")
    ->
from('pages')
    ->
where('id'$page_id)
    ->
get()->row()->article_ordering;

  
$query $this->db->select("a.title AS articles_title, a.content AS articles_content, a.excerpt AS articles_excerpt, a.slug AS articles_slug,a.views AS articles_views, a.views AS articles_views,CONCAT(up.first_name, ' ', up.last_name) AS articles_author, DATE_FORMAT(a.created, '%T %d.%m.%Y') AS articles_created, 
   (SELECT IFNULL(ROUND(AVG(av.vote),1), 0) FROM articles_votes av WHERE a.id = av.article_id) AS articles_votes,(SELECT COUNT(ac.id) FROM articles_comments ac 
    WHERE a.id = ac.article_id) AS articles_totalcomments"
FALSE)
    ->
from('articles a')
    ->
join('user_profiles up''a.author_id = up.user_id')
    ->
where('page_id'$page_id)
    ->
order_by("a.$ordering")
    ->
limit($num$offset)
    ->
get(); 

Thank you,
Regards

 
Posted: 16 September 2012 12:25 PM   [ Ignore ]   [ # 704 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

The first one can’t, because Datamapper doesn’t do rows. The get() returns an object, so you’ll have to use $ordering->article_ordering to access that column value.

The second one is no problem, providing you have defined the correct relations (to create the join).

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 16 September 2012 01:10 PM   [ Ignore ]   [ # 705 ]   [ Rating: 0 ]
Joined: 2012-09-04
3 posts

Hi there,

first of all: thanks for providing DataMapper, I really enjoy the functionality and your support.

Now I got a head-scratcher: I am currently writing an auth lib for myself and I am getting this error:

ErrorException [ Fatal Error ]Call to undefined method CI_DB_mysql_driver::dm_call_method()
FCPATH/application/libraries/datamapper.php [ 1113 ]
1108       register_shutdown_function
(array($CI->db'close'));
1109       $CI->db->_has_shutdown_hook TRUE;
1110      }
1111      
// clone, so we don't create additional connections to the DB
1112      $this->db = clone($CI->db);
1113      $this->db->dm_call_method('_reset_select');
1114     }
1115     
else
1116     {
1117      
// connecting to a different database, so we *must* create additional copies.
1118      // It is up to the developer to close the connection! 

I searched the web and the forums and the only thing I found was: Bootstrapper not included, but it is in my index.php:

/* --------------------------------------------------------------------
 * LOAD THE DATAMAPPER BOOTSTRAP FILE
 * --------------------------------------------------------------------
 */
require_once APPPATH.'third_party/datamapper/bootstrap.php';

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
require_once BASEPATH.'core/CodeIgniter.php'

I am not auto-loading any models.

I thought a moment if my php version does not support heredoc, but dismissed this though (running 5.3.10 on ubuntu) and still, I have no idea what the problem might be. Most of the time it plain works as it should, but right now, I am getting this error, always on the same controller function call. Disappointingly enough, I thought it was ion_auth interfering with datamapper, that’s why I wrote my own auth lib working very closely with Datamapper ORM, but now that I switch from ion_auth to my library I am getting the same errors (not sure if at the same spot, it has been some time).

Any pointers? The third_party/datamapper/system/DB_driver.php is definitely loaded, since it throws errors when I change something in there.

Thanks in advance,
puk

 

 
47 of 63
47