magento can not load my custom model - magento

Hi I have a issue which I don't understand, please someone could help me.
I'm on the 1.8.1
I have a Module in app->code->local->Mycompany
Mycampany
TestModel
etc
config.xml
Model
FirstModel.php
code in config.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_TestModel>
<version>0.1.0</version>
</Mycompany_TestModel>
</modules>
<global>
<models>
<TestModel>
<class>Mycompany_TestModel_Model</class>
</TestModel>
</models>
</global>
</config>
code in FirstModel.php
class Mycompany_TestModel_Model_FirstModel extends Mage_Core_Model_Abstract {
public function output()
{
echo "get";
}
}
When I use
Mage::getModel('TestModel/FirstModel')
magento can not load the class.
I tested and it is working on my local machine.
Please help.
Plus. I also tried:
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_TestModel>
<version>0.1.0</version>
</Mycompany_TestModel>
</modules>
<global>
<models>
<testmodel> /* instead of <TestModel> */
<class>Mycompany_TestModel_Model</class>
</testmodel>
</models>
</global>
</config>
I still can not get anything in
Mage::getModel('testmodel/FirstModel')
Thanks very much.

As you use magento fatory method [Mage::getModel('testmodel/FirstModel') ]
that means you need to use it procedure to define an model.
As per as magento, when autoloader execute testmodel/FirstModel run like:
testmodel=Mycompany_TestModel_Model
and
FirsModel is wrong ,File and name should be Firstmodel as after model model folder( Mycompany/TestModel/Model) all folders and files name should first letter with uppercase case and after that all should be lower a
That means file should be
Firstmodel.php instead of FirstModel
and testmodel/FirstModel should be testmodel/firstmodel
NO underscore should be use in folders and file name

Related

Magento - Override/extend Ebizmarts_SagePaySuite_Model_Api_Payment model class

I am trying to override/extend the Ebizmarts SagePay Suite Ebizmarts_SagePaySuite_Model_Api_Payment model class
My module looks like this:
etc/modules/Sulman_ModifySagePay.xml
<?xml version="1.0"?>
<config>
<modules>
<Sulman_ModifySagePay>
<active>true</active>
<codePool>local</codePool>
<version>1.0</version>
<depends>
<Ebizmarts_SagePaySuite />
</depends>
</Sulman_ModifySagePay>
</modules>
</config>
app/code/local/Sulman/ModifySagePay/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Sulman_ModifySagePay>
<version>0.0.1</version>
</Sulman_ModifySagePay>
</modules>
<global>
<models>
<sagepaysuite>
<rewrite>
<api_payment>Sulman_ModifySagePay_Model_Api_Payment</api_payment>
</rewrite>
</sagepaysuite>
</models>
</global>
</config>
app/code/local/Sulman/ModifySagePay/Model/Api/Payment.php
<?php
class Sulman_ModifySagePay_Model_Api_Payment extends Ebizmarts_SagePaySuite_Model_Api_Payment {
public function _construct(){
Mage::log('in my construct()', null, 'Sulman.log');
}
public function requestPost($url, $data, $returnRaw = false) {
Mage::log('in my requestPost()', null, 'Sulman.log');
}
}
Nothing gets logged and this gives me errors on the checkout page because:
When I check to see if it is instantiated it returns false (if I disable my module it returns the Ebizmarts_SagePaySuite_Model_Api_Payment object):
var_dump(Mage::getModel('sagepaysuite/api_payment'));
Other calls to this model throughout the site also fail.
I'm a little confused as it seems my rewrite is working correctly but it can't seem to find my new model class.
Anyone have any ideas where I'm going wrong?
Thanks
In your app/code/local/Sulman/ModifySagePay/etc/config.xml in the <models></models> section you need to define where your models are located:
<models>
<modifysagepay>
<class>Sulman_ModifySagePay_Model</class>
<modifysagepay>
</models>

How to rewrite a Magento Block with an extension?

I want to rewrite the Mage_Catalog_Block_Product_View block.
I created this file: config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<JB_CustomPrice>
<version>0.1.0</version>
</JB_CustomPrice>
</modules>
<global>
<blocks>
<customprice>
<class>JB_CustomPrice_Block</class>
</customprice>
<catalog>
<rewrite>
<product_view>JB_CustomPrice_Block_Catalog_Product_View</product_view>
</rewrite>
</catalog>
</blocks>
</config>
Then I created the block in
/app/code/local/JB/CustomPrice/Block/Catalog/Product/View.php
<?php
class JB_CustomPrice_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View
{
public function getJsonConfig()
{
return 'test';
}
}
?>
Though, the product view doesn't change, even not when I call the getProduct() method. What's going wrong?
The global error was a typo, solved by using Mage::log(get_class($this)), that fixed it. The problem was that another plugin extended Mage_Catalog_Block_Product_View already. It was an old Yoast_MetaRobots plugin, which is deprecated. I disabled it and everything was fixed. Thanks for the answer!

Mage::getModel is returning false

