Zend - db table not found in another module - php

I have an application with three modules:default, disciplines and plans. In disciplines I have a dbtable which works fine in this module, but if I want to use the dbtable in module plans inside plans_dbtable I get
Class 'Disciplines_Model_DbTable_Disciplines' not found in C:\xampp\htdocs\proiect_mps\application\modules\plans\models\DbTable\Plans.php on line 43.
Require_once and include don't solve the problem. I have Disciplines_Boostrap and Plans_Bootstrap classes written. But it doesn't work. Any ideas?
class Plans_Model_DbTable_Plans extends Zend_Db_Table_Abstract
{
...
public function addPlan(
$year,
$name,
$code,
$domain,
$specialization,
$years)
{
// Id-ul disciplinei
$id_discipline = 0;
$discipline = new Disciplines_Model_DbTable_Disciplines();
....
}
...
}

Since you're using Zend I would not suggest your answer of having a require_once to be the best Basically if you have your configuration nice you dont need to have require_once any place. This might be of a help :
In file application.ini
;Module Configuration
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleControllerDirectoryName = "controllers"
; Enables Modules bootstrap resource plugin, so each module directory has a bootstrap.php file
resources.modules = 1
and in you BootStrap.php file
protected function _initFrontController() {
// The Zend_Front_Controller class implements the Singleton pattern
$frontController = Zend_Controller_Front::getInstance();
// look in the modules directory and automatically make modules out of all folders found
$frontController->addModuleDirectory(APPLICATION_PATH . '/modules');
// forces the front controller to forward all errors to the default error controller (may already be false by default)
$frontController->throwExceptions(false);
return $frontController;
}
And yes you will need to have Bootstrap.php for your every module
class Disciplines_Bootstrap extends Zend_Application_Module_Bootstrap
{
/**
* This file is ABSOLUTELY NECESSARY to get module autoloading to work.
* Otherwise calls to "$form = new Module_Form_MyForm()" will fail.
*/
}

I think I've resolved it myself. I had to write
require_once(APPLICATION_PATH.'/modules/disciplines/models/DbTable/Disciplines.php');
instead of
require_once '/proiect_mps/application/modules/disciplines/models/DbTable/Disciplines.php';
This also works:
require_once('/../../../disciplines/models/DbTable/Disciplines.php');
for my folder structure.

Related

Zend view helper not available in module

