According to this blog post it should be possible to have an init() method within the model. Like this:
class MyModel
{
public function init()
{
// prepare something
}
...
}
This does not work for me. Does anybody know if I have to set somewhere a parameter (e.g. in the config file) or if this is not possible any more in my Zend FW version (1.11.4)? I checked with error_log('something') within the init method and it does not get called at all.
UPDATE:
I have extended Adam's solution a little:
public function __construct() {
if (get_parent_class($this) != false) {
parent::__construct();
}
if(method_exists($this, 'init')) {
$this->init();
}
}
This way it can be placed in a base class and then extended. Just in case someone needs the same solution later too.
That post is very misleading. If the model classes in the application extend Zend_Db_Table or Zend_Db_Table_Row, which is not the case in the post's examples but is implied from the text, then yes you can add an init() method that will be called automatically. For this to work in your application you would need to be calling the method from whatever creates your models, or from the constructor of a base class.
Try adding a call to init() from the constructor as so:
public function __construct()
{
parent::__construct();
$this->init();
}
you have that problem because a php class does not have an init method. That init method is only applicable to Zend classes. Which mean you can only use it when you extend a Zend class. Which means you are actually overriding the init method.
E.g. here it is in the Zend_Db_Table_Abstract class:
/**
* Initialize object
*
* Called from {#link __construct()} as final step of object instantiation.
*
* #return void
*/
public function init()
{
}
So if you change your model to extend zend it will work:
class MyModel extends Zend_Db_Table_Abstract
{
public function init()
{
// prepare something
}
...
}
It's the same with Zend_Controller_Action, which is why you can also use it in your controllers.
Related
What would the magento 2 equivalent of Mage::helper('core')-> ?
The Mage static methods are not existing anymore, you will have to use dependency injection to get your helper instance, for example in your model:
<?php
namespace Mycompany\Mymodule\Model;
use Mycompany\Mymodule\Helper\Data;
class Custom {
private $helper;
public function __construct(
Data $helper
) {
$this->helper = $helper;
}
public function myMethod() {
$this->helper->helperMethod();
}
}
You can use the same system in Blocks etc.. and for existing helpers that you want to use.
in Symfony2 samples I can find how to access mongodb in from the Controller class:
$dm=$this->get('doctrine_mongodb')->getManager();
how to do it in an arbitray class?
You should use Dependency Injection to inject the doctrine_mongodb service into your class like this.
Create your class like this:
use Doctrine\Common\Persistence\ObjectManager;
class MyClass
{
protected $documentManager;
// ObjectManager is the interface used only for type-hinting here
// DocumentManager or EntityManager are the actual implementations
public function __construct(ObjectManager $om)
{
// The property $documentManager is now set to the DocumentManager
$this->documentManager=$om;
}
public function findSomething()
{
// now do something with the DocumentManager
return $this->documentManager->getRepository('YourBundle:SomeDocument')->findBy(array(/* ... */));
// ...
}
Then declare this class as a service:
# app/config/config.yml
services:
your_service_name:
class: Namespace\Your\SomeClass
arguments: ['#doctrine.odm.mongodb.document_manager']
In order to access your class from a controller it's service name to get it from the container (the DocumentManager will then be injected automatically into the constructor)
// Vendor/YourBundle/Controller/SomeController.php
public function doWhatever()
{
$myClassService = $this->get('your_service_name');
$something = $myClassService->findSomething();
}
what can I do to make the abstract function work in the emaillogger class?
class EmailLogger extends Zend_Log_Writer_Abstract
and I want to use the function _write
protected function _write($event)
{
$this->_events[] = $this->_formatter->format($event);
}
then I got this error
Class EmailLogger contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Zend_Log_FactoryInterface::factory)
I am not really sure what to do here
I try'd to use implements Zend_Log_FactoryInterface, but it diddn't work
thanks, Richard
Zend_Log_Writer_Abstract implements Zend_Log_FactoryInterface which has the following code:
static public function factory($config);
This forces the Zend_Log_Writer_Abstract and any child classes to also have a factory method. To satisfy this requirement, you could put in a wrapper method which calls the parent method:
class EmailLogger extends Zend_Log_Writer_Abstract
{
// Add this method in conjunction to what you already have in your class
public static function factory($config)
{
parent::factory($config);
}
}
i have created my own Zend_Form class currently just to add some helper functions and setdefault decorators.
now what i want to do is add some defaults (setMethod('post')) and add some elements (if u know the honeypot method to prevent spam bots.) how do i do it? in the __construct()?
Overwrite the contructor should be fine.
class Your_Form extends Zend_Form
{
public function __construct($options = null)
{
$this->setMethod('POST');
$this->addElement('hidden', 'hash', array('ignore' => true));
parent::__construct($options);
}
So your other Forms can extend Your_Form and call init(), so it stays consistent.
class Model_Form_Login extends Your_Form
{
public function init()
{
$this->addElement('input', 'username');
...
If you overwrite the init()-Method you don't have to call the parent::__construct()...
class Your_Form extends Zend_Form
{
public function init()
{
$this->setMethod('POST');
$this->addElement('hidden', 'hash', array('ignore' => true));
}
... but all your extending Forms have to call parent::init() like so
class Model_Form_Login extends Your_Form
{
public function init()
{
$this->addElement('input', 'username');
...
parent::init();
If you will read the __construct of Zend_Form you will see it is calling the empty method init().
So you have two choices:
create your own construct and do not forget to call the parent construct
(recommended) put what ever you want in the init() method. That way you will be sure that everything was instantiated correctly.
the init() is something consistent to the entire framework (which ever class you want to extend).
class My_Super_Form extends Zend_Form{
protected function init(){
//here I do what ever I want, and I am sure it will be called in the instantiation.
}
}
How to get resource in controller action?
Resource db was initialized in application.ini.
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// I want db resource here
}
}
Try and see if this works:
$this->getFrontController()->getParam('bootstrap')->getResource('db')
UPDATE : While this solution works, it is NOT a recommended
practice. Please, read comment by
#Brian M. below.
You can use Zend_Registry. Initialize the database connection in the bootstrap and store it in the registry:
// set up the database handler
// (...)
Zend_Registry::set('dbh', $dbh);
Then you can retireve it from anywhere else:
$dbh = Zend_Registry::get('dbh');
In answer to a similar question on Nabble, Matthew Weier O'Phinney (Mr Zend Framework 1) suggests using this form:
$this->getInvokeArg('bootstrap')->getResource('db');
So, in the context of this question, it would be something like:
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// db resource here
$db = $this->getInvokeArg('bootstrap')->getResource('db');
}
}