2

I have been reading a lot of blog from Alan strom and Magento tutorials and i have gained a lot, still blurry but good. Now i am at a point, where i need to put everything together. I am really lost on what and where to start but as for my understanding

You call a controller and that controller in turn call a Action, which runs the code. The models have database code, that talk to the database, the code in layout.xml has the code that run in conjunction with views as to where to display, what sort of block etc.

That is my understanding now, but it is very raw and vague and i want to improve it. So what i don't understand is

  1. What kind of code will controllers have?
  2. How can a controller coordinate with blocks and know where to display? such as code like this (code not to be indexed), it is totally alien code to me, how can i understand it? How does it connect to our view or block type
  3. How does the code above will have connection to the database?
  4. When speaking of database, code like this

    Magentotutorial_Weblog_Model_Resource blog_posts

What does entities mean? Any real life example. Is that all what is needed to make a connection to database, how does magento insert data into multiple table at once if that is the format.

  1. How can i connect a event with a task,
  2. How can i know the method name build into magento e.g getTitle(''); etc
  3. How do i know about to make a custom layout,

How can magento know what kind of block we want, content or structural?, what is output="toHtml", i know it renders the page and it doesn't need to be called as it auto generate, unlike other getChildHtml(); which we need to call. when we say getChildHtml('left');, can we do a custom args into the getChildHtml(); or no? If yes or not, how do i pass "my stuff" into the left or anything that i have created.

  1. How do we map data outputted from a db to a form e.g a form that has input fields and i need to populate data into it.
  2. How do i make custom menu for my own page and link to different that display different data

So i suppose my post is all about bringing the magento module together and make it work like you want to.

Edit: I can see a lot of methods in my controller, but no idea which one will run the basic homepage for that module and render which template file.

Update : What is the best resource to learn PHP which is used in magento or higher level if i may.

  • So you want to be explained what is MVC? – Julien Lachal May 3 '16 at 11:43
  • @JulienLachal i have no idea mate. Will that help, is that what i am lacking? As i got laravel tutorials. – Nofel May 3 '16 at 11:45
  • 1
    Pretty much yes, the MVC (model-view-controller) architecture pattern is what Magento uses - here seems a good place to start to learn about MVC. – zigojacko May 3 '16 at 11:47
  • @zigojacko with that how does magento render page, with specified action etc like in my question. – Nofel May 3 '16 at 11:52
  • 1
    Honestly, you'd be better off just writing extensions. Every single one of those points can be Googled for answers. Read about MVC and start developing. – musicliftsme May 3 '16 at 14:07
3
+25

What kind of code will controllers have?

The code for data input and output is in the Controller. In other words, it's ok to run validation of input parameters from the form in the controller; indication of the data type that are given (e.g. JSON) – it’s also fine. But running different operations with data inside the model – it’s not a good approach, because they should be in the model itself.

How can a controller coordinate with blocks and know where to display? such as code like this (code not to be indexed), it is totally alien code to me, how can i understand it? How does it connect to our view or block type?

The Layout (not the Controller) is responsible for displaying blocks. Hence, it is completely incorrect to write die($data) to get data. So, working with such data volume should be shifted to the separate modules or the helper (if it has something in common). You should specify in the layout how and where to display a certain block and what data (template for example) should be used. Thus, this data may be specified directly in the class block or in the controller during the work with layout.

How does the code above will have connection to the database?

Your code is communicating with database through the model, that have its own resource model with established connection. $billingAddress = Mage::getModel('customer/address') is used to call the model, but $model->save() will call resource model to save data.

What does entities mean? Any real life example. Is that all what is needed to make a connection to database, how does magento insert data into multiple table at once if that is the format.

Entitles mean a certain table (data) to use with this resource model. In fact, this is the name of the table. If you need to fill out multiple tables with data, you can realize it on the resource model level for model that you are saving or updating. For example, Mage_Catalog_Model_Resource_Product: after saving the method _afterSave is called and the method _saveWebsiteIds is called inside it to save additional data:

//Save product website relations
....
$adapter = $this->_getWriteAdapter();
....
$adapter->insertMultiple($this->_productWebsiteTable, $data);

From this part of code it is clear that another table is used. It is quite a normal practice, when you are working with difficult models like product, that has different dependencies.

How can i connect a event with a task?

Use the observer for your event. In addition, you can place events in your code to use them in future modules. Here you can read the example:

How can i know the method name build into magento e.g getTitle(''); etc

Usually, in Magento such methods are somewhat “magical”. They are: get, set and uns (unset). If in the object and its ancestors is clearly no such a method, then the method $object->getData('title') will be called. If your method looks like getSomeImportantTitle() then it will be called $object->getData('some_important_title'). To better understand how it works, I suggest using phpStorm and wonderful plugin for M1 - Magiciento (he teaches phpStorm to understand Magento). Also, it adds a number of useful features, that I never used.

How do i know about to make a custom layout, ...

It is simple to understand the layout. There are location and content. If you call getChildHtml('left’) then its too late to change something in the “left”. You should take care about this in the layout and add the required data and checks. If your block shouldn’t be displayed on the all lefts, then it should be entered not in the <default> area, but ​​only in the desired one, for example <catalog_product_view>. You should study Magento standard layouts. It’s not difficult to understand them. Note, that the layout of the customer account page, for example, can be updated in any other module. Layouts also have the ability to change the behavior of the blocks using the <action method="methodName"> – in fact this is the call of block method (that is inscribed in the action, its parent). Parameters can be transferred to actions inside. getChildHtml() can be used only in case when you want to add the contents added via layout in the specific place. In this case, any developer can add a child to your unit and its contents will also be displayed in the template.

How do we map data outputted from a db to a form e.g a form that has input fields and i need to populate data into it.

Use standard features Magento such as varienForm with validator. But the form itself should be manually drawn (written in html) by yourself in the template. There are a lot of options to work with forms in the admin panel. There is a fieldset for connecting the model and view. It has a lot of opportunities, but I can write a lot about it and it exceeds the possibilities of this answer. You may read some information here:

  1. Module Development Series – Magento Admin Module – Part4
  2. Magento admin form fieldset custom type

How do i make custom menu for my own page and link to different that display different data

The admin panel has a separate file for menu etc/system.xml. You can see the example of the same module Mage_Catalog. When you are working with menus on the front-end, you need to use the unit in this menu and its methods. Here is the example:

<block type="core/text_list" name="top.menu" as="topMenu" translate="label">

Standard menu is the block Mage_Core_Block_Text_List, that displays each child. To add something, you can assign your own child to this block in the layout and data should be displayed without problems. But in each case you need to study how it works. I hope this information would be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.