I've read many posts about how to register a custom view helper. This has not been my issue. I have custom view helpers working from my library; however, they seem to only be accessible from the "default" module (i.e. outside of a module). When attempting to call the helper from within a module, I get the following error:
Message: Plugin by name 'IsCurrent' was not found in the registry; used paths: Zend_View_Helper_: Zend/View/Helper/:/Users/firemanrob/Development/Projects/mysite.com/www/application/modules/admin/views/helpers/
As stated above, when I make this call from the outside of a module, it works fine...
if ($this->isCurrent(...
But this same line, in a view script, in a module produces the error.
My helper class is in a file called
library/Sc/View/Helper/IsCurrent.php
The class is defined and function declared as such:
class Sc_View_Helper_IsCurrent extends Zend_View_Helper_Abstract {
public function isCurrent($startTime, $endTime) {
My Bootstrap.php file (among other things) contains this:
function _initViewHelpers() {
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->setViewSuffix('php');
unset($viewRenderer);
/* #var $view Zend_View */
$view = $layout->getView();
$options = Zend_Registry::get('options');
if (Zend_Registry::isRegistered('db')) {
$this->_db = Zend_Registry::get('db');
} else {
$db = $this->getPluginResource('db');
$this->_db = Zend_Db::factory($db->getAdapter(), $db->getParams());
Zend_Registry::set('db', $this->_db);
}
$view->doctype('HTML5');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$view->headTitle($options->template->websiteBaseTitle);
$view->headTitle()->setSeparator(' | ');
$view->addHelperPath("Sc/View/Helper", "Sc_View_Helper");
}
The only thing my application.ini file contains, pertaining to modules or view helpers is:
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
; setup modules
resources.modules[] =
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
;content settings
content.path = "../application/content"
;layout settings
resources.layout.layoutpath = APPLICATION_PATH "/layouts"
resources.layout.viewSuffix = php
resources.layout.layout = "default"
autoloadernamespaces[] = Amg
autoloadernamespaces[] = Sc
autoloadernamespaces[] = Phpmailer
I've tried commenting out the resources.modules[] line, as I read something similar in another post. That just broke everything.
The particular module that I'm trying to call this from is called "admin" and in that module's folder is another Bootstrap.php file that contains only:
<?php
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
Any suggestions of how I can make this working view helper visible to my modules as well?
This is a bit of a long shot, but could you try commenting out the three view renderer lines you currently have and adding the following to the end of your _initViewHelpers() function instead:
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
$viewRenderer->setViewSuffix('php');
$stack = Zend_Controller_Action_HelperBroker::getStack();
$stack->push($viewRenderer);
This should ensure that the $view object you're configuring is the one that the view renderer uses. Your helper path isn't in the error that you're getting, which suggests this is coming from a different view object.
I don't have an explanation as to why it works in the default module but not the admin one. If it worked from within a layout, but not from a view script that might make a little more sense.
It looks like you're using a relative filepath for you view helpers - this could explain why it works when you're in the default module but not in others because in other modules you change the relative path to your view helper is different than when in a module. Try something like $view->addHelperPath("/ABSOLUTE/PATH/TO/library/Sc/View/Helper", "Sc_View_Helper");

How to include custom folder in config in Zend Framework?

How to add include path to custom folder in application.ini in Zend Framework?
I need access to some classes from custom folder.
Thx for answers.
Kamilos
includePaths.customfolder = APPLICATION_PATH "/../customfolder"
structure your applications:
application
data
library
public
in application.ini
data_uploads = APPLICATION_PATH "/../data/uploads"
in Bootstrap.php
public function _initDefines()
{
define('DATA_UPLOADS', $this->getOption('data_uploads'));
}
now you can use DATA_UPLOADS define in your script php! ;)
UPDATE
add this to application.ini
autoloadernamespaces[] = "Foo_"
in you libray add directory Foo! In this directory add your classes.
the filename class will be Bar.php and class name will be Foo_Bar!

Integrating ZF/Doctrine2: Where do i put my Models/Entities & Proxy classes

if i do integrate Zend Framework 1.10 with Doctrine 2 where do i put my Doctrine Models/Entities and Proxies? i thought of the /application or the /library directories. if i do put in the /library directory tho, will it interfere with ZF autoloading classes from there since the classes there will be using PHP 5.3 namespaces vs PEAR style namespaces.
I'm working on an application that integrates Doctrine 2 with ZF1.10 also.You don't need to use the Doctrine auto loader at all.
1) In your application.ini file add the following line (assuming you have Doctrine installed in your library folder (same as the Zend folder):
autoloadernamespaces.doctrine = "Doctrine"
2) Create a doctrine or entitymanager resource. In your ini file:
resources.entitymanager.db.driver = "pdo_mysql"
resources.entitymanager.db.user = "user"
resources.entitymanager.db.dbname = "db"
resources.entitymanager.db.host = "localhost"
resources.entitymanager.db.password = "pass"
resources.entitymanager.query.cache = "Doctrine\Common\Cache\ApcCache"
resources.entitymanager.metadata.cache = "Doctrine\Common\Cache\ApcCache"
resources.entitymanager.metadata.driver = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.entitymanager.metadata.proxyDir = APPLICATION_PATH "/../data/proxies"
resources.entitymanager.metadata.entityDir[] = APPLICATION_PATH "/models/entity"
3) Next, you will need to bootstrap it. I added a resource class in my resources folder. Make sure you map to the folder in your ini file:
pluginPaths.Application_Resource_ = APPLICATION_PATH "/resources"
Then your resource class...
class Application_Resource_EntityManager
extends Zend_Application_Resource_ResourceAbstract
{
/**
* #var Doctrine\ORM\EntityManager
*/
protected $_em;
public function init()
{
$this->_em = $this->getEntityManager();
return $this->_em;
}
public function getEntityManager()
{
$options = $this->getOptions();
$config = new \Doctrine\ORM\Configuration();
$config->setProxyDir($options['metadata']['proxyDir']);
$config->setProxyNamespace('Proxy');
$config->setAutoGenerateProxyClasses((APPLICATION_ENV == 'development'));
$driverImpl = $config->newDefaultAnnotationDriver($options['metadata']['entityDir']);
$config->setMetadataDriverImpl($driverImpl);
$cache = new Doctrine\Common\Cache\ArrayCache();
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
$evm = new Doctrine\Common\EventManager();
$em = Doctrine\ORM\EntityManager::create($options['db'],$config,$evm);
return $em;
}
}
The doctrine 2 entity manager is now available to your application. In your controller you can grab it like so:
$bootstrap = $this->getInvokeArg('bootstrap');
$em = $bootstrap->getResource('entitymanager');
I am sure this will help somebody :)
In theory, you could put then anywhere, as long as the namespaces resolve correct.
I would suggest this structure:
/application/models/MyApp/Entities
/application/models/MyApp/Proxies
Load the 'MyApp' using Doctrine's ClassLoader. I've had no conflicts using the Doctrine loader with the Zend Loader (if you have classes that use the PEAR convention inside your namespace folder, you will still need to use the Zend Loader).
Remember that 'models' can be more than just your Entity classes. My model layer consists of interfaces, factories, validators and service objects. To that end, anything that is application specific business logic should probably go in the model folder.
I would put the Models in the same directory where the "normal" Zend Framework models life. /models
You can tell Doctrine to generate the models at this place, and prefix them with "Default_Model" or whatever.
Check out one of John Lebenshold Screencasts about Zend Framework and Doctrine
Zend Screencasts

