I want to make clear something before i proceed with my project to prevent bad code practice. I developed in ruby on rail before and i want to know is it can make it the same like in PHP because im starting learning but i don't want to use the framework like Zend,Yii,etc
For me, model is the place where data is insert to the database.
controller is the place that find all the parameters and use the function in model to process the data into database. Besides, it also route or redirect when the procedure is return success or failure.
view is the place that get the instant variable that initialize in controller ,and use it to render the data in proper format.
So,here the question for me in PHP. I would like to create CRUD in php.I walked through many of the article or posts(in stackoverflow& others),but i got confused that some of the people saying different kind of things.
In my mind, model is the place that only related to data insertion/query to database. So,every query for my create,show,update,delete, i will put in my model. here is my example for create_user function in my model.
function create_user($firstName, $lastName,$username, $password){
$query = "INSERT INTO `users` (`user_id`, `first_name`, `last_name`, `email`, `username`, `password`, `created_at`, `handphone_no`, `street_address`, `city`, `state`, `country`, `postal`, `birthday`, `company_id`)
VALUES (NULL, ?, ?, NULL, ?, ?, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 2)";
if($stmt = $this->conn->prepare($query)) {
$stmt->bind_param('ssss', $firstName, $lastName,$username, $password);
$stmt->execute();
}
}
So,my FIRST question: in my model, am i suppose to an object and pass to controller?? or just return true or false to indicate the success or failure of the process.
in my user controller,
class Users_controller {
private $user;
function __construct(){
$this->user = New User();
}
function create($firstName, $lastName,$username, $password){
$this->user->create_user($firstName, $lastName,$username, $password);
}
}
my SECOND question: for my CRUD, in rails, it will either find the id of the object and show/edit it or create/destroy a new model. So, my controller here have to return an object for me to render data in view? and how am i suppose to connect my form to the controller. From my form to pass the params to the controller.
so,right now Im thinking so far of that only, any guidance and clear example for me to understand more because Im new to php. Thanks you...