简体   繁体   中英

Relationships in Zend_DB_Table_Abstract

I'm using the default framework code that was created with the Zend Framework Application tool, I added some autoloader magic so that any classes named Default_<*>_<*> would automatically be loaded from the correct directory, in this case Default_Model_TableName .

application/models/ProjectCategories.php:

<?php

class Default_Model_ProjectCategories extends Zend_Db_Table_Abstract {
    protected $_name = 'categories';
    protected $_dependentTables = array('Projects');
}

application/models/Projects.php:

<?php

class Default_Model_Projects extends Zend_Db_Table_Abstract {
    protected $_name = 'projects';

    protected $_referenceMap    = array(
        'Category' => array(
            'columns'           => 'cid',
            'refTableClass'     => 'ProjectCategories',
            'refColumns'        => 'id',
            'onUpdate'          => self::CASCADE,
            'onDelete'          => self::CASCADE,
        )
    );

}

What I am attempting to do is the following:

<?php
$categories = new Default_Model_ProjectCategories();
$category = $categories->find('1');
$category->findProjects();

At which point I get an error thrown at me that it is unable to find Projects.php, and or that the file might not have contained a class named Projects.

At that point I place Projects.php in the include path that was set up by the framework (/../library/) and the file is found, but now I lose my whole directory structure, and naming because I had to rename Default_Model_Projects to Projects . I am able to get everything to work if I place the file back in its original location, and change

protected $_dependentTables = array('Projects');

to

protected $_dependentTables = array('Default_Model_Projects');

but this also means that my ->findProjects() now becomes ->findDefault_Model_Projects() .

Is there a way to tell it that when I am looking for findProjects() that it has to instantiate Default_Model_Projects ? Is this something that is missing from Zend Framework, or am I attempting to shoehorn something in a way that it does not belong? How have you solved this issue?

This problem may have been introduced by the new Autoloader, and the way in which it prefers to load namespaced classes (eg those prefixed as you have ).

In my applications I simply name models like 'Projects' and add a separate models folder to the include path for that application. This is one solution - unfortunately I don't know how you can make the namespaced models load correctly, but I would suggest looking at Zend_Loader in greater detail, and possible the pluginLoaders.

i used to be able to do something like

$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
    'basePath'  => APPLICATION_PATH,
    'namespace' => 'App',
));
$resourceLoader->addResourceType('model', 'models/', '');

to shrink my model classes to App_TableName but seems like its not working now...

Change

protected $_dependentTables = array('Projects');

to

protected $_dependentTables = array('Default_Model_Projects');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM