ZF2 Cron Job call to Model - php

I am looking for help on ZF2 cron jobs. I will try to explain this the best I can. I am Fairly new with ZF2, sorry.
As a basic example, I have a cron job that runs through a list of active users. Currently the only way that works is recreating calls from my user model class, instead of reusing code already in place.
Example of work works
$this->usersTableGateway = new \Zend\Db\TableGateway\TableGateway('users', $dbAdapterTemp);
$users = $this->usersTableGateway->select(array('active' => 1));
This provides my users.
Why am I unable to use
if (!$this->usersTable) {
$sm = $this->getServiceLocator();
$this->usersTable = $sm->get('UserManagement\Model\UsersTable');
}
$this -> usersTable() -> getActiveUsers();
Is there a workaround for this for me to be able to call functions within my users model class? I am probably missing something really basic. I thought it had something to do with getServiceLocator but this statement works.
$this->getServiceLocator()->get('Config');
Thanks for the help in advance. Please let me know if you need more information.

Related

How to connect multiple db connection in ZF2 using PDO? [duplicate]

How to configure Zend Framework 2 project to work with multiple databases?
I have followed the answer in configure multiple databases in zf2
I created my own factory as MyProject/module/MyModuleName/src/MyModuleName/Classes/MyAdapterFactory.php. Is this a correct path to create the file?
I couldn't figure out where should I call:
$adapter1=$serviceManager->get('myadapter1');
$adapter2=$serviceManager->get('myadapter2');
And also I cant ask for more clarifications since the question is 'protected' and I'm a noob.
Thank you in advance and please save my day.
First of all, a better path would be modules/$Module/src/$Module/Db/Adapter/MyAdapterFactory.php, in conjuction with the namespace $Module\Db\Adapter (of course not "$Module".. ;) )
The examples of $serviceManager->get('myadapterX') are merely examples. Anywhere you have access to the ServiceManager you can call these adapter. On the controller level you'd do it this way:
$this->getServiceLocator()->get('myadapterX');
On configuration level when defining a TableGateway or such, it'd probably look something like this:
'my\Table\Gateway' => function ($sm) {
$dbAdapter = $sm->get('myadapterX');
$gateway = new Gateway($dbAdapter);
return $gateway;
}

Yii2 Model initialization AND session handeling

Here's a pickle :)
In the old Yii you could instantiate models anywhere in protected. Regardless of the module you were in. Then the Yii team decided to go and mess everything up :) and decided to change the structure and code and now, I'm a bit lost...
1. HOW DO YOU INSTANTIATE MODELS? (anywhere in the project)
Old fashion way was $model = new Model(); where model could be in a totally different module and it would still work. How do we do this now? when I try to do it, it says: Class 'app\modules\somemodule\controllers\Model' not found which is funny because I want a model and it searches in controllers...
2. SESSIONS IN YII
Old fashion way was
Yii::app()->session['var'] = 'value';
echo Yii::app()->session['var']; // Prints "value"
How are they done now?
L.E: Found my answer to the second question :D and sessions are handled about the same: Yii::$app->session['var'] = 'value'; the difference being the $... It's all about the $ :)
Thank you!
Ares.
Funny how no one showed interest in my question...
Anyways, nothing changed about it. If you need new fresh instance it's just "new Post()". If you need to get AR model with data it's "Post::find->where(...)->one()"
BUT you have to:
Either import class from another namespace with use:
use app\modules\someModule\models\Post;
// ...
$post = new Post();
or use fully qualified class name:
$post = new \app\modules\someModule\models\Post();
Hope this helps others as well as it did me :D

Zend Framework Action Stack Alternative

is there a way in Zend to call one controller from another?
I have seen the action stack but that does not seem to work for me and i have read that alot of people think it to be EVIL!
What i am trying to acchive is as follows:
Reports Controller scans through all the modules in the system, it then checks to see if a route has been registered for that module called MODULENAME-reports-run
The controller then reuns that registered route to generate all the reports from all the modules.
The idea is that i can create modules for my application that clients can simple drag and drop into place and the system picks up on the reports.
Your controller should not do any of these things. Your controller should only accept any input from the User Interface and then decide to delegate this to the appropriate classes in your Model.
If you have a ReportController, have it accept any input and forward it to a ReportsService or something else in the Model responsible for generating Reports. It's not the controllers responsibility to generate them.
It should look something like this:
public function generateReportAction()
{
try {
$service = new Model_ReportService;
$service->setReportToGenerate($this->getRequest()->getParam('reportId'));
$this->view->report = $service->generateReport();
} catch (ReportException $e) {
// do something with $e
}
}
If your ReportService needs to generate multiple reports, change the ReportService so it knows how to do that. You could do something like
$service = new Model_ReportService;
$service->setModulesDirectory('something');
$this->view->reports = $service->generateReportsForModules();
Personally I dont think a ReportService should need to know about a Module Directory, so you will want to give the public interface of that Service some more thought. But in general, this is the way to go.
Whatever you do, dont do it in the controller. Controllers ought to be slim.

retrieve sql query from cakephp finder method

I'm creating a behavior that log to a table the sql query executed by a particular Model in a controller. Looking for a method to return me the sql query executed for a particular finder method (like $this->MyModel->find('all') ) I found on the bakery that I can use $this->MyModel->find('sql'), but doesn't work for me. Someone knows how can I achieve this?
Thanks in advance
You can put this function in your app_model.php:
function getLastQueries()
{
$dbo = $this->getDatasource();
$logs = $dbo->_queriesLog;
return $logs;
}
And call it from any model ($this->getLastQueries()) or controller ($this->Model->getLastQueries()) to get them.
$this->Model->find('sql') is not supported natively by Cake. You have to follow the rest of the instructions in the Bakery article for installing a new DBO driver, and adding support for the find('sql') method in your AppModel. Once you do this, it should be able to get you what you're looking for.
http://bakery.cakephp.org/articles/grant_cox/2008/06/23/get-the-find-query-sql-rather-than-query-result

How do I call other Controllers within a Zend Framework Process and retrieve their rendered view?

I have the following setup:
An endless running PHP process that looks at a job queue which contains module names, controller names, action names and a parameter array.
For every job I want to call the given controllers action and retrieve the rendered view for further processing.
I was thinking about bootstrapping an instance of Zend_Application for every job but not exactly sure on how to handle the rest. Maybe there is also a better way.
So my question is:
How do I call other Controllers within a Zend Framework Process and retrieve their rendered view?
Thanks to everyone in advance!
I would think taking the Front_Controller and dispatching a new request would be the best to do.
Something like this, from your controller (not working code):
$frontController = $this->getFrontController();
$newRequest = new Zend_Controller_Request_Http();
$newRequest->setActionName('newAction');
$newRequest->setControllerName('newController');
$response = new Zend_Controller_Response_Http();
$frontController->dispatch($newRequest, $response);
It might not be this simple, but something to think about...
$this->_forward('otherAction', 'otherControllerOrNull');
http://framework.zend.com/manual/en/zend.controller.dispatcher.html
You can read this thread : Calling member function of other controller in zend framework?
Could you not use an Action View Helper? If you want the output in your controller then you can simply use $this->view->action('someAction', 'someController'); or the ActionStack helper.
In either case, be aware of the performance implications though. See Why the Zend Framework Actionstack is Evil for more details.

Categories