CodeIgniter Forums
DataMapper 1.6.0 - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Libraries & Helpers (
+--- Thread: DataMapper 1.6.0 (/showthread.php?tid=11358)

DataMapper 1.6.0 - El Forum - 02-25-2009

Well first off its in a fully managed object.

Im not exactly sure id wait until Overzealous or someone else can answer your question.

In my opinion it just makes everything easier as far as relations, management, validation etc...
To lets say check a username to see if its already taken You can do this from any controller throughout the CI site. (this is just a basic example but it shows what im talking about.

$u = new User();
$u = new User();
$u->username = $username;
if ($u->check_username())
// No username exists

Then in the User model
function check_username()
$u = new User();

// Check to see if the rest of the object was populated
if ($this->id)
// Looks like the username already exists
return FALSE;
// Looks like the username dosnt exist
return TRUE;

DataMapper 1.6.0 - El Forum - 02-25-2009

SOLVED!! The name of table was wrong. scopredmet_uporabniki is the right way, uporabniki_scopredmet wrong.


I have following problem, when i want't to save relations(has_many)
Fatal error: Call to a member function num_rows() on a non-object in C:\projekti\iam\streznik\htdocs\system\application\libraries\datamapper.php on line 2560
var dump of variable query
Object CI_DB_mysql_result extends CI_DB_result (
  ->conn_id = Resource [mysql link persistent] ( Resource id #33 )
  ->result_id = Resource [mysql result] ( Resource id #76 )
  ->result_array = Array 0 (
  ->result_object = Array 0 (
  ->current_row = Integer ( 0 )
  ->num_rows = Integer ( 0 )
  ->row_data = Null
  CI_DB_mysql_result methods: 19 (
the strange thing is that Kategorije is updated, but Uporabniki is not.
the code I'am using is
// Nov objekt
        $sco = new Scopredmet();
        // NOv objekt kategorij
        $kategorija = new Kategorije();
        $kategorija->where('id', $this->input->post('kategorija'))->get();
        // NOv objekt uporabnikov
        $uporabnikio = new Uporabniki();
        $uporabnikio->where_in('id', $this->input->post('uporabniki_id'))->get();
        $sco->save(array($kategorija, $uporabnikio));

Model Scopredmet
class Scopredmet extends DataMapper {
    var $table = "scopredmet";
    var $has_one = array("kategorije" => "kategorije");    
    var $has_many = array("uporabniki" => "uporabniki");
    function Scopredmet()

Model Kategorije
class Kategorije extends DataMapper {
    var $table = "kategorije";
    var $has_many = array("scopredmet");
    var $validation = array(
        'field' => 'ime',
        'label' => 'ime kategorije',
        'rules' => array('required', 'trim', 'unique', 'min_length' => 3, 'max_length' => 45)
    function Kategorije()

Model Uporabniki
class Uporabniki extends DataMapper {
    var $table = "uporabniki";
    var $has_many = array("scopredmet" => "scopredmet");

    function Uporabniki()

CI is 1.7.1 and DM from dated february 17.

DataMapper 1.6.0 - El Forum - 02-25-2009

The way ive been able to do it is save the object first then save the relations... Only way ive gotten it to work...

DataMapper 1.6.0 - El Forum - 02-25-2009

If you are saving multiple, new objects, you always have to save the objects first. Calling $obj1->save(array($obj2, $obj3)) will only save $obj1 to the database, and the relationships between $obj1 and $obj2, and $obj1 and $obj3.

It has to be this way, or else DM would spend a lot of time checking to see if something needs to be saved to the database. Also, you can save relationships to non-complete objects this way:


As for the previous discussion, about the multiple queries, it's how the field names are looked up. You'll only see the LIMIT 1 query once per model, as DataMapper has to determine which columns are available on the table. This is the standard ActiveRecord process.

If your application needs so much speed that calling one, near-instantaneous query to look up the column names is too much, then you probably shouldn't be using PHP, much less an ORM library.

DataMapper and other ORM libraries are designed to make developing code easier and faster. Most knowledgeable software developers will tell you that you should worry about application design and long-term management of code before you worry about fine-tuning for speed.

DataMapper 1.6.0 - El Forum - 02-26-2009

just wanted to say, this is astonishing! fantastic stuff, really; it's inspiring for a novice like me to see something like this!

cheers, and thank you so much Big Grin

DataMapper 1.6.0 - El Forum - 02-26-2009

thanks for u'r response.
i'm not worried about one query but i didn't understand why the query was run.
like i said in my first post is my first encounter with datamapper and with orm in general.
to be honest i only tested with 5-10 lines of code (busy with a project) that run queries one time per table so i didn't see it dose that only one time.
i was worried that it uses extra queries for every interrogation. i will test better next time

DataMapper 1.6.0 - El Forum - 02-26-2009

I completely understand! :cheese:
Feel free to ask any other questions you might have. You'll find, once you get used to it, that DataMapper is incredibly powerful for what it is, and allows you to produce results surprisingly fast. It does have limitations, and you'll probably need to work around some of them, but several of use here are still helping to fine-tune it.

DataMapper 1.6.0 - El Forum - 02-26-2009

So just so we can get a good example of how to save a uncomplete object with an object.

$d = New Dqip();
$d->version                     = set_value('version');
$d->title                       = set_value('title');
$d->description                 = set_value('description');
$d->demographics                = set_value('demographics');
$d->impact_to_organization      = set_value('impact_to_organization');
$d->automation_options          = set_value('automation_options');
$d->resolution_approach         = set_value('resolution_approach');
$d->re_extraction_schedule      = set_value('re_extraction_schedule');
$d->conversion_impact           = set_value('conversion_impact');
$d->presented_on                = set_value('presented_on');
$d->estimated_completion_date   = set_value('estimated_completion_date');
$d->estimated_count_manual      = set_value('estimated_count_manual');
$d->estimated_count_auto        = set_value('estimated_count_auto');
$d->estimated_count_not_valid   = set_value('estimated_count_not_valid');

$d = New Dqip();

$author = new User();

$dciu_staff_1 = new User();

$dciu_staff_2 = new User();

$data_source_1 = New Data_source();

$data_source_2 = New Data_source();

// Save the relations
if ($d->save( array(  'data_source_1'=>$data_source_1,
                      'dciu_staff_2'=>$dciu_staff_2) ))
    $data['success'] = 'Created the DQIP';
    $data['error'] = $d->error->string;

with your example above how would be the "other" way to do this. BTW above works.

DataMapper 1.6.0 - El Forum - 02-26-2009

edited: ignore this post...

DataMapper 1.6.0 - El Forum - 02-26-2009

In your example, just add ->select('id') in to your reference lookups. What I mean by "incomplete" objects is that you don't bother selecting the whole row. You will still be able to save the relationship, as long as the object has the correct ID.

I'm not sure why you would be seeing validation errors. What fields exactly? I don't think I changed the validation fields any. Are you sure that 'required' isn't getting set on these fields? Are you setting 'required' on the relationships?