2012-02-19 29 views
6

Tôi đang sử dụng Zend Framework để tạo một ứng dụng web. Dựa trên một số khuyến nghị, tôi đã chọn Doctrine làm hệ thống RDBM của mình.Cách tạo Proxies theo cách thủ công mà không có CLI trong Doctrine?

;--------------------------------------------------- 
; DOCTRINE CONFIGURATION 
;--------------------------------------------------- 
resources.entityManager.connection.driver = "pdo_mysql" 
resources.entityManager.connection.host = "localhost" 
resources.entityManager.connection.dbname = "private" 
resources.entityManager.connection.user = "private" 
resources.entityManager.connection.password = "private" 
resources.entityManager.connection.entities = APPLICATION_PATH "/models" 
resources.entityManager.connection.proxies.location = APPLICATION_PATH "/models/Proxies" 
resources.entityManager.connection.proxies.ns = "Proxies" 

; According to Doctrine manual, this should be true for 
; development, and false for production 
resources.entityManager.connection.proxies.generate = true 

Ở trên là cấu hình Doctrine của tôi trong ứng dụng Zend.ini. Mọi thứ đều hoạt động tốt, nhưng tôi muốn biết trước cách tạo Proxies bằng tay mà không cần sử dụng CLI vì nhiều lý do. Trước hết, Doctrine 2.0 doc đề cập rằng các proxy tạo tự động sẽ gây ra các vấn đề về hiệu suất. Thứ hai, tôi vẫn chưa tìm ra cách sử dụng Doctrine CLI đặc biệt là tôi đã chuyển phát triển dự án của mình sang một hộp máy chủ chia sẻ mà không cần truy cập dấu nhắc lệnh.

Tôi đã tạo các thực thể Doctrine theo cách thủ công bằng cách tạo các lớp. Làm cách nào để tạo thủ công các proxy Doctrine tương tự?

Trả lời

4

tôi đã tìm thấy cách dễ dàng để tạo ra các proxy:

$proxyDir = null; //to genearate to default proxy dir 
    $proxyFactory = $em->getProxyFactory(); 
    $metadatas = $em->getMetadataFactory()->getAllMetadata(); 
    $proxyFactory->generateProxyClasses($metadatas, $proxyDir); 

để tạo ra các đối tượng sử dụng:

$classes = $em->getClassMetadataFactory()->getAllMetadata(); 
    $generator = new \Doctrine\ORM\Tools\EntityGenerator(); 
    $generator->setGenerateAnnotations(true); 
    $generator->setGenerateStubMethods(true); 
    $generator->setRegenerateEntityIfExists(false); 
    $generator->setUpdateEntityIfExists(true); 
    $generator->generate($classes, '/path/to/generate/entities'); 
+0

Chúng tôi có tổ chức, tuy nhiên dòng thứ ba của mã của bạn (các metadatas $ line) cung cấp cho chúng tôi lỗi: Uncaught exception 'Doctrine \ ORM \ Mapping \ MappingException' với thông báo 'Trình điều khiển ánh xạ tệp phải có đường dẫn thư mục hợp lệ, tuy nhiên đường dẫn đã cho dường như không chính xác!' trong C: \ Zend \ Apache2 \ htdocs \ Webate \ library \ Doctrine \ ORM \ Mapping \ MappingException.php: 193 – Furyvore

+0

u cần thiết lập đường dẫn proxy trong EntityManager, ở đây mã từ dự án cũ của tôi: '$ config = new \ Doctrine \ ORM \ Configuration; \t \t $ config-> setMetadataCacheImpl (self :: getCache()); \t \t $ config-> setMetadataDriverImpl (new \ qweb \ driver \ DoctrineYml (QWEB_MODULE)); \t \t $ config-> setEntityNamespaces (mảng ('cơ sở')); \t \t $ config-> setProxyDir (DOCTRINE_PROXIES); \t \t $ config-> setProxyNamespace ('proxy'); \t \t $ connection = \ Doctrine \ DBAL \ DriverManager :: getConnection (tự :: loadYml (QWEB_CONFIG.DIRECTORY_SEPARATOR.'databases.yml ')); \t \t $ em = \ Doctrine \ ORM \ EntityManager :: tạo ($ connection, $ config); ' –

Các vấn đề liên quan