CodeIgniter Forums
Problem with using DataMapper ORM - 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: Problem with using DataMapper ORM (/showthread.php?tid=59308)



Problem with using DataMapper ORM - El Forum - 09-18-2013

[eluser]burakdirin[/eluser]
Hello,

I have a problem using a Datamapper with CI.

I created 2 tables.

Table 1 name : Roles
Table 2 name : Users

There is the relationship between the tables and connected with foreign key in mysql


Roles
---------
id
name
--------------------
User
---------
id
name
role_id

I don't want the deletion if role is being used. But, it deleted quickly with '$role->delete()' command.

Code:
class Role extends DataMapper {
   var $has_many = array('user');
}

class User extends DataMapper {
   var $has_one = array('role');
}

Thank you...


Problem with using DataMapper ORM - El Forum - 09-18-2013

[eluser]johnpeace[/eluser]
This might have some info helpful for you...

http://stackoverflow.com/questions/13464321/ci-datamapper-find-with-no-related-objects#8203;datamapper-find.


Problem with using DataMapper ORM - El Forum - 09-18-2013

[eluser]burakdirin[/eluser]
Thanks for comment but, I just want if role name is not used in user table then it can be deleted. Can I do it automatically in Model ? Because I do not want to check always.


Problem with using DataMapper ORM - El Forum - 09-18-2013

[eluser]WanWizard[/eluser]
You can run a related count, and only delete if the result is 0?

Code:
if ($role->user->count() == 0)
{
    $role->delete();
}



Problem with using DataMapper ORM - El Forum - 09-23-2013

[eluser]burakdirin[/eluser]
Thank you for helping.