Zend_Test_PHPUnit_ControllerTestCase and Zend_Layout, unable to find Layout plugin while running tests

I am starting to write some test cases for controller classes using Zend Framework 1.10.6 and Zend_Test_PHPUnit_ControllerTestCase. I am having problems with one item, which is that while the test cases are running, Zend_Controller_Action_HelperBroker can't find the Layout action helper.
Here are the bare bones of my test case:
require_once 'PHPUnit/Framework.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
require_once 'controllers/IndexController.php';
class Application_Controllers_IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase {
public $_application;
protected function setUp() {
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp ();
}
public function appBootstrap() {
// Create application, bootstrap, but don't run
$this->_application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$this->_application->bootstrap();
$this->getFrontController()->setParams($this->_application->getOptions())
->addControllerDirectory(APPLICATION_PATH . '/controllers');
}
public function testIndexAction() {
$this->dispatch('/index/index');
$this->assertController('index');
$this->assertAction('index');
}
}
I get an exception when I run the test case:
Zend_Controller_Action_Exception: Action Helper by name Layout not found
When I comment out the two lines in class Zend_Controller_Action_HelperBroker to try to find the source of this around line 368, I get:
Zend_Loader_PluginLoader_Exception: Plugin by name 'Layout' was not found in the registry; used paths:
Zend_Controller_Action_Helper_: Zend/Controller/Action/Helper/
The loading of layout scripts works fine in my application when running, it appears that the correct path or registry for the Zend_Controller_Action_Helper can't be found while running tests under PHPUnit and therefore the Layout plugin can't be loaded.
I have verified that Zend is installed correctly and that Layout.php is in the correct place.
Any ideas?
Del
In you appBootstrap() at the end place this line:
Zend_Controller_Action_HelperBroker::addHelper(new Zend_Layout_Controller_Action_Helper_Layout);
At which point do you add your Layout code?
Remember that the 'boostraping' is different when running a PHPUnit test, and that things that are bootstrapped in your main app might not be when running as a PHPUnit test.
My workaround:
function someAction() {
// workaround for unit tests 'Action Helper by name Layout not found'
if ($this->_helper->hasHelper('layout')) {
$this->_helper->layout->disableLayout(); // disable layouts
}
...

Include HTMLpurifier with Zend_Loader

I want to use the HTMLpurifier in combination with the Zend Framework. I would love to load the Class and its files with the Zend_Loader. How would you include it? Would you just use the HTMLPurifier.auto.php or do you know a better way of doing it?
I use HTML Purifier as a filter in my Zend Framework project. Here's an altered version of my class:
require_once 'HTMLPurifier.includes.php';
require_once 'HTMLPurifier.autoload.php';
class My_Filter_HtmlPurifier implements Zend_Filter_Interface
{
protected $_htmlPurifier = null;
public function __construct($options = null)
{
// set up configuration
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.DefinitionID', 'My Filter');
$config->set('HTML.DefinitionRev', 1); // increment when configuration changes
// $config->set('Cache.DefinitionImpl', null); // comment out after finalizing the config
// Doctype
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
// configure caching
$cachePath = APPLICATION_PATH . '/../cache/htmlpurifier';
if (!is_dir($cachePath)) {
mkdir($cachePath, 0755, true);
}
$cachePath = realpath($cachePath);
$config->set('Cache.SerializerPath', $cachePath);
if (!is_null($options)) {
//$config = HTMLPurifier_Config::createDefault();
foreach ($options as $option) {
$config->set($option[0], $option[1], $option[2]);
}
}
$this->_htmlPurifier = new HTMLPurifier($config);
}
public function filter($value)
{
return $this->_htmlPurifier->purify($value);
}
}
Unless I'm misunderstanding the question (Or HTMLpurifier). If you have Zend_Loader running and it's set to autoload.
require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();
Or something to that effect. Put the HTMLpurifier class in your library directory. I'm just not sure on it's actual class name.
You can just put the class file in the library directory and call it by it's name, or maybe toss it in a misc package.
Examples
// SITE/library/Zend/Auth.php
class Zend_Auth
{
}
// SITE/library/htmlpurifier.php
class htmlpurifier
{
}
// SITE/library/misc/htmlpurifier.php
class Misc_HTMLpurifier
{
}
Make sense?
you can register an autoloader class using the Zend_Loader class. when you call the registerAutoLoad() method without any parameters, you are actually registering Zend_Loader itself as an autoloader. so:
Zend_Loader::registerAutoLoad();
// equals to: Zend_Loader::registerAutoLoad('Zend_Loader'),true);
Zend_Loader tries to load classes using Zend Framework's naming convention, which is like this:
each class is defined in a separate file
each class name begins with a capitalized letter
underlines in class name, means a directory level.
so if 'Zend_Loader' is the name of a class, it is defined in the file 'Loader.php' in 'Zend' directory in your path. to you PHP can file load this class from file Zend/Loader.php
if your classes follow this naming convention, they can be automatically loaded using the same autoloader. else, you need to define your own autoloader. write an autoloader class winch can extend Zend_Loader, and define the loading functionality so that it will load classes with other naming conventions. then register your own autoloader with Zend_Loader. like this:
Zend_Loader::registerAutoLoad('myLoader',true);
I've put the contents of library of the archive of HTMLPurifier in my library path. So I have this directory structure :
library/
HTMLPurifier\
HTMLPurifier.auto.php
...
HTMLPurifier.safe-includes.php
And then I put this on top of the file where I'm using the HTMLPurifier :
require_once 'HTMLPurifier.safe-includes.php';
Ugly, but it's working.

Categories