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 - 04-26-2009

[eluser]reyntjensw[/eluser]
I'd like to use this library, but I'm having some problems getting this to work.

I've already have created my db like this:

Contents
- id
- title
- content_category_id

Content_categories
- id
- title

And my models look like this :
Code:
<?php

    class Content extends DataMapper
    {
        var $table = "Contents"
        var $has_one = array('content_category'=>'content_categories')
    
    function Content()
    {
        parent::DataMapper();
    }
    
    }

?>
Code:
<?php

    class ContentCategory extends DataMapper
    {
        var $table = "ContentCategory"
        var $has_many = array('content'=>'contents')
    
    function ContentCategory()
    {
        parent::DataMapper();
    }
    
    }

?>

In my controller I query my db for all the content_category items
Code:
$query = $this->db->get('content_categories');
$data['results'] = $query->result_array();

In my view, I loop through the results, but I can't get all the content-entries to be displayed.
Code:
<?php foreach($results as $result): ?>
    <?= $result['title']; ?>
<pre>
    &lt;?php print_r($result); ?&gt;
</pre>
    &lt;?php foreach($result->content->get()->all as $content): ?&gt;
    <pre>
        &lt;?php print_r($content); ?&gt;
    </pre>
    &lt;?php endforeach; ?&gt;
&lt;?php endforeach; ?&gt;

What am I doing wrong?


DataMapper 1.6.0 - El Forum - 04-26-2009

[eluser]OverZealous[/eluser]
This line:
Code:
var $has_one = array('content_category'=>'content_categories');
is wrong. This hasn't looked like this since DataMapper 1.2 or something. Just make it this:
Code:
var $has_one = array('content_category');



DataMapper 1.6.0 - El Forum - 04-26-2009

[eluser]reyntjensw[/eluser]
I've changed that, but I'm still getting a Fatal error: Call to a member function get() on a non-object.
Do I have to change some other things to?


DataMapper 1.6.0 - El Forum - 04-26-2009

[eluser]OverZealous[/eluser]
Well, most likely "content" isn't set up correctly on whatever "result" is. Look at your code, make sure you've correctly set up all relationships (on BOTH models). Make sure that $result is a valid DataMapper model.

Only you are going to be able to debug your code, because only you have access to it.


DataMapper 1.6.0 - El Forum - 04-26-2009

[eluser]MeanStudios[/eluser]
I believe you are missing your relationship table between those two tabels.
You need to created one called: content_categories_contents with the fields:
id
content_category_id
content_id

Also in your ContentCategory model, you need to name the class Content_category and the var $table should be "content_category". DataMapper is case sensitive. I would recommend re-reading the General Topics on the manual.


DataMapper 1.6.0 - El Forum - 04-26-2009

[eluser]NachoF[/eluser]
I have run into another problem now Sad


I have events and products, in a many to many relation...
the thing is, the table events_products has one extra field called quantity... and I dont know how to add a value to that field.
heres my controller function so far.
Code:
function event_product()
{
        $event=new Event();
        $product=new Product();
        //$event->get_by_id($this->input->post('event_id'));
        $event->get_by_id("1");
        $product->get_by_id($this->input->post('product_id'));
        $producto->save($evento);
//where and how do I add quantity??
}
Please help.
Edit: Nevermind, figured it out, set_join_field is awesome


DataMapper 1.6.0 - El Forum - 04-26-2009

[eluser]MeanStudios[/eluser]
Not able to use the class 'Address' as the inflector helper turns it into 'Addres'.


DataMapper 1.6.0 - El Forum - 04-26-2009

[eluser]OverZealous[/eluser]
@MeanStudios
Make sure you are using the updated inflector helper included with DataMapper. I'm pretty sure that handles "Addresses".

If that doesn't work, then you can hard-code odd pluralizations directly into DataMapper, by setting $table on the class itself:
Code:
class Address extends DataMapper {
    var $table = 'addresses';
    // var $model = 'address'; // also an option if needed
    ...

Also, this is explained clearly in the documentation.


DataMapper 1.6.0 - El Forum - 04-26-2009

[eluser]MeanStudios[/eluser]
@OverZealous
Yup, read over that very carefully and that's why I am using var $table. I didn't know about var $model though. That has fixed my problem.
Address definitely doesn't work with the updated inflector helper that comes with the latest DataMapper.

It was trying to save to the field 'addres_id' in my relationship table as well. Once I used the var $model trick, everything works.

Thanks for the info.


DataMapper 1.6.0 - El Forum - 04-26-2009

[eluser]MeanStudios[/eluser]
*removed*

update:
n/m, forgot a reciprocal $has_one definition Wink.