RedBeanPhp fuse with CI 2.1.0 HMVC - redbean

the controller
class User extends MX_Controller{
function __construct() {
parent::__construct();
}
function index(){
$r=R::dispense('group');
$r->GroupName="hh";
$i=R::store($r);
echo $i;
$r->hello();
}
}
the model
class Model_Group extends RedBean_SimpleModel{
function __construct() {
parent::__construct;
}
function open(){
echo "model";
}
function update(){
echo "update";
}
function hello() {
echo "hello";
}
}
the output
it just returns the id neither the hook functions (open -update - etc........)nor the custom functions (hello) as mentioned from the redbean docs
so i am asking what's wrong or what i should do to work properly

According to your code, I think you should load a model to fix this problem.
For example:
$this->load->model("module_name/model_name")

Related

Yii2 queue job constructor Dependency Injection alternative

I need to get dependencies in class that implements yii\queue\Job interface. In perfect world I would do something like this:
public function __construct(SomeInterface $service)
{
$this->service = $service;
}
public function execute($queue)
{
$this->service->doSomething();
}
Unfortunately yii2-queue doesn't support resolving dependencies in job handler constructor. For now I deal with it like this:
public function execute($queue)
{
$service = Yii::$container->get(SomeInterface::class);
$service->doSomething();
}
Maybe there is a cleaner way to do this?
I dealt with it using factory method pattern and resolving dependencies from DI container in it. Altought there are some problems with serialization of heavy dependecies. To resolve that I used simple proxy.
$this->queue->push(FooJob::create($someId));
// (...)
class FooJob implements Job
{
public $id;
private $fooService;
private $heavyService;
public function __construct(int $id, FooInterface $fooService)
{
$this->fooService = $fooService;
}
public static function create(int $id): self
{
return Yii::$container->get(static::class, [$id]);
}
public function execute(Queue $queue): void
{
$this->fooService->bar($this->id); // service available since construct
$this->heavyService->bark($this->id); // service available since first call
}
public function getHeavyService(Queue $queue): HeavyInterface
{
if (!$this->heavyService) {
$this->heavyService = Yii::$container->get(HeavyServiceInterface::class);
}
return $this->heavyService;
}
}
This is way more clean approach than one I used before. It's not perfect, but with Yii limitations its enough.

How to replace class with mock in DI? Phalcon + Codeception

I try writing functional tests for my controllers using Codeception testing framework. I want to replace real service in DI with fake one.
Controller code example:
<?php
namespace App\Controllers;
class IndexController extends ControllerBase
{
public function indexAction()
{
// some logic here
$service = $this->getDI()->get('myService');
$service->doSomething();
// some logic here
}
}
Test code example:
<?php
namespace App\Functional;
class IndexControllerCest
{
public function testIndexAction(FunctionalTester $I)
{
// Here i want to mock myService, replace real object that in controller with fake one
$I->amOnRoute('index.route');
}
}
I already try different combinations with Codeception Phalcon module like addServiceToContainer.
I setup Codeception using bootstrap.php file almost the same as for real app.
Phalcon version: 3.4.1
Codeception version: 3.1
So my question in last code fragment on comment section. Thank you for any help.
I would like suggest you start from creating a separated helpers to create and inject dependencies as follows:
# functional.suite.yml
class_name: FunctionalTester
modules:
enabled:
- Helper\MyService
- Phalcon:
part: services
# path to the bootstrap
bootstrap: 'app/config/bootstrap.php'
# Another modules ...
Create a separated service:
<?php
namespace Helper;
use Codeception\Module;
/** #var \Codeception\Module\Phalcon */
protected $phalcon;
class MyService extends Module
{
public function _initialize()
{
$this->phalcon = $this->getModule('Phalcon');
}
public function haveMyServiceInDi()
{
$this->phalcon->addServiceToContainer(
'myService',
['className' => '\My\Awesome\Service']
);
}
}
And use it in tests as follows:
<?php
namespace App\Functional;
use Helper\MyService;
class IndexControllerCest
{
/** #var MyService */
protected $myService;
protected function _inject(MyService $myService)
{
$this->myService = $myService;
}
public function testIndexAction(FunctionalTester $I)
{
$I->wantTo(
'mock myService, replace real object that in controller with fake one'
);
$this->myService->haveMyServiceInDi();
$I->amOnRoute('index.route');
}
}

code run before every actions in a module ZF2?

I want to write some code to run before every actions in my module. I have tried hooking onto onBootstrap() but the code run on the other modules too.
Any suggestions for me?
There are two ways to do this.
One way is to create a serice and call it in every controllers dispatch method
Use onDispatch method in controller.
class IndexController extends AbstractActionController {
/**
*
* #param \Zend\Mvc\MvcEvent $e
* #return type
*/
public function onDispatch(MvcEvent $e) {
//Call your service here
return parent::onDispatch($e);
}
public function indexAction() {
return new ViewModel();
}
}
don't forget to include following library on top of your code
use Zend\Mvc\MvcEvent;
Second method is to do this via Module.php using event on dispatch
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module {
public function onBootstrap(MvcEvent $e) {
$sharedEvents = $e->getApplication()->getEventManager()->getSharedManager();
$sharedEvents->attach(__NAMESPACE__, 'dispatch', array($this, 'addViewVariables'), 201);
}
public function addViewVariables(Event $e) {
//your code goes here
}
// rest of the Module methods goes here...
//...
//...
}
How to create simple service using ZF2
reference2
reference3

Forwarding and non-forwarding calls in late static binding (PHP 5.3)

<?php
class A {
public static function foo() {
static::who();
}
public static function who() {
echo __CLASS__."\n";
}
}
class B extends A {
public static function test() {
A::foo();
parent::foo();
self::foo();
}
public static function who() {
echo __CLASS__."\n";
}
}
class C extends B {
public static function who() {
echo __CLASS__."\n";
}
}
C::test();
?>
given below is the output:
A
C
C
can anyone evaluate tell how that output has been produced?
The result from the first line in test() "A" does not leverage Late Static Binding, since you are, in all cases, directly calling the class "A"'s implementation of foo (technically it is LSB, but static is bound to A). The second and third lines demonstrate expected behavior with the static keyword, the chain of inheritance does not matter, static refers to the called class. So even though you are calling parent::foo() from B, that implementation used LSB where static keyword resolves to the called class, which is C. The same happens with self::foo().

php5 --- method visibility issue

Please have a look at below given code.
<?php
class Bar
{
public function test() {
$this->testPrivate();
$this->testPublic();
}
public function testPublic() {
echo "Bar::testPublic\n";
}
private function testPrivate() {
echo "Bar::testPrivate\n";
}
}
class Foo extends Bar
{
public function testPublic() {
echo "Foo::testPublic\n";
}
private function testPrivate() {
echo "Foo::testPrivate\n";
}
}
$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate
// Foo::testPublic
?>
In above example, when we called $myFoo->test();it called testPrivate of Bar class
But how come it called testPublic of Foo class.
Can any one help me in this ?
Bar.testPrivate and Foo.testPrivate have to be protected methods instead of private ones. See here for more:
http://php.net/manual/en/language.oop5.visibility.php
Because test() is NOT in Foo and is running in Bar scope. Bar scope can't access to Foo private methods.
Just add test() to Foo...
Indeed one of the comments on the visibility page does reiterate this:
"private methods never participate in the in the overriding because these methods are not visible in the child classes."
It does feel a bit strange because you would think that the child class would override the parent with the method names being the same, but its not the case with private methods and the parents method takes precidence here, so best to use protected methods if you want to override.

Resources