I am trying to get to grips with calling and using models in magento.
My current task is to simply display a string held in a model by calling it from a controller.
When trying to call SimpleOutput.php I get an error message saying that a non object has been called. I have var_dumped it as you will see and it return false.
I have looked at my code and with my limited understanding of what I need to do in Magento I have everything correct. Obviously i'm missing something. Could someone please take a look and if it's a typo tell where to look and if it's more than a simple spelling mistake explain what ive missed and what I should have done and why?
My code is below
Ts/Firstmodule/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Ts_Firstmodule>
<version>0.1.0</version>
</Ts_Firstmodule>
</modules>
<models>
<firstmodule>
<class>Ts_Firstmodule_Model</class>
</firstmodule>
</models>
<frontend>
<routers>
<firstmodule>
<use>standard</use>
<args>
<module>Ts_Firstmodule</module>
<frontName>firstmodule</frontName>
</args>
</firstmodule>
</routers>
</frontend>
</config>
Ts/Firstmodule/controllers/indexController.php
class Ts_Firstmodule_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$simple = Mage::getModel('ts_firstmodule/simpleoutput');
var_dump($simple);
}
}
Ts/Firstmodule/model/simpleoutput.php
class Ts_Firstmodule_Model_SimpleOutput extends Mage_Core_Model_Abstract
{
public function basicText()
{
echo 'this is some text from the simple output model inside the basic text function';
}
}
You must wrap <models> in <global>, just like this :
<global>
<models>
<firstmodule>
<class>Ts_Firstmodule_Model</class>
</firstmodule>
</models>
</global>
Don't hesitate to take a look at the source of the simpler core modules (eg, GoogleAnalytics), to see how they're done and understand the logic behind it.
Like always :
Mage::getModel('ts_firstmodule/simpleoutput');
When you do a getModel / getBlock / helper / etc
The first part of the parameter string is the XML node of the layer definied in the config.xml
The second part is the full path to your file from the layer folder container.
So, in your case : Mage::getModel('firstmodule/simpleoutput'); should load Ts/Firstmodule/Model/Simpleoutput.php
Note : be carefull of the case of your resources (take a look a standard magento for good practices) !
You should modify the config.xml file and add a <global> tag around your <models> tag:
<global>
<models>
<firstmodule>
<class>Ts_Firstmodule_Model</class>
</firstmodule>
</models>
<global>
After this, in order to instantiate a model use it like this:
Mage::getModel('firstmodule/simpleoutput')
The first part of the getModel method (up until /) should be the tag name you set in config.xml right under <models> tag. In your case firstmodule.

Magento: Rewrite Block is not working

I try to rewrite core file from magento.
Somehow it does not overwrite the code. I try to overwrite the function getProduct().
Tipfix/Block/Product/View.php
<?php
class WP_Tipfix_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View
{
public function getProduct()
{
if (!Mage::registry('product') && $this->getProductId()) {
$product = Mage::getModel('catalog/product')->load($this->getProductId());
Mage::register('product', $product);
}
//return Mage::registry('product');
}
}
Tipfix/etc/config.xml
<blocks>
<WP_Tipfix>
<class>WP_Tipfix_Block</class>
</WP_Tipfix>
<catalog>
<rewrite>
<product_view>WP_Tipfix_Block_Catalog_Product_View</product_view>
</rewrite>
</catalog>
</blocks>
I have know idea what i'm doing wrong.
Gr.
Lex
Your class is WP_Tipfix_Block_Catalog_Product_View which means it must be in the folder WP/Tipfix/Block/Catalog/Product/View.php. You must either move your Product directory into a new directory called Catalog in that place or rename your class (both the class and in the XML) to WP_Tipfix_Block_Product_View. I recommend moving the file.
Please change the config.xml content of your module to this, and I'm sure that it should work:-
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<WP_Tipfix>
<version>1.0.0</version>
</WP_Tipfix>
</modules>
<global>
<blocks>
<wptipfix>
<class>WP_Tipfix_Block</class>
</wptipfix>
<catalog>
<rewrite>
<product_view>WP_Tipfix_Block_Catalog_Product_View</product_view>
</rewrite>
</catalog>
</blocks>
</global>
</config>
Hope it helps.
UPDATE:-
After Ben's comment, I feel that I should have mentioned that the OP must also use the solution as mentioned by Max in his answer. So the OP will need a combined effort to fix his problem.

where can i declare my custom functions in magento

I want to declare some php functions and i would like to call those functions in various places in magento.Usually in my core php projects i'm declaring php functions in functions.php and i include that file in all pages.I'm not familiar with MVC structure.So where can i declare these kind of functions.
Thanks
Edit :-
Mango_Myfunc.xml (app/etc/modules)
<?xml version="1.0"?>
<config>
<modules>
<Mango_Myfunc>
<active>true</active>
<codePool>local</codePool>
</Mango_Myfunc>
</modules>
</config>
Config.xml(app/code/local/Mango/Myfunc/etc/configure.xml)
<?xml version="1.0"?>
<config>
<modules>
<Mango_Myfunc>
<version>0.1.0</version>
</Mango_Myfunc>
</modules>
<global>
<helpers>
<Myfunc>
<class>Mango_Myfunc_Helper</class>
</Myfunc>
</helpers>
</global>
</config>
Data.php(app/code/local/Mango/Myfunc/helper/Data.php)
class Mango_Myfunc_Helper_Data extends Mage_Core_Helper_Abstract
{
public function short_str ($str, $len, $suf = '...') {
if (strlen($str) > $len)
return substr($str, 0, $len - strlen($suf) ) . $suf;
return $str;
}
}
This is what i added
i used bellow one to call the function in list.phtml
echo $this->helper('Myfunc/Data')->short_str("test","3"); got the error
Fatal error: Class 'Mage_Myfunc_Helper_Data' not found
Magento has helper classes for those kind of methods. So make your extensions and add your methods and you can then later call them like follows
Mage::helper('yourextension/yourhelper')->yourMethod();
Or you can make a library class out of your common methods.

Resources