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 - 12-10-2008

Thanks Stensi for sharing it with us Smile
going to upgrade now.

You're fast Smile

Life is easier with yoUSmile)))

DataMapper 1.6.0 - El Forum - 12-10-2008

@hugle: There's several things you're doing that you don't need to, such as creating $f2 when you already have $forum there to use.

I'd rewrite part 1 as just:

// save topic count for later use
$topic_count = $forum->topic->count();

I'm assuming you're trying to list the number of topics each forum has, and the number of posts each of those topics have. Is that correct? If so, there are many ways of doing it. I'll write up a quick sample when I have time. I can't right now since you've just caught me on my way out the door, sorry!

DataMapper 1.6.0 - El Forum - 12-10-2008

@stensi: Thanks a lot for such contribution.
I had quick view of all posts in this thread Smile

I had changed part1 as you sugested:
$f->get_by_id($forum->id); // i need this, since there are 4 different subforums, so I need to count topics in that forum
$topic_count = $f->topic->count();
somehow I thought it won't work, since I'm not PHP user too long, only 4 months. But I try to be better each day Smile

Yes stensi, you are 100% correct.
I now have 4 subforums, each subforum has N topics, and each of topics in this subforum has N posts.
I need to count how many posts have topics in each of subforum.

Thanks for your time.
Good luck and take care, wherever you would be Smile

DataMapper 1.6.0 - El Forum - 12-10-2008

I think i have found a bug tho i'm not sure.

I'm using
Codeigniter 1.7
Datamapper 1.5.4

I have created a table called products and to avoid conflicts trying to load a datamapper model (product) and a web page controller(product) [conflict = they have the same name] i have renamed my datamapper model to Db_product. I also have a table called agents and the two are related. An agent can have many products and a product can have many agents.

Thusly, I have a table called rel_agents_products (rel_ is my join prefix).

Next i have added the table variable in the db_product obviously so it knows which table to reference.

Now the problem...

I used the get() method on db_agent to call my agent data. then i attempted to load all related products for that agent.

$agent = new Db_agent();
$agent->get_by_id(1); // << My agent ID.
$agent->db_product->get(); << get the related products using my db_product dm model

when stepping through the code I found that the relationship was trying to use the db_product as the field name in the relationship ie. products.db_product_id.

obviously this isnt correct as the relationship table consists of id, agent_id and product_id.

I was forced to modify the Datmapper library to check if the table var has been set and so use a singular version of the table var, else use the model name.

Is this correct or have I solved a problem that there is another way around?

This is what i have changed in the Datamapper library.

Line 2104

$this->db->join($relationship_table, $this->table . '.id = ' . $relationship_table . '.' . $this->model . '_id', 'left');

..which i changed to..

$this->db->join($relationship_table, $this->table . '.id = ' . $relationship_table . '.' .(isset($this->table)?singular($this->table):$object->model) . '_id', 'left');


line 2105

$this->db->join($object->table, $object->table . '.id = ' . $relationship_table . '.' . $object->model . '_id', 'left');

..which i changed to..

$this->db->join($object->table, $object->table . '.id = ' . $relationship_table . '.' . (isset($object->table)?singular($object->table):$object->model) . '_id', 'left');

I hope i just made sense!?
Sorry about the long post.

DataMapper 1.6.0 - El Forum - 12-10-2008

Actually, what DM was doing IS correct. The name of the MODEL is the name of the relationship key. This is done on purpose, to allow for inheritance. Check out the Self Referencing Relationships section of the manual.

I recommend that, instead of renaming your models, think about renaming your controllers to be plural: /products/<whatever>.

If you do this, you won't have any conflicts, and it often reads a little better. That is, of course, your choice. If you decide to rename the models, you might want to be consistent, then, and rename the whole model, and all of the relationship fields.

DataMapper 1.6.0 - El Forum - 12-10-2008

Thanks for your response.

What you say makes sense and I have taken onboard your advice about the naming conventions I use and your right. It does make for easier reading.


DataMapper 1.6.0 - El Forum - 12-11-2008

@stensi, I somehow figured out the question about counting the posts.. maybe it is not as professional as it could be but It will be good for start Smile

now I have a question about delete_all function.

It somehow does not work as I expected. Maybe my approach is not possible at all, I will explain:

I have assoc array: Array ( [msg_id] => Array ( [0] => 25 [1] => 37 )

I thought, I could delete entries, like this:
#example 1
$m = new Privatemsg();
$m->where_in('id', array($this->input->post('msg_id')))->get();

it produces query:
"SELECT * FROM (`privatemsg`) WHERE `id` IN (Array)"

#example 2
So i tried a different approach:
$m = new Privatemsg();

            foreach ($m->all as $msg)
                if (in_array($msg->id, $this->input->post('msg_id'))) {

it throws error, and the QUERY WAS:
DELETE FROM `privatemsg_users` WHERE `privatemsg_id` = '25'.
it tries to access: `privatemsg_users` table, it is normal behaviour of delete function?
because my msg table is `privatemsg`

But after I looked in the database, I saw that that msg with ID = 25, was removed.
only msg ID 37 left.

Anybody have thought on this ?
Or I should somehow extend this functionality?

Thank you!

SOrry. example 2 works. I had a line in privatemsg model:
var $has_one = array("user");
which I removed now.

DataMapper 1.6.0 - El Forum - 12-11-2008

Sorry but you're doing a lot of non-standard things so I'm finding it difficult to answer your questions in a straight forward way. Same with your other issue, as I found it hard to understand the way you were approaching it code-wise.

Have you read the DataMapper User Guide? It explains how the delete_all() method works.

Quote:Delete All is used to delete all objects in an objects all list.

For example, let's say we have a users table with 100 users and we grab the first 10 users like this:

// Create user object and get the first 10 users
$u = new User();

The $u object's all list ($u->all) now contains an array of user objects, which is the 10 users returned. Doing this:

// Delete all users

...will delete the users in the $u object's all list. That is, it's the same as doing this:

foreach ($u->all as $user)

Whenever you do a get() on an object, the all list is populated. If the get() query resulted in more than one record, they go in the all list as well.

DataMapper 1.6.0 - El Forum - 12-11-2008

hi stensi ...
Just want to say that everything what you have done which is related to DataMapper is AWESOME.
Haven't explored all the availeable library , but everything which I have seen so far is great ...

Using CI I wanted to choose a clean version of active record for this framework ... I've choosen DataMapper instaed of ActiveRecord because of the documentation you provided is very clean ... as clean as CI documentation ...

All in all ... great job man

best regards

DataMapper 1.6.0 - El Forum - 12-12-2008

hi stensi, great work integrating all the new features with DM. I've been ultra busy and haven't had a chance to pipe in or work much with the new versions. So i installed DM 1.6 and tried running it but i keep getting the error:

Message: array_keys() [function.array-keys]: The first argument should be an array
Filename: libraries/datamapper.php
Line Number: 79

Message: Invalid argument supplied for foreach()
Filename: libraries/datamapper.php
Line Number: 79

this only happens when i'm extending Datamapper (which is every model plus a common extend i use).. it's like the static $config var vanishes when extended. I think i'm just missing something simple. I even created a totally blank model too, that just called parent:Big GrinataMapper() in the constructor and same error. Any hints here?..