I would like to trigger some events in my model, if I register my events via app->on in index.php then I don't receive required $event->data from event object.
How register my events handlers, so when I call trigger it will call correct function with event object.
Code sample:
namespace app\modules\Product\models\Product;
class Product extends Entity\Entity {
public static function tableName() {
return 'sometable';
}
public function afterFind() {
parent::afterFind();
$event = new yii\base\Event;
$event->data = $this;
$this->trigger('calculatePrice', $event);
}
}
namespace app\modules\PriceRules\models;
class PriceRules extends yii\base\Model {
public static function calculatePrice($event) {
$entity = $event->data;
}
}
//somehow register events
Add in Product in method init this code:
class Product extends Entity\Entity {
public function init()
{
$this->on('calculatePrice', ['app\modules\PriceRules\models\PriceRules', 'calculatePrice']);
}
...
}
See doc - http://www.yiiframework.com/doc-2.0/guide-concept-events.html
I found solution. I placed PriceRules module inside bootstrap and added this code there:
yii\base\Event::on(Product::className(), 'calculatePrice', [PriceRulesEvent::className(), 'calculatePrice']);
Related
I create a file in yii2 "actions", and I create a class name actionC
is it possible to call function from
actions/actionC
inside a controller
my calss is
<?php
namespace app\actions;
class ActionC
{
protected function CPost(){
// return something
}
}
is it possible to call my function CPost() inside a controller actionView ?
Hello while your function is protected you can not call it, if you want to call a function of your class it has to be public it would be something like that
Class actionC
<?php
namespace app\actions;
class ActionC
{
protected function CPost()
{
// return something
}
public function BPost()
{
// return something
}
}
And in you View
<?php
$a=new \app\actions\ActionC();
$a->BPost();
//$a->CPost(); //this will be error because is protected
i made controller name crud.php this the code :
class Crud extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('m_data');
$this->load->helper('url');
}
public function admin(){
$data['produk'] = $this->m_data->tampil_data()->result();
$this->load->view('v_admin',$data);
}
}
i want take the admin() to place in admin.php code here :
class Admin extends CI_Controller{
function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
function index(){
$this->crud->admin();
}
}
what's code for take another function in another controller.. that's possible or not?
You could try to require Crud controller within Admin controller :
public function index()
{
require_once('crud.php');
$crud = new crud();
$crud->admin();
}
I do not know if I understood your question very well, but you could have the controller Admin extend the Crud controller, so he would have access to the admin ()
class Admin extends Crud{
function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
function index(){
$this->admin();
}
}
Loading another controller inside a controller is a bad idea. It can lead to hard to find bugs. One solution is to make Crud a custom library that the controller loads.
File: /application/libraries/Crud.php
class Crud
{
protected $CI;
public function __construct()
{
$this->CI = & get_instance();
$this->CI->load->model('m_data');
$this->CI->load->helper('url');
}
public function admin()
{
$data['produk'] = $this->CI->m_data->tampil_data()->result();
$this->CI->load->view('v_admin', $data);
}
}
Then the controller loads and uses the custom class.
class Admin extends CI_Controller{
public function __construct(){
parent::__construct();
if($this->session->userdata('status') != "login"){
redirect(base_url("login"));
}
}
public function index()
{
$this->load->library('crud');
$this->crud->admin();
}
}
You could load crud in the constructor too. That would make sense if other methods in the controller use the library.
May it can be useful for you, if you will customize your code in the view:
VIEW
your-link-to-another-controller
I have a function called wsGO() inside a class ceff(). I want to be able to run that function from within the class ceff. I used this code:
class ceff{
$ceff_instance = new ceff($this);
$ceff_instance = wsGO();
public function wsGO(){
....
}
}
However this didn't work. Any ideas?
In PHP you can only initialize a property with a constant - NOT functions or constructor calls. You can though do that in a constructor, such as:
class ceff{
public $ceff_instance;
public function __construct() {
$this->ceff_instance = $this->wsGO();
}
public function wsGO(){
....
}
}
I am a little confused by this concept.
If I override a public function in a base class, I would have thought that this override function is called and the original is ignored? though this doesn't seem to be the case...
public class AbstractScreen extends Sprite
{
public function AbstractScreen()
{
}
public function updateLanguage():void
{
trace("WARNING: need to override public function updateLanguage()");
}
}
public class Start extends AbstractScreen
{
override public function updateLanguage():void
{
title.text = _model.language.start
title.setTextFormat(titleFormat);
}
}
public class ViewManager extends Sprite
{
private var _model:Model;
private var _screens:Array;
public function ViewManager(model:Model)
{
_model = model;
_model.addEventListener(AppEvent.INIT_VIEW, initViews);
_model.addEventListener(AppEvent.UPDATE_VIEW, updateView);
_model.addEventListener(AppEvent.CHANGED_LANGUAGE, changeLanguage);
}
private function initViews(e:AppEvent):void
{
trace("View Manager: initViews");
_screens = new Array(new Start(_model), new TakePhoto(_model));
dispatchEvent(new ViewEvent(ViewEvent.VIEW_READY));
}
private function changeLanguage(e:AppEvent):void
{
for each (var screen:AbstractScreen in _screens)
{
screen.updateLanguage();
}
}
}
If my model dispatches a CHANGED_LANGUAGE event, the text in the views gets updated, But I also get a trace of "WARNING: need to override public function updateLanguage()" What could I be doing wrong?
You are right, this should not call the base class function. Are you sure there is no call to
super.initLanguage()
within your override?
Most IDE's add this call in the function body automatically, if they create an override for you.
EDIT
From your edit I see your are iterating over two objects of the types Start and TakePhoto. I assume TakePhoto is also derived from AbstractScreen and the trace may be comming from this one.
Also I suggest to use the abstract base class in your iteration.
for each (var screen:AbstractScreen in _screens)
{
screen.updateLanguage();
}
I've created an Event Handler/Listener like so:
import flash.events.Event;
public class DanielEvent extends Event {
public var data:*;
public static const APP_STARTED:String = "APP_STARTED";
public function DanielEvent(n:String, data:*){
this.data = data;
super(n)
}
}
Listening to an event using:
addEventListener(DanielEvent.APP_STARTED, appStarted);
Dispatching an event by:
dispatchEvent(new DanielEvent("APP_STARTED", "test"))
And receiving the data by:
private function appStarted(e:Event){
trace(e.data)
}
But I get the error:
Access of possibly undefined property
data through a reference with static
type flash.events:Event.
You have to use your custom event type in the event handler, if you want to access the data property:
private function appStarted(e:DanielEvent): void {
trace(e.data);
}
your event handler is passed a DanielEvent, not an Event:
private function appStarted(e:DanielEvent):void
{
trace(e.data);
}
also. you should also use your constant for your dispatch instead of passing a string, like you've done for your listener:
dispatchEvent(new DanielEvent(DanielEvent.APP_STARTED, "test"));
and don't forget to override clone() if you are planning on dispatching that event more than once.
public override function clone():Event
{
return new DanielEvent(n, data);
}