How can I call model in a custom module? Lets assume that model is really simple one and returns just arrays of static data.
I have a directory structure similar to this:
app
local
Mypackage
Module
Models
Model1.php
How can I include Model1.php inside controller? If I go with Mage::getModel('Mypackage/Modul/Model1) it returns error since it searches model inside Mage/Module/Model/Model1.php
Thanks!
Mage::getModel('mypackage_module/modeltest) should work. But first check your config.xml.
You should have declared it like so:
<models>
<mypackage_module>
<class>Mypackage_Module_Model</class>
<resourceModel>modeltest_mysql4</resourceModel>
</mypackage_module>
<modeltest_mysql4>
<class>Mypackage_Module_Model_Mysql4</class>
<entities>
<modeltest>
<table>mypackage_module</table>
</modeltest>
</entities>
</modeltest_mysql4>
</models>
Mage::getModel('modulename/modelname')->modelmethod();
i.e.
Mage::getModel('catalog/product')->getName();
Hope this help you
This should work:
Mage::getModel('module/model_model1')->modelmethod();
Please note that the model folder should be "Model" not "Models"
Hello you can try this but I have not tested it
$collection = Mage::getModel('Module/Model1')->getCollection();
Related
I have a custom module and everything works fine. I'm adding and admin panel portion to the module and would like a separate admin helper. I know I can create and call my admin helper like this:
app/code/local/namespace/module/helper/Admin.php
class Namespace_Module_Helper_Admin extends Mage_Core_Helper_Abstract....
$helper = Mage::helper('namespace_module/admin');
And everything works great.
I was really wanting the structure for my admin helper to be something like this:
app/code/local/namespace/module/helper/admin/Data.php
But can't figure out the how to set that up in config.xml and then call the helper.
My initial thought was to setup the config like this:
...
<helper>
<namespace_module>
<class>Namespace_Module_Helper</class>
</namespace_module>
<namespace_module_admin>
<class>Namespace_Module_Admin_Helper</class>
</namespace_module_admin>
</helper>
...
Then call the helper like this:
$helper = Mage::helper('namespace_module_admin');
But this doesn't work.
Is it possible to have a second helper for my module in a helper directory child directory? If so could someone point me in the right direction.
Thanks for the help!
Yes, it is possible to do this but I think you have some typos. I was able to register a new helper using your approach with settings like this:
<helpers>
<namespace_module>
<class>Namespace_Module_Helper</class>
</namespace_module>
<namespace_module_admin>
<class>Namespace_Module_Helper_Admin</class>
</namespace_module_admin>
</helpers>
The helper file itself was at the path: app/code/local/Namespace/Module/Helper/Admin/Data.php
The helper class looks like this:
class Namespace_Module_Helper_Admin_Data extends Mage_Core_Helper_Abstract
{
public function test()
{
return 'test';
}
}
And I was able to invoke it with the syntax:
Mage::helper('namespace_module_admin')->test();
So it is possible that your issue is due to your file/class name not matching up with the location Magento’s autoloader was expecting. For example your <class>Namespace_Module_Admin_Helper</class> should map to the (improper) directory app/code/local/Namespace/Module/Admin/Helper rather than the expected app/code/local/Namespace/Module/Helper/Admin.
Your approach looks fine and absolutely right. You have only one mistake in config.xml. You should name node <helpers> instead of <helper>
I have had a request to change the url of a module.
For example let's say the url is www.example.com/world
I need the url to be www.example.com/hello/world
I know that within the modules config xml you can change the node to whatever you like (provided it's unique) but does not allow prepend /hello/
<frontend>
<routers>
<anexample>
<use>standard</use>
<args>
<module>An_Example</module>
<frontName>hello/world</frontName>
</args>
</anexample>
</routers>
</frontend>
Does anyone have any idea how this can be done ? Some example code or even a point in the right direction would be appreciated!
Sorry if this isn't detailed enough, i'll be happy to post more of my code if required.
Thanks in advance.
You can add rewrites into Magento's URL Rewrite Manager. Haven't tested following code. But hope it helps.
Mage::getModel('core/url_rewrite')
->setIsSystem(0)
->setStoreId($storeId)
->setOptions('RP')
->setIdPath('index.php?cat=c' . $categoryId . '_' . $this->strip($data['name']) . '.html')
->setTargetPath($categoryModel->getUrlPath() . '.html')// Put the actual path
->setRequestPath('index.php?cat=c' . $categoryId . '_' . $this->strip($data['name']) . '.html') // put the path you want to display
->save();
here i can give you idea to reach your solution it will sure help you
<?xml version="1.0"?>
<config>
<global>
<rewrite>
<some_unique_name_to_identify_this_rewrite>
<from><![CDATA[#^/from_this/#]]></from>
<to>/to_this/</to>
</some_unique_name_to_identify_this_rewrite>
</rewrite>
</global>
</config>
And if you want to do url rewrite with Admin functionality you can go throw this link
Also what you want to achieve could be done with Custom Url Rewrite. For example:
request path: hello/world
target path: 'world'
In this way you're making a destination of system rewrite yours start point.
hope this will sure help you
This was pretty simple in the end. Instead of going to the hassle of creating a rewrite for it i just changed my frontname to 'hello' then created a controller called 'worldController'
Then in my indexController i just put a redirect to */world
Please see the following article by Allan Storm about the Magento rewrite
http://alanstorm.com/magento_dispatch_rewrites_intro
Access custom Module controller path as custom HTML Url Link
Say http://domain.com/index.php/customwallpaper/index/create/
will be rewrited to
http://domain.com/index.php/fotomural-personalizado.html
Insert below data in core_url_rewrite table for url rewrite
Mage::getModel('core/url_rewrite')
->setStoreId(1)
->setIdPath('customwallpaper')
->setRequestPath('fotomural-personalizado.html')
->setTargetPath('customwallpaper/index/create') // Put the actual Controller path
->setIsSystem(1)
->save();
-OR-
Run below sql query
INSERT INTO `core_url_rewrite`
(`url_rewrite_id`, `store_id`, `id_path`, `request_path`, `target_path`, `is_system`, `options`, `description`, `category_id`, `product_id`)
VALUES ('7', '1', 'customwallpaper', 'fotomural-personalizado.html', 'customwallpaper/index/create', '1', NULL, NULL, NULL, NULL);
I have used this module creator to get an custom attribute (which takes the type 'file').
http://www.silksoftware.com/magento-module-creator/
This works and can be seen in the admin area. I also have other custom attributes for customers which I have used this tutorial to create:
http://www.fontis.com.au/blog/magento/know-more-about-your-customers-adding-custom-signup-attributes
This also works. The reason I used the module creator was because I was unsure of how to make the input type as 'file'.
The attributes created through the fontis tutorial can be displayed as needed on the front end (which was only needed in the registration form).
The problem I'm having is in the custom area in the logged in accounts area on the front end. What I need is to retrieve the value of the 'file' attributes which was created in the module creator. Could anyone point me in the right direction of how to display these please? I have tried getAttributeName but this is not working.
Thank you.
If you post the code from your custom module we could help you more.
Meanwhile, here is some info that could help you:
Whenever you store something in the DB using a module, there is a Model class (that allows you to access the necessary data)
You can find the class name by looking in your modules etc/config.xml file
In the file look for section named <models>
The sub nodes of <models> is the name of the namespace (see below)
The sub node of your 'namespace' called <class> contains the rest of the info you will need
Next you need to call the Model with Mage::getModel('namespace/class_name')->load($id); to get a collection of all the custom attribute records that are in the system
To break this down in to manageable pieces:
Let's assume this is what your config.xml contains:
<models>
<customattribute> // this is your namespace
<class>Mycompany_Customattribute_Model</class> //this tells you wher to find your model files
<resourceModel>customattribute_resource</resourceModel>
</customattribute>
...
</models>
This means that your 'namespace' is 'customattribute'.
Next you need to find the file that contains your Model.
In this case we look at the <class> node to give us the file location (in this case app/code/local/Mycompany/Customattrbute/Model), now we need to go there and see the file that is there (let's say it's called 'File.php')
To get all the data we call the follwoing function:
Mage::getModel('customattribute/file')->load();
This will give us all the data.
If we want to narrow it down we can use the following function:
Mage::getResourceModel('customattribute/file')->addFieldToFilter('name_of_filed_in_db', 'value_we_want');
Let's say I use a custom controller to have a url path/frontend name of
/customcategory
Well, obviously if I have a controller file named 'TestController.php' and indexAction
the url path would be
/customcategory/test/index
What I am trying to figure out is how I do re-name the Test Controller, or modify the config xml file, so I can have a hyphenated url from a controller file such as
/customcategory/test-section/index
I know that if I want /customcategory to be hyphenated, I can just modify the frontend tag in the config file. But the site I am building would benefit from a hyphenated controller route, the part that comes after /customcategory with keywords and I cannot get it to work nor can I find an example on google - as crazy as that may seem.
Thanks for your time.
What you are trying to do is possible using global rewrite in your custom module. You could pass all incoming request for /customcategory/* to a specific controller action. But you would have to manage your own route (base on the depth of your url path).
e.g www.MageIgniter.com/customcategory/path1/path2
config.xml
<global>
<rewrite>
<fancy_url>
<from><![CDATA[/customcategory\/(.*)/]]></from>
<to><![CDATA[customcategory/index/processroute/tagname/$1/]]></to>
<complete>1</complete>
</fancy_url>
<rewrite>
</global>
<frontend>
<routers>
<tagseo>
<use>standard</use>
<args>
<frontName>customcategory</frontName>
</args>
</tagseo>
</routers>
class MageIgniter_Customcategory_IndexController extends Mage_Core_Controller_Front_Action
{
public function processRoute(){
print_r($requestUri = Mage::app()->getRequest()->getRequestUri()); //path1/path2
print_r($this->getRequest()->getParam('tagname')); // path1
print_r($this->getRequest())
// do you custom logic here base on about request path explode('/', trim($requestUri,'/'))
}
...
For a working example see "Product Tags" section # http://www.contempospace.com/bedroom-furniture/wardrobe-closets/custom-closet-systems/isa-closet-system-shelves-hanging-walk-in-reach-in-closet.html
As far as I am aware you cannot add hypens in the url to match up to a filename. If you are trying to get a folder structure you can just add more paths to it.
For example if you wanted:
Namespace/CustomCategory/controller/test/SectionController.php
you could do:
/customcategory/test_section/index
Think of a small and basic affiliate system. I want an URL like
www.myshop.com/mynewproduct.html?afid=123
Every time afid is found in the URL, a method should be called (basically to save "afid" in the session and when the customer buys stuff, I want to track it).
You don't need a router for this. You'll want to setup an event listener that fires for every page load, and then access the variables in the request collection. The controller_front_init_routers event should do.
So, setup your module's config with the following
<global>
<events>
<controller_front_init_routers>
<observers>
<packagename_modulename_observer>
<type>singleton</type>
<class>Packagename_Modulename_Model_Observer</class>
<method>interceptMethod</method>
</packagename_modulename_observer>
</observers>
</controller_front_init_routers>
</events>
</global>
And then create the following class
app/code/local/Packagename/Modulename/Model/Observer.php
class Packagename_Modulename_Model_Observer {
public function interceptMethod($observer) {
$request = $observer->getEvent()->getData('front')->getRequest();
$afid = $request->afid;
//do whatever you want with your variable here
}
}
The interceptMethod can be named whatever you want.
I know this is a very old answer, but it is valid to mention we shouldn't use the controller_front_init_routers event if we intend to store those parameters in session, which is the scenario for the original question. For example, if you instantiate customer/session at this point you won't be able to perform a customer login anymore. Alan pointed this himself in http://alanstorm.com/magento_sessions_early. BTW, thanks Alan for this great article.