I want to get the a variable of a magento model. In my app/code/community/Project/Module/Model/Module.php, I declare a variable like that :
public var = "image_predefined";
And I want to get this variable from app/design/frontend/product/default/template/product/extension/image.phtml
I can get the model name with mage::getModel($model) where $model contains my model. But I can't get the attribute directly. I tried with ->var or with getAttribute() but it doesn't work.
How should I do to get the variable please ?
You could build your own "getter" for this (depending on context).
Within your model, create the following:
class Namespace_Module_Model_MyModel
{
...
var $image = "image_predefined";
function getImage()
{
return $this->image;
}
...
}
Then you can call your function Mage::getModel($model)->getImage().
Magento will automoatically provide you functionality out of the box (if you're extending the base class) by using magic methods. You just need to make sure you extend the base model provided.
assuming a property called 'my_var' you can use two methods:
$model->getData('my_var');
or (using camel case)
$model->getMyVar();
Related
Is there any possible way to lazy load a custom attribute on a Laravel model without loading it every time by using the appends property? I am looking for something akin to way that you can lazy load Eloquent relationships.
For instance, given this accessor method on a model:
public function getFooAttribute(){
return 'bar';
}
I would love to be able to do something like this:
$model = MyModel::all();
$model->loadAttribute('foo');
This question is not the same thing as Add a custom attribute to a Laravel / Eloquent model on load? because that wants to load a custom attribute on every model load - I am looking to lazy load the attribute only when specified.
I suppose I could assign a property to the model instance with the same name as the custom attribute, but this has the performance downside of calling the accessor method twice, might have unintended side effects if that accessor affects class properties, and just feels dirty.
$model = MyModel::all();
$model->foo = $model->foo;
Does anyone have a better way of handling this?
Is this for serialization? You could use the append() method on the Model instance:
$model = MyModel::all();
$model->append('foo');
The append method can also take an array as a parameter.
Something like this should work...
public function loadAttribute($name) {
$method = sprintf('get%sAttribute', ucwords($name));
$this->attributes[$name] = $this->$method();
}
I am a newbie in PHP.
I have
$input = readline("\nEnter object:");
to read the name of the object that the user wants the details of, to variable $input, which by default becomes a string.
I need to check if there exists an object named $input, in any of the classes that i have defined, and call functions accordingly,like
$this->getObsDetails() if it belongs to class Obstacles,
else
$this->getCharaDetails() if it belongs to class Characters.
I cannot use is_object($input) or get_class($input) function as $input is a string.
How can I efficiently achieve this?
Edit: I have a GameObjects class, which is the base class for Characters and Obstacles classes.
I have a public function in GameObjects
public function printDetails() {
/*
based on the class of the calling object, which I intend to find
here itself using get_class($this), I intend to call respective functions as */
$this->getCharaDetails();
// OR
$this->getObsDetails();
}
Now, outside the class using is_object($$input) I know if there exists an object named $input. But $$input->printDetails() or $input->printDetails() cannot be performed.
How do I do it?
If I understand you correctly, you are trying to instantiate classes given as inputs and run specified method of the class depending on the class name.
What I would do is make a common method called run and call whatever needed, inside..
As for example:
<?php
class Cylinder{
public function run(){
return $this->getVolume();
}
protected function getVolume(){
// your stuffs here
}
}
class Circle(){
public function run(){
return $this->getArea();
}
protected function getArea(){
// your stuffs here
}
}
Then, I would check if the user inputted class exists.. and call run method if it does..
<?php
$userInput = readline("\nEnter object:");
if(class_exists($userInput)){
$obj = new $userInput();
$neededData = $obj->run();
}
Is it what you are looking for? If not, then explain your problem in a clearer fashion..
Edit: if you are using namespace, then check this thread too.
To check if class exists just use
http://php.net/manual/en/function.class-exists.php
To check if is object just variable variables
http://php.net/manual/en/language.variables.variable.php
is_object($$input);
How can I call all of my models attributes including the ones defined as accessors/mutators?
At the moment I am creating accessors/mutators like;
public function setSatisfiedWithQualityOfCourseAttribute($value)
{
$this->attributes['group_answers']['satisfied_with_quality_of_course'] = $value;
}
public function getSatisfiedWithQualityOfCourseAttribute()
{
if (isset($this->group_answers['satisfied_with_quality_of_course'])) {
return $this->group_answers['satisfied_with_quality_of_course'];
}
return null;
}
And I can call them from my controller with something like $response->satisfied_with_quality_of_course but I need to be able to return all of them without explicitly calling them one at a time. Can I do that?
I need to call all "real" attributes and all "accessors".
You can use $response->attributesToArray() to get an array of all attributes, including your custom accessors.
You can use $response->toArray() to get loaded relationships as well, with their attributes.
If your custom accessors define new pseudo attributes (as opposed to overwriting existing database columns) you should also add them to the $appends array.
protected $appends = ['satisfied_with_quality_of_course'];
I have a class, in this class I have a method that sets my array.
How can I get values of this array and use them from another method in same class?
This my class:
class HomeController extends Controller
{
private $tmp = array();
public function setValues(){
array_push($this->tmp,"blue","yellow");
print_r(array_values($this->tmp)); // It works well, I can see values.
}
public function getValues(){
print_r(array_values($this->tmp)); // It doesn't work - shows empty array.
// return $this->tmp also doesn't work - shows empty array.
}
}
How can I get values of this array?
I am not sure how controllers work in Laravel. However, if it is similar to other frameworks then the Controller is not a singleton. A new controller is potentially created for each request. This means that you are calling setValues(); on one instance of the class and getValues(); on another.
I am pretty sure you can configure Laravel to treat the Controller a singleton.
http://laravel.com/docs/5.0/container Shows how you can register your controller as a singleton if you need to.
It works! You need to call setter first, and then getter:
$o = new HomeController();
$o->setValues();
$o->getValues();
Check yourself again, please: https://ideone.com/UYAfxe
I m using zend.
I want to define the below code outside the controller class & access in different Actions.
$user = new Zend_Session_Namespace('user');
$logInArray = array();
$logInArray['userId'] = $user->userid;
$logInArray['orgId'] = $user->authOrgId;
class VerifierController extends SystemadminController
{
public function indexAction()
{
// action body
print_r($logInArray);
}
}
But it does not print this array in index function on the other hand it show this array outside the class.
How it is possible.
Thanks.
To access a global variable from inside a method/function, you have to declare it as global, inside the method/function :
class VerifierController extends SystemadminController
{
public function indexAction()
{
global $logInArray;
// action body
print_r($logInArray);
}
}
In the manual, see the section about Variable scope.
Still, note that using global variables is not quite a good practice : in this case, your class is not independant anymore : it relies on the presence, and correct definition, of an external variable -- which is bad.
Maybe a solution would be to :
pass that variable as a parameter to the method ?
or pass it to the constructor of your class, and store it in a property ?
or add a method that will receive that variable, and store it in a property, if you cannot change the constructor ?
print_r($GLOBALS['logInArray']);
http://php.net/manual/en/reserved.variables.globals.php
You can store the user in many ways and access it in more clean manner. You can store it in Zend_Registry and then use Zend_Registry::get('user') where you need to retrieve the user. You can also store it as a parameter of request object, and then in a controller simply do $user = $this->_getParam('user');
If you need access to the user array in many controllers that inherit from the SystemadminController, what you can do is store it as a protected property of the SystemadminController (eg. protected $_user). Then all you need to do in child controllers is access $this->_user.