I thought it delete all related lines from other tables:( I looked my tables and it did not delete.
So I have same problem.
It's related to the database you're using, mostly. afaik PostgreSQL supports cascading deletion but not MySQL, SQLite, …
You should use delete_all to remove all the related lines and then delete your stuff.
https://github.com/kla/php-activerecord/blob/master/lib/Model.php#L864-920
Try to set up InnoDB foreign key, it'll keep your DB sane.
Thanks for the reply.
Can u please explain the purpose of defining relationships in phpactiverecord, if we have to manually delete records related to a particular row.
PHP-AR is an abstraction level atop your database and it doesn't know all about it. The relationship you define are useful for getting stuff (what you do most of the time).
1 $myuser->payments;
2
3 $myuser->delete(); // will only delete the $myuser, not any payments.
You can override the delete method in your user. (This code hasn't been tested and I might be a little bit rusty)
1 class User extends … {
2 …
3 function delete() {
4 Payments::delete_all(array('conditions' => array('user_id' => $this->id)));
5 return parent::delete();
6 }
7 }
Wrong.
One should instead use before_destroy().
Here's an example:
class User extends … { … public function before_destroy() { $related_payment = Payments::find(array( 'conditions' => array( 'user_id' => $this->id) )); $related_payment->delete(); } }
(1-5/5)
Subject: Delete - Table relations
Hi All,
Im new to this amazing library and I need help with this:
'User' table and 'Payments' table has 1-many relationship. I need to delete all user related payment when a user is deleted. I know how to print all payments related to a particular user . But i do not know how to delete all payments related to that user.
Can someone please help me with that.
Thanks.