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 - 02-25-2009

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

Code:
$u = new User();
$u->check_username($username)
or
Code:
$u = new User();
$u->username = $username;
if ($u->check_username())
{
// No username exists
}

Then in the User model
Code:
function check_username()
{
$u = new User();
$u->where('username',$this->username)->get();

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



DataMapper 1.6.0 - El Forum - 02-25-2009

[eluser]cupacup[/eluser]
SOLVED!! The name of table was wrong. scopredmet_uporabniki is the right way, uporabniki_scopredmet wrong.


Hello!

I have following problem, when i want't to save relations(has_many)
Code:
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
Code:
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 (
    ->num_rows();
    ->num_fields();
    ->list_fields();
    ->field_data();
    ->free_result();
    ->_data_seek();
    ->_fetch_assoc();
    ->_fetch_object();
    ->result();
    ->result_object();
    ->result_array();
    ->row();
    ->set_row();
    ->row_object();
    ->row_array();
    ->first_row();
    ->last_row();
    ->next_row();
    ->previous_row();
  )
)
the strange thing is that Kategorije is updated, but Uporabniki is not.
the code I'am using is
Code:
// Nov objekt
        $sco = new Scopredmet();
        $sco->naslov=$this->input->post('naslov');
        $sco->opis=$this->input->post('opid');
        $sco->barve=$this->input->post('tema');
        $sco->objavljeno=1;
        
        // 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
Code:
class Scopredmet extends DataMapper {
    var $table = "scopredmet";
    var $has_one = array("kategorije" => "kategorije");    
    var $has_many = array("uporabniki" => "uporabniki");
    
    function Scopredmet()
    {
        parent::DataMapper();
    }
}

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

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

    function Uporabniki()
    {
        parent::DataMapper();
    }
}

CI is 1.7.1 and DM from OverZealous.com dated february 17.


DataMapper 1.6.0 - El Forum - 02-25-2009

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

[eluser]OverZealous[/eluser]
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:
Code:
$obj2->select('id')->get();
$obj1->save($obj2);

--

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

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

[eluser]quasiperfect[/eluser]
thanks @OverZealous.com 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

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

[eluser]tdktank59[/eluser]
So just so we can get a good example of how to save a uncomplete object with an object.

Code:
$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->save();

$d = New Dqip();
$d->where('title',set_value('title'))->get();

$author = new User();
$author->where('id',set_value('author'))->get();

$dciu_staff_1 = new User();
$dciu_staff_1->where('id',set_value('dciu_staff_1'))->get();

$dciu_staff_2 = new User();
$dciu_staff_2->where('id',set_value('dciu_staff_2'))->get();

$data_source_1 = New Data_source();
$data_source_1->where('id',set_value('data_source_1'))->get();

$data_source_2 = New Data_source();
$data_source_2->where('id',set_value('data_source_2'))->get();

// Save the relations
if ($d->save( array(  'data_source_1'=>$data_source_1,
                      'data_source_2'=>$data_source_2,
                      'author'=>$author,
                      'dciu_staff_1'=>$dciu_staff_1,
                      'dciu_staff_2'=>$dciu_staff_2) ))
{
    $data['success'] = 'Created the DQIP';
}
else
{
    $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

[eluser]tdktank59[/eluser]
edited: ignore this post...


DataMapper 1.6.0 - El Forum - 02-26-2009

[eluser]OverZealous[/eluser]
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?