I am following this tutorial
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics.
As per this tutorial,when i pass a value with url like(http://localhost/dev/weblog/index/testModel/id/1), I am getting error like this
Fatal error: Call to a member function load() on a non-object in app\code\local\Srivats\Weblog\controllers\IndexController.php on line 10
This is my index controller code
<?php
class Srivats_Weblog_IndexController extends Mage_Core_Controller_Front_Action
{
public function testModelAction()
{
$params = $this->getRequest()->getParams();
$blogpost = Mage::getModel('weblog/blogpost');
echo("Loading the blogpost with an ID of ".$params['id']);
$blogpost->load($params['id']);
$data = $blogpost->getData();
var_dump($data);
}
}
Blogpost.php file
<?php
class Srivats_Weblog_Model_Mysql4_Blogpost extends Mage_Core_Model_Mysql4_Abstract
{
protected function _construct()
{
$this->_init('weblog/blogpost','blogpost_id');
}
}
Here is my config file
<?xml version="1.0" encoding="utf-8"?>
<config>
<global>
<modules>
<srivats_weblog>
<version>1.0.0</version>
</srivats_weblog>
</modules>
<models>
<weblog_mysql4>
<class>Srivats_Weblog_Model_Mysql4</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog_mysql4>
</models>
</global>
<frontend>
<routers>
<weblog>
<use>standard</use>
<args>
<module>Srivats_Weblog</module>
<frontName>weblog</frontName>
</args>
</weblog>
</routers>
</frontend>
</config>
system.log shows
Warning: include() [function.include]:
Failed opening 'Mage\Weblog\Model\Blogpost.php' for inclusion
(include_path='app\code\local;E:\dev\app\code\community;app\code\core;E:\dev\lib;.;C:\php\pear')
in lib\Varien\Autoload.php on line 93
Here is the link to all files https://gist.github.com/cf99e4277599954f38d4
I think this line $blogpost->load($params['id']); causing trouble.What i am missing.Any pointers?
Ahh in you config you should have
<global>
<models>
<weblog>
<class>Srivats_Weblog_Model</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog>
</model>
</global>
you already have for mysql4 resource add this on top of that. You should have
<models>
<weblog>
<class>Srivats_Weblog_Model</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog>
<weblog_mysql4>
<class>Srivats_Weblog_Model_Mysql4</class>
</weblog_mysql4>
</models>
Your real problem is that the line:
$blogpost = Mage::getModel('weblog/blogpost');
Is not returning an object and so $blogpost is just NULL.
Is the blogpost model class file actually created in app/code/local/Srivats/Weblog/Model/Blogpost.php
Related
I am trying to override the admin Sales Totals block (Mage_Adminhtml_Block_Sales_Totals) situated under "/app/code/core/Mage/Adminhtml/Block/Sales/Totals.php"
I declare my module like this:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<CompanyName_Adminhtml>
<version>0.1.0</version>
</CompanyName_Adminhtml>
</modules>
<global>
<blocks>
<companyname_adminhtml>
<class>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</class>
</companyname_adminhtml>
<adminhtml>
<rewrite>
<sales_totals>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</sales_totals>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
My module class path is like this:
local/CompanyName_Adminhtml/Block/Adminhtml/Sales/Totals.php
and My class code start like this:
class CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals extends Mage_Adminhtml_Block_Sales_Totals {
// block methods goes here..
}
Unfortunately the class override don't works and I don't get any errors in the log files.
I am using Magento Version 1.7.0.2
In the magento backoffice I can see my module as enabled.
Any help?
Try Overriding the individual Totals.php block which is at the location Mage/AdminHtml/Block/Sales/Order/ file rather than the main Totals.php under the Mage/AdminHtml/Block/Sales/ folder.
It worked for me. As i wanted to show one more row in the order-totals block.
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<CompanyName_Adminhtml>
<version>0.1.0</version>
</CompanyName_Adminhtml>
</modules>
<global>
<blocks>
<companyName_adminhtml>
<class>CompanyName_Adminhtml_Block</class>
</companyName_adminhtml>
<companyname_adminhtml>
<class>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</class>
</companyname_adminhtml>
<adminhtml>
<rewrite>
<sales_order_totals>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</sales_totals>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
then class
class CompanyName_Adminhtml_Block_Adminhtml_Sales_Order_Totalsextends Mage_Adminhtml_Block_Sales_Order_Totals {
protected function _initTotals() {
parent::_initTotals();
$this->_totals['foo_total'] = new Varien_Object(array(
'code' => 'purchasewrapping_total',
'value' => $this->getSource()->getFooTotal(),
'base_value'=> $this->getSource()->getBaseFooTotal(),
'label' => $this->helper('sales')->__('Foo'),
));
return $this;
}
}`enter code here`
Mage_Adminhtml_Block_Sales_Order_Totals is extends at Mage_Adminhtml_Block_Sales_Totals
and here you can extend and write your changes
Attempting to override the Cms/controllers/IndexController.
This is the directory structure of my module -
Local/MyCompany/FourOhFourExp/controllers/IndexController.php
Local/MyCompany/FourOhFourExp/etc/config.xml
The code for my IndexController is
<?php
require_once 'Mage'. DS .'Cms'. DS .'controllers'. DS .'IndexController.php';
class MyCompany_FourOhFourExp_IndexController extends Mage_Cms_IndexController
{
public function noRouteAction($coreRoute = null){
Mage::log('First Function');
header("Location: new_route.html");
die();
}
public function defaultNoRouteAction()
{
Mage::log('Second function!');
header("Location: new_route.html");
die();
}
}
?>
and this is the code for my config.xml file
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyCompany_FourOhFourExp>
<version>0.1.0</version>
</MyCompany_FourOhFourExp>
</modules>
<frontend>
<routers>
<cms>
<args>
<modules>
<MyCompany_FourOhFourExp before="Mage_Cms">MyCompany_FourOhFourExp</MyCompany_FourOhFourExp>
</modules>
</args>
</cms>
</routers>
<events>
<controller_action_noroute>
<observers>
<cms>
<class>cms/observer</class>
<method>noRoute</method>
</cms>
</observers>
</controller_action_noroute>
</frontend>
</config>
Hoping someone can explain why this isn't overriding. I've looked through numerous tutorials and blog posts but have yet to figure out why it doesn't work.
There are lot of issue:
1)Modulepath:
As your controller class is MyCompany_FourOhFourExp_IndexController then path of class should be
app/code/local/MyCompany/FourOhFourExp
Local/MyCompany/controllers/IndexController
Local/MyCompany/etc/config.xml
change to
Local/MyCompany/FourOhFourExp/controllers/IndexController/php
Local/MyCompany/FourOhFourExp/etc/config.xml
2)Mage cms controller donot add properly
require_on Mage::getModuleDir('controllers', 'Mage_Cms'). DS .'IndexController.php';
What need to do:
add to app/code/local/Mage/Cms/controllers/IndexController.php , Mage::dispatchEvent('cms_controller_action_noroute', array('action'=>$this));
in your custom module, catch the Event
i try to overridde
app\code\core\Mage\Customer\Model\Resource\Customer.php
with this xml:
<models>
<customer_resource>
<rewrite>
<customer>My_Company_Model_Resource_Customer</customer>
</rewrite>
</customer_resource>
</models>
but i get
Mage registry key "_resource_singleton/customer/customer" already exists
and with
<customer>
<rewrite>
<customer_resource>My_Company_Model_Resource_Customer</customer_resource>
</rewrite>
</customer>
or
<models>
<customer>
<rewrite>
<resource_customer>My_Company_Model_Resource_Customer</resource_customer>
</rewrite>
</customer>
</models>
nothing happens
You should be able to accomplish it with the code you had. If it is not being overwritten try some of the basics like making sure you've cleared your cache. If that still doesn't work, you might be fighting with another module that is already overriding it. In that case you'd need to figure out the next steps in figuring out which one wins: merge logic, rewrite/extend the other one, etc.
<models>
<customer_resource>
<rewrite>
<customer>My_Company_Model_Resource_Customer</customer>
</rewrite>
</customer_resource>
</models>
I did it (in 1.7.0.2) with the following xml:
<?xml version="1.0" encoding="UTF-8" ?>
<config>
<modules>
<Final_Dummy>
<version>0.0.1</version>
</Final_Dummy>
</modules>
<frontend>
<routers>
<dummy>
<use>standard</use>
<args>
<module>Final_Dummy</module>
<frontName>dummy</frontName>
</args>
</dummy>
</routers>
</frontend>
<global>
<helpers>
<dummy>
<class>Final_Dummy_Helper</class>
</dummy>
</helpers>
<models>
<dummy>
<class>Final_Dummy_Model</class>
<resourceModel>dummy_resource</resourceModel>
</dummy>
<dummy_rsource>
<class>Final_Dummy_Model_Resource</class>
<deprecatedNode>dummy_mysql4</deprecatedNode>
</dummy_rsource>
<customer_resource>
<rewrite>
<customer>Final_Dummy_Model_Resource_Customer</customer>
</rewrite>
</customer_resource>
</models>
</global>
</config>
and in order to make sure that it really works I created the following class:
<?php
class Final_Dummy_Model_Resource_Customer extends Mage_Customer_Model_Resource_Customer{
public function amIHere(){
return 'Yes sir';
}
}
?>
and called the resource in my module's controller like this
<?php
class Final_Dummy_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
$res=Mage::getResourceModel('customer/customer');
die($res->amIHere());
}
}
?>
and the result was:
Yes sir
I hope these code help you in some way. remember to clear the cache and I do recommend that in development phase, disable Magento compiler.
I would like to do the above.
Ive overridden many files in the past...block, model, helper....but this one eludes me.
Can anyone see what im doing wrong here:
(ive edited this code...to include some of the recomendations now...)
Heres my folder structure (2 controller locations as a test):
/Idigital/Idgeneral/etc/config.xml
/Idigital/Idgeneral/controllers/Checkout/CartController.php
/Idigital/Idgeneral/controllers/CartController.php
Heres my config.xml:
<?xml version="1.0"?>
<config>
<modules>
<idigital_idgeneral>
<version>0.1.0</version>
</idigital_idgeneral>
</modules>
<global>
<blocks>
<idgeneral><class>Idigital_Idgeneral_Block</class></idgeneral>
</blocks>
</global>
<frontend>
<routers>
<checkout>
<use>standard</use>
<args>
<modules>
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
</modules>
</args>
</checkout>
</routers>
<layout>
<updates>
<idgeneral>
<file>idigital.xml</file>
</idgeneral>
</updates>
</layout>
</frontend>
</config>
Ihave placed my controller file in 2 locations to test.
And heres the top of my FIRST controller file:
require_once 'Mage/Checkout/controllers/CartController.php';
class Idigital_Idgeneral_Checkout_CartController extends Mage_Checkout_CartController
{
public function testAction()
{
var_dump('inside checkout/cart/test');exit;
}
/**
* Add product to shopping cart action
*/
public function addAction()
{
blah...
}
Ans my second controller:
require_once 'Mage/Checkout/controllers/CartController.php';
class Idigital_Idgeneral_CartController extends Mage_Checkout_CartController
{
public function testAction()
{
var_dump('inside cart/test');exit;
}
/**
* Add product to shopping cart action
*/
public function addAction()
{
blah...
}
When i visit: /checkout/cart/add
Im directed to the mage controller...not my local. (i have var_dump stmts in each..so i can see which is ran).
When i visit /checkout/cart/test - i get a 404
When i visit /cart/add or cart/test - i get a 404
when i visit idgeneral/cart/test or idgeneral/cart/add - i get a 404
Create your module folders and files
app/code/local/MyNameSpace/MyModule/etc/config.xml
app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
app/etc/modules/MyNameSpace_All.xml
Edit /etc/config.xml and Create app/code/local/MyNameSpace/MyModule/etc/config.xml with the following content:
<?xml version="1.0"?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<global>
<!-- This rewrite rule could be added to the database instead -->
<rewrite>
<!-- This is an identifier for your rewrite that should be unique -->
<!-- THIS IS THE CLASSNAME IN YOUR OWN CONTROLLER -->
<mynamespace_mymodule_checkout_cart>
<from><![CDATA[#^/checkout/cart/#]]></from>
<!--
- mymodule matches the router frontname below
- checkout_cart matches the path to your controller
Considering the router below, "/mymodule/checkout_cart/" will be
"translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
-->
<to>/mymodule/checkout_cart/</to>
</mynamespace_mymodule_checkout_cart>
</rewrite>
</global>
<!--
If you want to overload an admin controller this tag should be <admin> instead,
or <adminhtml> if youre overloading such stuff (?)
-->
<frontend>
<routers>
<mynamespace_mymodule>
<!-- should be set to "admin" when overloading admin stuff (?) -->
<use>standard</use>
<args>
<module>MyNameSpace_MyModule</module>
<!-- This is used when "catching" the rewrite above -->
<frontName>mymodule</frontName>
</args>
</mynamespace_mymodule>
</routers>
</frontend>
Note: The above didn’t work for me when I override catalog/product controller. I had to use:
<from><![CDATA[#^catalog/product/#]]></from>
<to>mymodule/mycontroller</to>
(notice the missing leading slash)
Since Magento 1.3 you can simply add your module to the frontend router. Rewrites are not neccessary any more:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule</MyNameSpace_MyModule>
</modules>
</args>
</checkout>
</routers>
</frontend>
Please note that before=”Mage_Checkout” will load your controller first if available and fall back to Magento’s if not.
If the controller is in another folder, you’ll have to modify the code:
app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php.
Replace
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule</MyNameSpace_MyModule>
with
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule_Checkout</MyNameSpace_MyModule>
Edit 'controllers/Checkout/CartController.php'
Create app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php with the following content: (the only change to indexAction() is adding an error_log() call):
<?php
# Controllers are not autoloaded so we will have to do it manually:
require_once 'Mage/Checkout/controllers/CartController.php';
class MyNameSpace_MyModule_Checkout_CartController extends
Mage_Checkout_CartController
{
# Overloaded indexAction
public function indexAction() {
# Just to make sure
error_log('Yes, I did it!');
parent::indexAction();
}
}
Edit 'app/etc/modules/MyNameSpace_All.xml'
(This is to activate your module)
true
local
Edit 'app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml' and add the following to use the same update handle as before:
<mynamespace_mymodule_checkout_cart_index>
<update handle="checkout_cart_index"/>
</mynamespace_mymodule_checkout_cart_index>
(Note that these tags seem to be case sensitive. Try using all lowercase if this isn’t working for you)
[by Hendy: When I override catalog/product/view using the method described in this Wiki or here, I didn’t have to do the above. However, when using the 'cms way', I had to update the handle manually.]
The above item did not work for me (2009-02-19 by Jonathan M Carvalho)
I discovered that the file to change is app/design/frontend/[myinterface]/[mytheme]/layout/mymodule.xml
Add the following lines:
Point your browser to /checkout/cart/
In your PHP error log, you should find ‘Yes, I did it!’.
You need to be extra precise with the rewrite regular expression because errors are very hard to find.
<Idigital_Idgeneral before="Mage_Checkout">Idgeneral_Checkout</Idigital_Idgeneral>
Should be
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
or try moving your custom controller up to
../Idigital/Idgeneral/controllers/CartController.php
and use
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral</Idigital_Idgeneral>
There is also an error in your <modules> tag location. It should be:
<config>
<modules>
<idigital_idgeneral>
<version>0.1.0</version>
</idigital_idgeneral>
</modules>
<global>
...
</global>
<frontend>
....
</frontend>
...
</config>
i.e <modules> shouldn't be inside <global>
Here's a good tutorial on how to dump the config tree that Magento sees as XML: http://alanstorm.com/magento_config
I'm leaving this here for the next poor developer forced to work with this jalopy. Much of the instructions here are pasted from the magento docs which like its source, is a twisted labyrinth of misdirection. Okay enough complaints...
This worked for me in version 1.8
Create your namespace and module:
/app/code/local/MyNameSpace/MyModule
Create your module config:
/app/code/local/MyNameSpace/MyModule/etc/config.xml
<?xml version="1.0" ?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule_Checkout</MyNameSpace_MyModule>
</modules>
</args>
</checkout>
</routers>
</frontend>
Create your controller:
/app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
<?php
require_once Mage::getModuleDir('controllers', 'Mage_Checkout').DS.'CartController.php';
class MyNameSpace_MyModule_Checkout_CartController extends Mage_Checkout_CartController
{
public function indexAction() {
// /var/log/debug.log should log the output
Mage::log('cart index override', null, 'debug.log');
// Call the parent class
parent::indexAction();
}
}
Enable your new module:
/app/etc/modules/MyNameSpace_All.xml
<?xml version="1.0" ?>
<config>
<modules>
<MyNameSpace_MyModule>
<active>true</active>
<codePool>local</codePool>
</MyNameSpace_MyModule>
</modules>
</config>
That's all thats needed. Now go celebrate, you just polished a turd! ;)
This is a little notification on the include path of the controller.
This include path can cause errors if the Magento Compiler mode is turned on.
require_once 'Mage/Checkout/controllers/CartController.php';
Instead of that it is good to use
require_once Mage::getModuleDir('controllers', 'Mage_Checkout').DS.'CartController.php';
It will be safer.
Well...it WONT override the checkout cart controller.
So i used the URL REWRITE in the PRODUCT VIEW...from this link...near the bottom of the article. They say its a proper method...
http://www.excellencemagentoblog.com/magento-add-product-to-cart-ajax
if(!url){
url = jQuery('#product_addtocart_form').attr('action');
}
url = url.replace("checkout/cart","idgeneral/cart");
That worked for me. Allows me to get cracking. Basically calls MY controller..instead of checkout controller.
Thanks for the help ROSCIUS...appreciated.
I also had to change my config....my routers section now looks like this:
<routers>
<!-- THIS PART REGISTERS OUR CONTROLLERS FOLDER FOR USE -->
<idgeneral>
<use>standard</use>
<args>
<module>Idigital_Idgeneral</module>
<frontName>idgeneral</frontName>
</args>
</idgeneral>
<!-- THIS PART WONT WORK TO OVERWRITE OUR MAGE CONTROLLER -->
<checkout>
<use>standard</use>
<args>
<modules>
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
</modules>
</args>
</checkout>
</routers>
I'm building a Controller and when I call an Action I get a 404 Error (everything worked last week!). I'm calling the action using:
http://www.website.com/route/controllername/actionname
eg:
http://www.website.com/abcdef/test/echo
I have investigated the problem (using Mage::log('..')) and found out that the controller is being loaded. The controller's name is fine too. Further investigation revealed that the noRouteAction within my controller is being called.
ie my controller contains:
public function noRouteAction() {
echo "noRoute Action";
}
public function echoAction() {
echo "Echo Action";
}
When I call http://www.website.com/abcdef/test/echo I get "noRoute Action".
Why isn't echoAction being called?
Edit:
The config.xml
<?xml version="1.0"?>
<config>
<modules>
<Abcdef_Web>
<version>1.0.0</version>
</Abcdef_Web>
</modules>
<global>
<helpers>
<abcdef>
<class>Abcdef_Web_Helper</class>
</abcdef>
</helpers>
<blocks>
<abcdef>
<class>Abcdef_Web_Block</class>
</abcdef>
</blocks>
</global>
<frontend>
<routers>
<abcdef>
<use>standard</use>
<args>
<module>Abcdef_Web</module>
<frontName>abcdef</frontName>
</args>
</abcdef>
</routers>
<layout>
<updates>
<abcdef>
<file>abcdef.xml</file>
</abcdef>
</updates>
</layout>
</frontend>
</config>
The controller class file: (TestController.php)
<?php
class Abcdef_Web_TestController extends Mage_Core_Controller_Front_Action {
public function noRouteAction() {
echo "noRoute Action";
}
public function echoAction() {
echo "Echo Action";
}
}
?>
OK found the problem!!!!
It seems that "Add Store Code to Urls" was set to yes, so Magento was expecting:
http://www.website.com/storecode/abcdef/test/echo
rather then:
http://www.website.com/abcdef/test/echo
For those with the same problem. This can be disabled through:
System / Configuration / General / Web / Url Options / Add Store Code to Urls
Note:
I do not remember setting this option to Yes. I've seen some reports that this setting could be changing automatically (4 people reporting it):
http://www.magentocommerce.com/boards/viewthread/192613/