Michael Miller Fri Jul 16 02:37:58 -0400 2010

Subject: Foreign key validation?

Hello,

I have several foreign key constraints in my database, and I was wondering if there was a technique or standard way for validating them within php.activerecord.

From the list:
http://www.phpactiverecord.org/projects/main/wiki/Validations

I am not exactly looking for something this strong:
http://guides.rubyonrails.org/activerecord_validations_callbacks.html#validates-acceptance-of
(as I understand it, this would run the validators of the associated objects, I merely wish to detect the presence or lack thereof)

Cheers,
-Michael


Kien La Fri Jul 16 21:13:53 -0400 2010

Try the following:

static $validates_presence_of = array(
array('fk_id'),
array('another_fk_id')
);

Michael Miller Tue Jul 20 05:29:38 -0400 2010

Well, sure, but I was worried more along the lines of:

Author
id: 1 name: Jim Writerson

Book
id: 2 author_id: 1 name: PHP-Activerecord Makes PHP Fun!

$a = Author::find(1);
$a->delete()
(You shouldn't be able to delete an association because a Book is referring to it! Perhaps even given the option to delete 'force'=>true where it breaks the link, and delete 'associated' => true deletes any books that belong_to it)

Thanks to line 877 of http://github.com/kla/php-activerecord/blob/master/lib/Model.php
if (!$this->invoke_callback('before_destroy',false))

It seems reasonable that I could write a before-callback that interrupts the process.

(1-2/2)