CodeIgniter Forums
DataMapper 1.6.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper 1.6.0 (/showthread.php?tid=11358)



DataMapper 1.6.0 - El Forum - 12-14-2008

[eluser]ryeguy[/eluser]
Eww having to select whole rows just to modify one value is really dirty and inefficient. I might just have to program in a hack. I'm thinking of adding a boolean to the Save() function that will only validate values that are present. Or maybe I could make it so it looks at what values where selected() and then only validates those.


DataMapper 1.6.0 - El Forum - 12-15-2008

[eluser]ryeguy[/eluser]
You may want to change this:
Code:
function _get_by($field, $value = array())
    {
        if (isset($value[0]))
        {
            $this->where($field, $value[0]);
        }

        return $this->get();
    }

To this:
Code:
function _get_by($field, $value = array())
    {
            $this->where($field, $value[0]);

        return $this->get();
    }

The reason being, is because if I do $user->get_by_username($string_thats_empty), instead of returning no results, it defaults to returning ALL users. I don't see why this functionality would ever be needed.


DataMapper 1.6.0 - El Forum - 12-16-2008

[eluser]sankai[/eluser]
hi,everybody.

How can I get a data row with array type not object type?

from

Code:
$o = new Object();
$o->get();
echo $o->title;

to

Code:
$o = new Object();
$row = $o->get();
echo $row['title'];

has any params can to use?

thank your reply


DataMapper 1.6.0 - El Forum - 12-16-2008

[eluser]stensi[/eluser]
I'll be adding in an as_array() method soon that transforms not just the current object record but its all list as well, into an array.

For now, you can transform just the current object record via the _to_array():

Code:
$o = new Object();
$o->get();
$row = $o->_to_array();

echo $row['title'];



DataMapper 1.6.0 - El Forum - 12-16-2008

[eluser]tdktank59[/eluser]
Ive got a question on how I would link these together

Im having an issue with the fact that i need a contact_id and a updated_by id, both go to the users table... How do i setup a relationship for this???

Heres the layout

