I’ve updated this text to clarify this, and added a link to the save() method.
This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.
The active forums are here.
March 14, 2010 11:43pm
Subscribe [104]#751 / Nov 18, 2010 3:38pm
I’ve updated this text to clarify this, and added a link to the save() method.
#752 / Nov 19, 2010 10:38am
Deleting record error:
When running the delete() function on an object, it doesnt seem to be able to delete the relationships. According to the manual, the delete function has built in house cleaning.
The record Im trying to delete does exist, and has multiple relationships with one other table. Here is the error message Im getting:
========================
A Database Error Occurred
Error Number: 1451
Cannot delete or update a parent row: a foreign key constraint fails (`db01/posts_tags`, CONSTRAINT `Ref_21` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
DELETE FROM `posts` WHERE `id` = 3
=========================
Grateful for any help on this!
#753 / Nov 19, 2010 11:28am
Yes, it does.
But currently it can’t work if you have defined foreign key constrains on the tables. This is because it deletes the parent first, before it processes any relations.
Can you add a ticket for this issue at http://bitbucket.org/wanwizard/datamapper/issues, so I won’t forget to have a look at it?
If possible, attach an sql file with the create commands for your posts_tags and related tables, including all constraint definitions, so I have something to test with.
#754 / Nov 19, 2010 11:42am
gotcha.
i didnt intentionally add the constrains, my database designer software did.
so if i just remove it from the database modeling, it should work?
#755 / Nov 19, 2010 11:53am
In the meantime, I’ve been looking at the code.
What you can try: at the top of the delete() function, there are these two lines:
$this->db->where('id', $this->id);
$this->db->delete($this->table);Move these two lines down, just before this:
// Complete auto transaction
$this->_auto_trans_complete('delete');As far as I can see, this makes sure all relations are deleted first, before the record itself is deleted. From a logic point of view a better solution imho.
Could you try and see if this fixes your problem? If so you can leave the constraints in place.
#756 / Nov 19, 2010 12:22pm
thanks, that will most likely do it. will have to look into it tomorrow or monday, as im leaving the office now!
have a good weekend, and thanks again!
#757 / Nov 23, 2010 11:24pm
a quick update to my issue (the decrypt get_rules thing):
I was able to get it to work, but I had to move the setting of my decryption key outside the constructor. For some reason, either the constructor is not being run (which I HIGHLY doubt), or maybe the object attributes are being over-written or something. I noticed that when I was getting the CI instance, and trying to load the encrypt library in the constructor, then using it in the encrypt and decrypt functions. I kept getting an error of trying to call a function on a non-object. When I moved it all down into the functions, it worked just fine. The same thing happens with the decrypt key. I am now setting it when instantiating it instead of in the constructor, and it all works just fine.
Visual Aid time.
this works
class User extends DataMapper {
var $pass_key = "[removed]";
var $has_one = array('group');
var $has_many = array('maillist','email','sent_email','signature');
//.....
function __construct($id = NULL)
{
parent::__construct($id);
}
//.....
function _encrypt($field)
{
if (!empty($this->{$field}))
{
$CI = get_instance();
$CI->load->library('encrypt');
$this->{$field} = $CI->encrypt->encode($this->{$field}, $this->pass_key);
}
}
function _decrypt($field)
{
if (!empty($this->{$field}))
{
$CI = get_instance();
$CI->load->library('encrypt');
$this->{$field} = $CI->encrypt->decode($this->{$field}, $this->pass_key);
}
}this DOES NOT work:
class User extends DataMapper {
var $pass_key;
var $CI;
var $has_one = array('group');
var $has_many = array('maillist','email','sent_email','signature');
//.....
function __construct($id = NULL)
{
parent::__construct($id);
$this->pass_key = "[removed]";
$this->CI = get_instance();
$this->CI->load->library('encrypt');
}
//.....
function _encrypt($field)
{
if (!empty($this->{$field}))
{
$this->{$field} = $this->CI->encrypt->encode($this->{$field}, $this->pass_key);
}
}
function _decrypt($field)
{
if (!empty($this->{$field}))
{
$this->{$field} = $this->CI->encrypt->decode($this->{$field}, $this->pass_key);
}
}Can you think of any reason it wouldn’t work the other way?
#758 / Nov 23, 2010 11:38pm
Heh, ok, 1 more update.
I just tried setting the variables in the constructor, but BEFORE calling the parent constructor, and it all worked just fine. So, I guess, evidentally, when I was setting them (after calling parent), it was silently failing validation or something. But if you set them BEFORE calling parent, and not change them after, it works just fine.
VISUAL AID:
Works:
function __construct($id = NULL)
{
$this->pass_key = "[removed]";
$this->CI = get_instance();
$this->CI->load->library('encrypt');
parent::__construct($id); // <--- parent called last
}Does NOT Work:
function __construct($id = NULL)
{
parent::__construct($id); // <--- parent called first
$this->pass_key = "[removed]";
$this->CI = get_instance();
$this->CI->load->library('encrypt');
}#759 / Nov 24, 2010 4:08am
Ok, so your conclusion is that the Datamapper class constructor somehow interferes with your class property?
I’ll have to look into that, maybe the __get() magic method is doing something funny here…
#760 / Nov 24, 2010 4:15am
Ok, so your conclusion is that the Datamapper class constructor somehow interferes with your class property?
Makes it sound kinda harsh. Wasn’t meaning it that way, if that’s how I sounded. 😛
#761 / Nov 24, 2010 5:40am
No worries, it was just a wrap-up of your conclusion, to make sure I understood it. 😉
#762 / Nov 30, 2010 3:16pm
I figured it out.
#763 / Dec 06, 2010 1:27pm
Hi,
Does somebody knows the new Official Overzealous DMZ site?
http://www.overzealous.com/dmz is offline.
Does somebody assumed the project?
Maybe I’m speaking shit, I had no time to search forum about this… so, sorry if I did.
thanks.
#764 / Dec 06, 2010 2:56pm
@Lucas Alves
New official website: http://datamapper.exitecms.org/
Sorry about the old one going down without much warning. I’m (finally) moving, and so my servers will been shut down for a while. (I migrated the top-level page to my SliceHost account).
Edit:
I also updated the first post in this topic, and updated my signature.
#765 / Dec 06, 2010 3:22pm
Thanks @OverZealous