Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]CI-Newb[/eluser]
@wanwizard: How would one go about loading the email library in a datamapper model. Using $this->load does not work for obvious reasons. I'm not 100% sure about the work around. Would this be the best way to acomplish it:

users model
Code:
function dosomething() {
            $ci = & get_instance();
            $ci->load->library('email');
            $ci->email->from('','');
}
etc

[eluser]Maglok[/eluser]
[quote author="WanWizard" date="1344955119"]So what exactly is the issue, because your approach is spot on?[/quote]
I... hmm. Yeah, *shrug* I think my thought train was 'The event should not know the person, so why is the person_id in there?' but that was just wrong train of thought.

Gawd I love this library. Smile

[eluser]WanWizard[/eluser]
@CI-Newb,

A Datamapper model extends a library, so you have to work as you would with a library, use get_instance().

You can work around it by including this in your model:
Code:
/**
* __get
*
* Allows models to access CI's loaded classes using the same
* syntax as controllers.
*
* @param string
* @access private
*/
function __get($key)
{
    static $CI;
    is_null($CI) and $CI =& get_instance();

    return $CI->$key;
}

[eluser]Maglok[/eluser]
A theoretical:

If this is the structure
Person has 0.* Events
Event has 0.* Dates

A Person thus should be able to get all the Dates he is going to be busy with events.

One would like to do: $person->events;

But instead it is a funky foreach loop that first grabs the dates from events the person is attending and somehow adds that to the $person-events var.

Has to be an easier way. I checked on cloning and copying, but it doesn't quite give me what I need.

At the moment it works just fine, but quite 'ugly' with a foreach loop that grabs all the bits I need and glues them all together, but it forces a lot of database querying on the background.

Small note: Count() functions in datamapper are really sweet. Smile

[eluser]Carmichael[/eluser]
I just started using DataMapper. I'm having trouble getting into relationships.

Code:
class Product extends DataMapper {

var $table = "products";

var $belongs_to = array(
  "category"
);

var $has_many = array(
  'attribute' => array(
    'class'    => 'attribute',
    'other_field'  => 'product',
    'join_self_as'  => 'product',
    'join_other_as' => 'attribute',
    'join_table'  => 'product_attributes'
  )
);

class Attribute extends DataMapper {

var $table = 'attributes';

var $has_many = array(
  'product' => array(
    'class'    => 'product',
    'other_field'  => 'attribute',
    'join_self_as'  => 'attribute',
    'join_other_as' => 'product',
    'join_table'  => 'product_attributes'
  )
);

I have three tables. Products, attributes and product_attributes. In products I store all the products with id and name. In attributes I store all attributes with id and name. The product_attributes table works as a map between product and attributes. I store all the products attributes in that table with product_id, attribute_id and value.

[eluser]Jacob F.[/eluser]
[quote author="WanWizard" date="1345019808"]There's a bit more info on advanced relations here, with a discussion I think is relevant in this case.

One of the issues is that Datamapper always needs both sides of the relation, they have to be symmetrical. So the user model needs two has_many definitions, that have to point back to the admin and publisher definitions on the materials model. If those are missing several types of query will fail.

So instead of the current 'material' relation you need the relation 'publishes' (which links to the publisher_id relation) and 'administers' (which links to the admin_id relation).

I'll see if I can come of with a more elaborate example for the docs.
[/quote]

Ahha! That link was very helpful--thanks! I think I've almost got it, just 2 small questions:

1. How to I specify the "id" column `users`.`id`? When I put 'join_self_as' => 'id' DataMapper still appends _id so it becomes id_id and putting 'join_self_as' => '' results in _id

2. Can I name the $has_many index & "class" index whatever I want (and then use the same name to reference it in other models)?

In case this helps (just posting code for one of the relations since the second will be just like it):
Code:
`users`
| id | fname | sname  | etc... |
|  1 | jacob | foobar | ...    | //admin
|  2 | jason | bazvar | ...    | //publisher

`materials`
| id | lang_id | admin_id | publisher_id |
|  1 |       1 |        1 |            2 |
Code:
class Material extends DataMapper {

var $has_one = array(
  'administrative_approval' => array(
      'class'         => 'user',     //references the `users` table/model
      'other_field'   => 'material', //references the `materials` table/model
      'join_self_as'  => 'admin',    //references `materials`.`admin_id`
      'join_other_as' => '',         //I think this should reference `users`.`id`
      'join_table'    => '',
      'reciprocal'    => FALSE,
      'auto_populate' => NULL
    )
);

}//Material{}
Code:
class User extends DataMapper {

var $has_many = array(
  'administrative_approval' => array(
      'class'         => 'material',
      'other_field'   => 'user',  //references the `users` table/model
      'join_self_as'  => '',      //I think this should reference `users`.`id`
      'join_other_as' => 'admin', //references `materials`.`admin_id`
      'join_table'    => '',
      'reciprocal'    => FALSE,
      'auto_populate' => NULL
    )
);

}//User{}
Code:
$this->materials
->include_related('user',array('fname','sname'),FALSE)
->get();

You've got a great memory btw! (remembering that other forum thread/post)

Thanks!

[eluser]Maglok[/eluser]
@Carmicheal: Is there a question in there?

I think the 'other_field' might not be required in a has_many.

The concept looks solid. You have 2 tables, they have a has_many relationship and thus you use the join table to relate them together.

Maybe your problem is getting the value of the join table? You do that like this:

Code:
$something->product->include_join_fields()->get();
echo $something->join_value;

http://datamapper.wanwizard.eu/pages/get...oin_fields

[eluser]WanWizard[/eluser]
@Jacob F.

Quote:1. How to I specify the “id” column `users`.`id`? When I put ‘join_self_as’ => ‘id’ DataMapper still appends _id so it becomes id_id and putting ‘join_self_as’ => ‘’ results in _id
It doesn't matter. It's a one-to-many relation, so Datamapper knowns which table has the FK, and therefore which field to use. The other side is always 'tablename.id', this is hardcoded.

Quote:2. Can I name the $has_many index & “class” index whatever I want (and then use the same name to reference it in other models)?
The index, yes, you can name that whatever you want. You can even create multiple entries all pointing at the same other model. You can NOT name 'class' whatever you want, it has to be the name of the model class. Otherwise Datamapper doesn't know which model to load if you reference a relationship (i.e. the index).

[eluser]Carmichael[/eluser]
I forgot to write the question haha. I want to be able to retrieve all attributes a product has in product_attributes and then from that retrieve attribute data from attributes.

I tried your code, but it doesn't work.

[eluser]Maglok[/eluser]
To retrieve data from the join table (product_attributes) just populate the join_fields like I mentioned then reference them as explain by using 'join_nameofthevar' as above.

Attribute data is automatically connected to the product through the join table. You can thus do:

Code:
foreach($product->attribute as $attribute)
{
// Do stuff
}




Theme © iAndrew 2016 - Forum software by © MyBB