Code:
CREATE TABLE IF NOT EXISTS `brainstorms` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `priority` int(11) default NULL COMMENT 'left as signed in case of negative priorities',
  `status` tinyint(1) unsigned NOT NULL default '1' COMMENT 'bit wise field see documentation',
  `short_desc` varchar(255) NOT NULL COMMENT 'AKA title',
  `long_desc` text,
  `origin_date` int(10) unsigned default NULL COMMENT 'unixtime where NULL = unknown',
  `est_count_total` int(10) unsigned default '0',
  `est_count_manual` int(10) unsigned default '0',
  `updated_on` int(10) unsigned NOT NULL COMMENT 'DataMapper automated timestamp in unixtime',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `uniq_idx_brainstorm_short_desc` (`short_desc`),
  KEY `idx_brainstorm_status` (`status`),
  KEY `idx_brainstorm_priority` (`priority`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;


CREATE TABLE IF NOT EXISTS `data_sources` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `short_desc` varchar(255) character set utf8 NOT NULL,
  `long_desc` text character set utf8,
  `status` tinyint(1) unsigned NOT NULL default '1',
  `updated_on` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `uniq_idx_data_source_short_desc` (`short_desc`),
  KEY `idx_data_source_status` (`status`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=5 ;


CREATE TABLE IF NOT EXISTS `issue_types` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `short_desc` varchar(255) NOT NULL,
  `long_desc` text,
  `status` tinyint(1) unsigned NOT NULL default '1' COMMENT 'bit wise field',
  `updated_on` int(10) unsigned NOT NULL COMMENT 'DataMapper automated timestamp in unixtime',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `uniq_idx_issue_type_short_desc` (`short_desc`),
  KEY `idx_issue_type_status` (`status`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;


CREATE TABLE IF NOT EXISTS `join_brainstorms_data_sources` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `brainstorm_id` int(10) unsigned NOT NULL,
  `data_source_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;


CREATE TABLE IF NOT EXISTS `join_brainstorms_issue_types` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `brainstorm_id` int(10) unsigned NOT NULL,
  `issue_type_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;


CREATE TABLE IF NOT EXISTS `join_brainstorms_user` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `brainstorm_updatedpid_id` int(10) unsigned NOT NULL,
  `brainstorm_contactpid_id` int(10) unsigned NOT NULL,
  `brainstorm_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;


CREATE TABLE IF NOT EXISTS `join_user_profiles_users` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `user_profile_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;


CREATE TABLE IF NOT EXISTS `users` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `salt` varchar(32) NOT NULL,
  `email` varchar(255) NOT NULL,
  `status` tinyint(1) unsigned NOT NULL default '1' COMMENT 'bit wise field see documentation',
  `updated_on` int(10) unsigned NOT NULL COMMENT 'DataMapper automated timestamp in unixtime',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `uniq_idx_username` (`username`),
  UNIQUE KEY `uniq_idx_email` (`email`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;


CREATE TABLE IF NOT EXISTS `user_profiles` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  `is_primary` tinyint(1) unsigned NOT NULL default '0' COMMENT 'Boolean that defaults to false',
  `active_start_date` int(10) unsigned NOT NULL COMMENT 'unixtime',
  `active_end_date` int(10) unsigned default NULL COMMENT 'unixtime',
  `status` tinyint(1) unsigned NOT NULL default '1' COMMENT 'bit wise field see documentation',
  `updated_on` int(10) unsigned NOT NULL COMMENT 'DataMapper automated timestamp in unixtime',
  PRIMARY KEY  (`id`),
  KEY `idx_user_profiles_title` (`title`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;



DataMapper 1.6.0 - El Forum - 12-16-2008

[eluser]OverZealous[/eluser]
The only way you can have multiple relationships with the same model / table is to either create virtual models (that use the same table, etc, but have a different model name), or to create an in-the-middle object that makes the extra connections.

To handle the virtual user:
Create a new model called, for example, Author:
Code:
class Author extends User {
   var $table  = 'users';
}

Add a new "author_id" column to your join_<whatever>_users. This is absolutely necessary. Also, make sure that your "author_id" and "user_id" columns allow NULL!

Modify any class that needs an author as well as a user by adding both to the $has_one or $has_many columns.

Now you can look up your Authors using $obj->author->get().

(I just wrote this off the top of my head, so you'll probably find typos.)

(Also, in the future, you might just want to describe your models, or maybe be a little more selective in what you post...)


DataMapper 1.6.0 - El Forum - 12-16-2008

[eluser]tdktank59[/eluser]
i figured i was too general lol...

But thanks thats i guess the problem im having so!!!


DataMapper 1.6.0 - El Forum - 12-16-2008

[eluser]empoleon[/eluser]
I'm getting an error I think is caused by the 'singular' function in inflector_helper.php

DataMapper is changing my model's singular name "Address" to "addres"

Code:
Error Number: 1054

Unknown column 'addresses_companies.addres_id' in 'on clause'

The singular function accepts $str="Address" as an argument and returns "addres", with the last "s" missing.

The 'address' Model:
Code:
class Address extends DataMapper {
    
    var $table = 'addresses';
    
    var $has_one = array('company');
    
    function __construct()
    {
        parent::DataMapper();
    }
    
}

The DataMapper user guide says (I think) that the inflector helper is not called if the $table var is set in the Model-- which I've done.

Quote:
Quote:If you don't supply the $table variable, DataMapper will automatically assume the table name is the same as your model name, in lowercase, with the letter s on the end (which will be the case most of the time).

Since $table was set in the Model I thought DataMapper presumes the column in the joining table is the same as the Model name (Model: Address, Joining table column: address_id)

Should I edit inflector_helper.php to handle singular words ending in "ess" or did I set this up wrong-?-hmm


DataMapper 1.6.0 - El Forum - 12-16-2008

[eluser]OverZealous[/eluser]
Simpler solution:
Add this to your model:
Code:
$model = 'address';

All set!


DataMapper 1.6.0 - El Forum - 12-16-2008

[eluser]empoleon[/eluser]
@overzealous-- Yep, setting $model in the Model did the trick. Now I don't need to brush up on inflection science. Thx!