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 - 01-22-2009

[eluser]ntheorist[/eluser]
[quote author="wolffc" date="1232685529"]Is it possible to give an order column to the joining table? Lets say a user can pick 3 of 10 options and set a specific order. Would they be inserted and retrived in the same order?


Thanks[/quote]

I've been toying with this idea of building in methods that access additional fields in the relationship tables..

essentially though, you can add whatever fields you like, in your linking table as long as you have the id, model1_id, model2_id fields..

so say you want to get a users groups, which would have a sort order, you could add in an order_number column on the groups_users table, and then (with a join specified), you can try
Code:
$user = new User();
$user->where('id',$userID)->get();
$user->group->order_by('groups_users.order_number', 'asc');
$user->group->get();

does that make sense?.. you just have to literally specify the relationship table name for now.

CC


DataMapper 1.6.0 - El Forum - 01-23-2009

[eluser]robertcsmith[/eluser]
@tdktank59
typically when saving new or updated Brainstorm objects and relations you should load the temp object values via $this->input->post('{fieldname}'). The forms fieldnames dont even need to be the extact same name as the db field since you pass its value into the object property and then upon a save attempt, validation runs off that, and either returns errors or has a successful save.

The set_value method is defined as "
set_value()

Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />

The above form will show "0" when loaded for the first time."

I dont think that method would return anything until after validation at least.

I was working on and is posted in our projects repository a couple of validation prep and preprocess validation rules including a method to track which user last updated the record.


DataMapper 1.6.0 - El Forum - 01-23-2009

[eluser]The Hamburgler[/eluser]
Hello again.
I've been busy developing an extension to the Datamapper Model that will provide methods to quickly draw forms/tables of your Datamapper models. It's going well, Datamapper has proved to be very effective.

However a number of times I have met an issue removing relationships from a model.

Example:
I have two related models.
Code:
class School extends DataMapper {

    var $has_many = array("student");

    function School()
    {
        parent::DataMapper();
    }
}
Code:
class Student extends DataMapper {

    var $has_one = array("school");

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

I wish to remove a subset of pupils from a school and then display the remaining school pupils
Code:
// ini models
$sch = new School();
$stu = new Student();

// load school
$sch->get_by_id(1);

// load subset of students
$stu->where_in('id', array(1, 3, 4, 7));
$stu->get();

// remove subset of students from school
$sch->delete($stu->all);

// refresh remaining students at school
$sch->school->get(); // throws 'Call to a member function get() on a non-object' Error

There are ways around this issue, you can re-initialise the student object within the school, but its not very pretty. Just wondering if anybody else has has this issue or can see a cleaner solution?

Cheers.


DataMapper 1.6.0 - El Forum - 01-23-2009

[eluser]robertcsmith[/eluser]
um I think you meant to call $sch->student->get() in this:

Code:
// refresh remaining students at school
$sch->school->get(); // throws 'Call to a member function get() on a non-object' Error

but if you want to use $stu as an object again you'll need to first clear it and then instantiate it as a new object with $stu = new Student; $stu->get();


DataMapper 1.6.0 - El Forum - 01-23-2009

[eluser]The Hamburgler[/eluser]
[quote author="robertcsmith" date="1232729564"]um I think you meant to call $sch->student->get() in this:

Code:
// refresh remaining students at school
$sch->school->get(); // throws 'Call to a member function get() on a non-object' Error
[/quote]
Oops, yes thats right.
My current solution is too re instantiate the student object within the school model. i.e.
Code:
// remove subset of students from school
$sch->delete($stu->all);

// instantiate schools students model
$sch->student = new Student();

// refresh remaining students at school
$sch->student->get();

I was just wondering why datamapper removes the related object from model instead of just removing the link record and refreshing the related object?


DataMapper 1.6.0 - El Forum - 01-23-2009

[eluser]tdktank59[/eluser]
[quote author="robertcsmith" date="1232727069"]@tdktank59
typically when saving new or updated Brainstorm objects and relations you should load the temp object values via $this->input->post('{fieldname}'). The forms fieldnames dont even need to be the extact same name as the db field since you pass its value into the object property and then upon a save attempt, validation runs off that, and either returns errors or has a successful save.

The set_value method is defined as "
set_value()

Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form. Example:
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />

The above form will show "0" when loaded for the first time."

I dont think that method would return anything until after validation at least.

I was working on and is posted in our projects repository a couple of validation prep and preprocess validation rules including a method to track which user last updated the record.[/quote]

This doesn't answer my question Robert, And yes set_value() does work after the validation has run (Good thing it has by the time I'm creating the objects...).

The problem im having is the fact that it will not save the relationships for some reason, ALl values ARE being passed and populated properly.


DataMapper 1.6.0 - El Forum - 01-23-2009

[eluser]OES[/eluser]
Ok I think I have gone totally mad. I hope someone can help before I pull all my hair out.

I have setup a new install and have installed as per the manual and have left all config items the same and have no table prefix as per the suggestion. (Can I take it config/datamapper is auto loaded ?).

But I just cannot get datamapper to relate tables.

Ie, A very basic example User & Country. I have set the tables below with sample data excatly as you can see here. http://screencast.com/t/sUHM9ERWluG.

Also set up the Models as per examples. Its all real basic.

But If I run this code.

Code:
$u = new User();
$u->get();
        
$u->country->get();

Im just trying to get all users and the country.

No matter whatever I try I just get SQL errors ie.

Code:
Unknown column 'countries.*' in 'field list'
SELECT `countries`.`*` FROM (`countries`) LEFT JOIN `countries_users` ON `countries`.`id` = `countries_users`.`country_id` LEFT JOIN `users` ON `users`.`id` = `countries_users`.`user_id` WHERE `users`.`id` = 1

Why is it trying to select countries?.

I also tried setting the auto_populates to TRUE and still no countrys.

Hope someone can advise.

And thank you in advance if you can.


DataMapper 1.6.0 - El Forum - 01-23-2009

[eluser]tdktank59[/eluser]
@OES

What are you trying to get???

I think your problem is calling the country with $u...

Checkout the examples.php in the saving relations area it might answer your question otherwise let me know what you are trying to get and ill see if I can help.


Also an update to my problem...
For some reason if I save the object before the relations then get the object back and then save the relations it seems to work...

Code:
$b = New Brainstorm();
$b->title                     = set_value('title');
$b->description               = set_value('description');
$b->orgin_date                = mktime(0,0,0,$date[0],$date[1],$date[2]);;
$b->estimated_count_total     = set_value('estimated_count_total');
$b->estimated_count_manual    = set_value('estimated_count_manual');
$b->save();

$b1 = New Brainstorm();
$b1->where('title',set_value('title'))->get();

$u = New User();
$u->where('id',set_value('created_by'))->get();

// Save the relations
$b1->save(array($u));



DataMapper 1.6.0 - El Forum - 01-23-2009

[eluser]OES[/eluser]
Thank you tdktank59

I may be a thick head but I though the idea of ORM was to do all my inner joins etc.

So im just trying to get all users with the country.

So just imagine I want to look all usernames and the Country.

Any suggestions ?

And thank you


DataMapper 1.6.0 - El Forum - 01-23-2009

[eluser]tdktank59[/eluser]
SO

User 1 -> country
user 2 -> country
user 3 -> country

or

Country -> user1, user2, user3
country2 -> user4, user5

which method?

or

user1
user2
user3

contry1
country2
country3

Im still not sure what you are looking for...