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.
The first model we need is Offer.php (notice the name is in singular and the first letter is capitalized)
<?php class Offer extends AppModel { public $belongsTo=array('User', 'Category'); } ?>
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.
<?php class Order extends AppModel { public $belongsTo=array("Offer", "User"); var $scaffold="admin"; }
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.
<?php App::uses('SimplePasswordHasher', 'Controller/Component/Auth'); class User extends AppModel { public $validate=array( 'username'=>array( 'alphaNumeric'=>array( 'rule'=>'alphaNumeric', 'required'=>true, 'message'=>'Your username cannot be empty and must contain only letters and numbers.' ), 'between'=>array( 'rule'=>array('lengthBetween', 5, 15), 'message'=>'Your username must be between 5 to 15 characters' ) ), 'password'=>array( 'rule'=>array('minLength', '6'), 'message'=>'Your password should contain at least 6 characters.' ), 'full_name'=>array( 'rule'=>array('minLength', '5'), 'required'=>true, 'message'=>'Your full name cannot be less than 5 characters long.' ), 'address'=>array( 'rule'=>array('minLength', '5'), 'required'=>true, 'message'=>'Your address cannot be less than 5 characters long.' ) ); public function beforeSave($options=array()) { if (!empty($this->data[$this->alias]['password'])) { $passwordHasher=new SimplePasswordHasher(array('hashType'=>'sha256')); $this->data[$this->alias]['password'] =$passwordHasher->hash( $this->data[$this->alias]['password'] ); } } } ?>
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
Tutorial Categories: