views:

857

answers:

1

My problem getting Zend Framework to provide a DRI layer can now be summarized as such.

Using the class definitions below I am able to delete the user but not the related comment through my local UserController "public/users/delete/userId/22", even though I have set up a refernece map and table relationship definition.

Does anyone have any answers to why the associated comment record is not deleted when i delete the users record?

    class Default_Model_DbTable_Comment extends Zend_Db_Table_Abstract
    {
        /**
         * @var string Name of the database table
         */
        protected $_name = 'comment';

        /**
         * @desc  reference map 
         * 
         * Rows in the comment table are to be automatically deleted if the row in the 
     * User table to which they refer is deleted
     *    
     */
     protected $_referenceMap = array(
        'User' => array(
         'columns'   => 'user_id',   // the foreign key(s)
      'refTableClass' => 'Default_Model_DbTable_Users',
         'refColumns' =>  'id',
         'onDelete'  =>  self::CASCADE,
        )
    );

}
class Default_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
    /**
     * @var string Name of the database table
     */
    protected $_name = 'users';


     /**
     * @desc Defining referential integrity here since we are using MyISAM
     * Dependent tables are referred via the class name. 
     */
    protected $_dependentTables = 'Default_Model_DbTable_Comment';



}
A: 

I've created models as yours and on testing it appears that it only works if dependent tables are listed in an array:

class Default_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
    /**
     * @var string Name of the database table
     */
    protected $_name = 'users';


     /**
     * @desc Defining referential integrity here since we are using MyISAM
     * Dependent tables are referred via the class name. 
     */
    protected $_dependentTables = array('Default_Model_DbTable_Comment');



}

When they aren't listed in an array, I get the error

Warning: Invalid argument supplied for foreach() in C:\PHP\includes\ZendFramework-1.8.4-minimal\library\Zend\Db\Table\Row\Abstract.php on line 632

This error may not have been visible in your environment.

David Caunt
How do you get errors like that.. since I dont find the Zend stack trace errors very useful?
Daniel Higgins
Also, I just tried the suggestion of defining dependentTables in an array and the app fell over with a Zend stack error:Stack trace: #0 C:\wamp\www\megashare\application\models\UsersMapper.php(104): Zend_Db_Table_Row_Abstract->delete()#1 C:\wamp\www\megashare\application\models\Users.php(761): Default_Model_UsersMapper->deleteUser(23, Object(Default_Model_Users))#2 C:\wamp\www\megashare\application\controllers\UsersController.php(113): Default_Model_Users->deleteUser(23)
Daniel Higgins
I think that error requires error_reporting to show E_STRICT in addition to E_ALL - in any case, make sure that all error conditions are shown. The only difference in my code and yours is that I didn't use a namespace for the model class names, i.e. my classes were Users and Comment. I didn't use a datamapper but this seems irrelevant; it just calls delete() on a row as I did.
David Caunt
I think the data mapper could be causing the problem, I have not tried without, but surely their must be a way to make this work with a data mapper?.. To be honest I am losing faith with Zend at the moment in favour of trying my hand at Drupal. Gonna spend the next 3 days hacking with Drupal to see if its time for a change...
Daniel Higgins
Thanks for the tip on error reporting
Daniel Higgins

related questions