Table Of Contentphp|architect’s Guide to
Programming Magento
byMarkKimsal
php|architect’sGuidetoProgrammingMagento
ContentsCopyright©2007-2008MarkKimsal–AllRightsReserved
Bookandcoverlayout,designandtextCopyright©2004-2008MarcoTabini&Associates,Inc.–AllRightsReserved
FirstEdition:May2008
ISBN:978-0-9738621-7-1
ProducedinCanada
PrintedintheUnitedStates
Nopartofthisbookmaybereproduced,storedinaretrievalsystem,ortransmittedinanyformor
bymeanswithoutthepriorwrittenpermissionofthepublisher,excetinthecaseofbriefquotations
embeddedincriticalreviewsorarticles.
Disclaimer
Althougheveryefforthasbeenmadeinthepreparationofthisbooktoensuretheaccuracyofthe
informationcontainedtherein, thisbookisprovided”as-is”andthepublisher, theauthor(s), their
distributorsandretailers,aswellasallaffiliated,relatedorsubsidiarypartiestakenoresponsibility
foranyinaccuracyandanyandalldamagescaused,eitherdirectlyorindirectly,bytheuseofsuch
information. Wehaveendeavouredtoproperlyprovidetrademarkinformationonallcompaniesand
productsmentionedinthebookbytheappropriateuseofcapitals.However,wecannotguaranteethe
accuracyofsuchinformation.
MarcoTabini&Associates,TheMTAlogo,php|architect,thephp|architectlogo,NanoBookandthe
NanoBooklogoaretrademarksorregisteredtrademarksofMarcoTabini&Associates,Inc.
Writtenby MarkKimsal
Publishedby MarcoTabini&Associates,Inc.
28BombayAve.
Toronto,ONM3H1B7
Canada
(416)630-6202/(877)630-6202
info@phparch.com/www.phparch.com
Publisher MarcoTabini
TechnicalReviewer PaulReinheimer
LayoutandDesign ArbiArzoumani
ManagingEditor ElizabethNaramore
FinanceandResourceManagement EmanuelaCorso
Contents
Chapter1—Introduction 1
WhoCanUseThisBook?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
StoreOwners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
AboutThisBook. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
CodeFormatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
XMLExamples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
OperatingSystemConsiderations . . . . . . . . . . . . . . . . . . . . . . . . . 4
Chapter2—DevelopingforMagento 7
PrepYourEnvironment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
LAMP/WAMPPlatform. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Subversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
MySQLTools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
DatabaseSetup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
MagentoSetup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
InitializingSubversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Chapter3—ExploringMagento 13
MagentoCatalogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
AttributeSystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
vi ” CONTENTS
MultipleStores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
MultipleCatalogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
MultipleDesigns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Chapter4—MagentoModules 27
ModuleStructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
CodePools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
ModulePackages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Configfiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
TemplateSystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
LayoutFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
TemplateFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
MageApplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Magento’sRequestCycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Chapter5—DatabaseDesign 43
ResourcesandDatabaseConnections . . . . . . . . . . . . . . . . . . . . . . . 43
MasterSlaveSetup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
ModelsandResourceModels . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
EAVDesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
EAVversusNormalization . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
DefaultEntities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
DefiningEntities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
SavingEntities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
EntityAttributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
AttributeValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
CONTENTS ” vii
Chapter6—CustomModules 57
TheFilesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
TheShellModule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
DefaultDirectories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
DefaultFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
ModuleRequirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
PlanofAttack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
ConfigurationFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Config.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
TemplateChanges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
LayoutChanges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
OverriddingaBlock. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Chapter7—CMSIntegration 75
CMSDrivenApproach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
TechnicalConsiderations . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
TheUserConnectModule . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
EntityAttributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
DealingwithLegacyAttributes . . . . . . . . . . . . . . . . . . . . . . . . 85
Registration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
SynchronizingSessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
MagentoListeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
CMSSessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
SharedThemes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
StoreDrivenIntegration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Chapter8—MadetoOrderModule 97
ExampleScenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
PlanofAttack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
TheLengthAttribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
NewModule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
viii ” CONTENTS
OverridingtheBlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
RecordingUserInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
AddingDatatotheQuote . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
AddingDatatotheOrder . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
ShowCustomizationstotheCustomer . . . . . . . . . . . . . . . . . . . . . . 108
ModifytheCartPage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
SeparatingSimilarProductsintheCart . . . . . . . . . . . . . . . . . . . 109
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Chapter9—PointsandRewardsModule 113
Planofattack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
AddingNewAttributestoProducts. . . . . . . . . . . . . . . . . . . . . . . . . 114
CreatingaShellModule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
RecordingPointsOrdered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
MakingaModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
EventListeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
ShowtheCustomerTheirPoints . . . . . . . . . . . . . . . . . . . . . . . . . . 124
DashboardLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
DynamicCoupons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
CouponModels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
DeductingPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Chapter10—BackendIntegration 135
StartingaNewModule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
TheController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
CRUDController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
ReadAction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
UpdateAction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
DeleteAction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
CreateAction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
SecuringtheController . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
ClientAccess. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
CONTENTS ” ix
Chapter11—QuickAnswerstoCommonQuestions 151
Magento’snamingconventionsarecrazy! . . . . . . . . . . . . . . . . . . . . . 151
HowdoIrunarawqueryagainstthedatabase? . . . . . . . . . . . . . . . . . 152
HowdoIturnoffthepriceinthelayerednavigation? . . . . . . . . . . . . . . 153
HowdoImovetheadminpaneltoanewnameforsecurity? . . . . . . . . . 154
HowdoIuseinstallationandupgradefilesinmycustommodules? . . . . . 155
HowdoIrunMagentocodewithoutbuildingamodule? . . . . . . . . . . . . 157
HowdoIshowtherootcatalogcategoryonthehomepage?. . . . . . . . . . 158
HowdoIhidethepriceofproductsbeforetheygointothecartorifaperson
isnotloggedin? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
HowdoIfindoutthepropertablename? . . . . . . . . . . . . . . . . . . . . . 161
HowdoIshowMagentoproductsonanon-Magentopage? . . . . . . . . . . 162
Help,myproductchangesdon’tshowuponthefrontend! . . . . . . . . . . . 163
Igiveup!Magentoistooconfusing . . . . . . . . . . . . . . . . . . . . . . . . 164
Index 167
Description:php|architect's Guide to Programming Magento . Chapter 5 — Database Design
.. If you are not a programmer, but simply want to run a Magento store, this