Automatic Download Models Kohana 3

I try to use the model, but I get a fatal error, so I assume that it will not automatically load.

ErrorException [Fatal error]: Class 'Properties_Model' not found

Controller Violation Line:

$properties = new Properties_Model; 

Model:

 class Properties_Model extends Model { public function __construct() { parent::__construct(); } } 

I also placed the class in three different places, hoping that it would work, and all this failed. They are: Application / classes / model Application / model application / models

What am I missing?

+4
source share
2 answers

And, I received this question by e-mail directly to me (through my contact form on the site)!

Here is what I answered (in the interests of other people who may run into this problem).

The correct location of the model with the property name

 application/classes/model/properties.php 

and the class definition will be the same follows

 class Model_Properties extends Model { } 

Think of the underscore as a directory separator. That is, if you replaced the underscore, a / you will have: "model / properties", which will be your file under application/classes .

To load the model from the controller, you can use the new standard PHP operator or do what I prefer that

 $propertiesModel = Model::factory('Properties'); 

I am not 100% why I prefer this way ... but it works for me :)

+6
source

Firstly, Kohana 3 fileyestem does not work like Kohana 2!

In K2, the autoloader looks for the class name, which looks for the class in different folders based on the class suffix.

In K3, class names are "converted" to file paths, replacing underscores with a slash.

i.e. Class Properties_Model becomes classes/properties/model.php

As you can see, using the Model suffix in this new system will not help to group your models, so basically you add β€œModel” to the class name, rather than suffixing it:

Model_Property is in classes/model/property.php

For more information see Kohana 3 userguide

+2
source

Source: https://habr.com/ru/post/1310612/


All Articles