有一段时间,我一直在使用自动加载的Doctrine_Record类;但读了一些之后,我决定同时实现Doctrine_Records和自定义表类。
所以我把这个加到我的鞋带里
$manager->setAttribute(
Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES,
true
);这使得自定义表类正常工作..。但它打破了自动记录!
如何使这两种方式都自动生效?
也就是说,新的User.php获得我的用户Doctrine_Record类,Doctrine_Core::getTable(' User ')获取我的自定义UserTable类。
在我尝试实现自定义表之前,以下是它的工作方式:
public function _initDoctrine() {
require_once 'Doctrine.php';
/*
* Autoload Doctrine Library and connect
* use appconfig.ini doctrine.dsn
*/
$this ->getApplication()
->getAutoloader()
->pushAutoloader(array(
'Doctrine',
'autoload'),
'Doctrine');
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(
Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE,
true
);
$manager->setAttribute(
Doctrine::ATTR_MODEL_LOADING,
Doctrine::MODEL_LOADING_CONSERVATIVE
);
// try these custom tables out!
// $manager->setAttribute( Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true );
$config = $this->getOption('doctrine');
$conn = Doctrine_Manager::connection($config['dsn'], 'doctrine');
return $conn;
// can call flush on the connection to save any unsaved records
}谢谢
编辑:
让我澄清一下。
不仅仅是自定义类..。我已经使用了扩展Doctrine_Record的自定义类。
class Model_User extends Doctrine_Record {}
$foo = new Model_User;我的很多应用程序目前都是围绕这一点工作的,在这方面不会改变。
不过,我想也要使用自定义表
class UserTable extends Doctrine_Table {}
$bar = Doctrine_Core::getTable('User');但是,一旦我启用这个(自定义表类)特性,就可以调用使用表后缀的Doctrine_Table类。我之前扩展和直接调用的任何Doctrine_Record类都会停止工作!我想两者兼得!
发布于 2012-05-30 01:30:32
我发现问题了!
您必须确保每个 x.php Doctrine_Record类都有一个关联的xTable.php Doctrine_Table类,否则记录加载会中断!
发布于 2012-05-29 09:19:49
我并不真正理解您关于自定义类的问题,但无论如何,这里是我在ZF中使用mysql和UTF-8的Doctrine1.2.4引导程序。它不需要一个require()并完美地加载我的所有模型。
protected function _initDoctrine()
{
$this->getApplication()->getAutoloader()
->pushAutoloader(array('Doctrine', 'autoload'));
spl_autoload_register(array('Doctrine', 'modelsAutoload'));
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
$manager->setAttribute (
Doctrine::ATTR_MODEL_LOADING,
Doctrine::MODEL_LOADING_CONSERVATIVE
);
$manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);
$dsn = $this->getOption('dsn');
$conn = Doctrine_Manager::connection($dsn, 'doctrine');
$conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
$conn->setCollate('utf8_unicode_ci');
$conn->setCharset('utf8');
$conn->setAttribute(Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true );
Doctrine::loadModels(APPLICATION_PATH . '/models');
return $manager;
}理论模型存储在“应用程序/模型”中。
然后在我的申请中[吐露/申请]。
autoloadernamespaces[] = "Doctrine"
dsn = "mysql://your_login:your_pass@server_ip/database"https://stackoverflow.com/questions/10795215
复制相似问题