February 10, 2015 9:36 am

How to Build a Marketplace with CakePHP 2 & Foundation 2/6

After we have set up the associations of our models, when we fetch a particular table row from our offers table we would also get that offer’s category and the user that posted that offer (as an example of why we need to set up the associations). We would also be able to access the tables that the model that our current controller is using has associations with from within that controller without any additional settings. Furthermore, we make scaffolding (the automatic CakePHP admin panel) possible with almost no additional coding when we set up our models properly.

How to Build a marketplace with CakePHP 2 & Foundation 2-6

The first model we need is Offer.php (notice the name is in singular and the first letter is capitalized)

All of your models must extend the AppModel, while all of your controllers must extend AppController (generally speaking).

There are different relationships/associations between models such as hasOne, hasMany, belongsTo, hasAndBelongsToMany. They are expressed as public variables named after the relationship and an array with the tables that the particular model(table) has that relationship.

See http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html if you do not understand this very well.

The next model targets the orders table.

We specify that an order belongs to a particular user and a particular offer.

var $scaffold=”admin”; is how we enable the admin panel only for users whose role is set to “admin”
Now, the most lengthy model is that of the users.

The $validate property is an array with the table columns as elements. Each table column is specified some rules that are checked whenever we work with that table. For example, this validation will be performed when we try to register a new account for the Market. If some of the validation fields do not pass – the data would not be saved and the validation messages will be displayed.

We check if the username contains only letters and numbers, whether it is between 5 and 15 characters and check whether the other fields have some specified minimum length.

The beforeSave() method is called before we save data to that particular model. In our case, we hash the password with SimplePasswordHasher before we save the data into the database and only if there is a password to hash.

Part 1: How to Build a Marketplace with CakePHP 2 & Foundation 1/6

Part 2: How to Build a Marketplace with CakePHP 2 & Foundation 2/6

Part 3: How to Build a Marketplace with CakePHP 2 & Foundation 3/6

Part 4: How to Build a Marketplace with CakePHP 2 & Foundation 4/6

Part 5: How to Build a Marketplace with CakePHP 2 & Foundation 5/6

Part 6: How to Build a Marketplace with CakePHP 2 & Foundation 6/6

Marketplace with CakePHP 2 & Foundation Demo & Download

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories: