-
Notifications
You must be signed in to change notification settings - Fork 481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[USEFUL! :)] Multiple Models #195
Comments
Yey awesome, I vouch for this. 👍 |
This is very exciting, I look forward to testing it. |
Hello, We can have both single, and multiple model. I have use this exemple for multiple model, and make the older script has $this->utils for shared functions. I can post a exemple if needed ! |
This one is what I am looking for.. :) Love you guys.. |
@coyarzun2013 I have implemented this technique in my project. I actually thought this was implemented in previous versions of the project. I see that this issues has not been closed. I would like to submit changes for this if you are no longer working on the changes. However, I would make a few changes. The loadModel() method does not need to be so verbose. I would suggest something like this:
As a general suggestion, I would space out your concatenated strings such as: Also, I would suggest keeping the model class and extending it to classes using it. So the model would simply be:
This way you can include other methods that you will share across all other models extending the base class. In order to show how this works l would create a new class, SongsModel, to show the method examples such as getAllSongs, etc. Let me know what you think. |
great work but why only a single model? yes there previous version had multiple models. |
Hi @mynumbercrunching , because thats the most simple case! when this project had multiple model-classes people were asking to merge everything to one class, now it has one class and people ask to split it to multiple classes. :) it's always a little bit tricky when software is designed by a community. Personally I think it's okay to stay with one class for now as it's the most simple solution possible and it will work for most users who only need <10 database calls. I you need multiple models, then please have a look at the above solution. By the way, this script does not use Slim, it's 100% pure native PHP. :) |
ahahahaha ! In my case, i have made a shared model classes, && a multiple classe for each controller ! ;) |
@panique Can you tell me what was the commit where this project was still wearing multiple models? |
@jaonoctus I'm not sure what commit had the multiple models but it's not that much different. You just need to change the controller class, this way everything else will still be up to date. In the controller class:
|
Thank you @skrecker 😄 |
@skrecker If i try to load 2 models that extends Model, the application returns the error:
The right way would be this:
|
@jaonoctus You're right. I've been using only one model in my controllers. Thank you |
@coyarzun2013 @skrecker @mynumbercrunching @sanjayrup @betweenbrain |
Using this code, I seem to have problems with MINI's default 'Home' model class being loaded, as there is already a controller with the class name 'Home'.
I need to change the loadModel() function to the following:
And then ensure I name my model class 'HomeModel', and the file 'homeModel.php'. |
Good point! This will be fixed with MINI3, where a model is named xxxModel and a controller xxxController! Thanks for the report & code! |
Hi everyone:
First of all, this is an awesome tiny php script, really great work.
For those who want to use this script with multiple Models, i´ll give you some little changes in the controller.php file
the new constructor function look like this
function __construct()
{
//getting the className of the Controller who extends controller class
$class = get_called_class();
$this->openDatabaseConnection();
//passing this classname as parameter to loadModel Function
$this->loadModel($class);
}
then in loadModel Function
/**
* Loads the "model".
* @return object model
*/
public function loadModel($class=null)
{
(string)$modelFile = strtolower($class);
$instanceClass = $class.'Model';
require APP . 'model/'.$modelFile.'.php';
// create new "model" (and pass the database connection)
$this->model = new $instanceClass($this->db);
}
and finally, declare your model Class name as 'nameModel' for example homeModel, songsModel, etc. and thats it!!!
The text was updated successfully, but these errors were encountered: