Skip to content
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

Model Examples #1

Open
aligoren opened this issue May 9, 2019 · 1 comment
Open

Model Examples #1

aligoren opened this issue May 9, 2019 · 1 comment
Labels
enhancement New feature or request

Comments

@aligoren
Copy link

aligoren commented May 9, 2019

https://www.slimmvc.com/docs/basics/model

I read model documentation using the above URL.

I think models shouldn't contain functions.

Instead of this;

use SlimFacades\Database;

	Class sampleModel {

		private $c;

		public function __construct($container) {
			$this->c = $container;
			Database::connectTo('default');
		}

		public function getSampleData($params = []) {
			return Database::table('sampleTable')
			  ->where('id', '=', $params["id"])
			  ->toArray();
		}

	}

/* path: ~app/models/sampleModel.php */

This example could be good.

use SlimFacades\Database;

	Class sampleModel {

		public $id;
                public $userName;
                public $password;
                // etc

	}

/* path: ~app/models/sampleModel.php */

Data classes are should be in a different layer. You can populate models in the controller layer. There is a pseudo code in the below code;

// list type model
List sampleModel {
   public $id;
   public $userName;
}

// foo controller

// get data from data layer
$data = $sampleData->getSampleData();

// init an array as sampleModel
var $sampleModel = new List<sampleModel>();

// iterate data and populate the array you initialized.
foreach($data as $modelData) {
   sampleModel.Add(new sampleModel 
   {
      $id => $modelData->id,
      $userName => $modelData->userName;
   });
}

// response
echo json_encode($sampleModel);
@gurkanbicer gurkanbicer added the enhancement New feature or request label May 10, 2019
@gurkanbicer
Copy link
Owner

Hi Ali,

Thanks for your valuable suggestions. When I was preparing SlimMVC, I thought the models as a data layer without solid rules. Your idea's more closest to OOP logic. But, the Slim hasn't a CLI like the Laravel Framework and the model layers not limited to single dependency. If I prepare your ideas in SlimMVC, I must write a CLI and I must find a dependency that supports SQL and NoSQL database platforms.

Therefore, I labeled this issue as an enhancement.

Thanks for your interest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants