Accessing other model file within another model file - php

I am trying to create an class inside the model directory. This class(eg:- Admin) exposes only the methods which makes sense to the controller.
The Admin class will do all the joins and stuffs internally on tables(using ORM) and prepares data which can be readily consumed by the controller.
I have created 15 files in the model directory each of them representing a table in my database using the ORM method.
Now I want to create to create an instance of table within the Admin classes' get_All() method. I have tried to use Kohana::factory() which was unavailable in my Admin class. I tried to create the instance using the 'new', but it ended in an error which says that the specified class is not found.
My class definition for Admin is as follows
<?php defined('SYSPATH') or die('No direct script access.');
class Model_Admin {
public function get_All()
{
$PD = new Model_PayPalData;
echo 'Success';
}
}
The error is:
ErrorException [ Fatal Error ]: Class 'Model_PayPalData' not found
APPPATH/classes/Model/admin.php
Please advice on how to deal with this situation.
Thanks for your attention

Seem like your class is not loaded. You can check this by viewing the declared classes
get_declared_classes();
If it is't there, make sure to include it, or add it to the Models directory, if needed without extending the ORM class.

Related

how to use another controller function without extends in our controller

how to use another controller function without extends in our controller
$this->load->library('../controllers/controllername');
already used
it is giving error =
Unable to locate the specified class: Session.php
Well you are not supposed to do that. If your controller uses repeatable logic, you should make class (Service for example), put the re-usable logic into it and call it in your controllers.
You can't use another controller function inside the controller. You can archive this in these two ways.
Create a Helper class
Create a generic model.

php calling a function through with autoloader

These are my classes
class DB{
// returns the instance as it should - all system works well
public function create(){
// creates a row in the db....
}
}
class User{
// does all it does....
}
I have an autoloader that loads the classes and the system works well
$user = new User();
$user->create(....)
I want to use the method create from the DB while instantiating User class, how can I do so?
the thing that I want to prevent is not having to need to create a function "update" in every class which needs to be updated
For example:
Class user - update "users table"
class kids - update "kids table"
instead
// from DB class
update($table_name, $field)
The autoloader is responsible for finding the and including the files that contain the class definitions so you don't have to explicitly call include 'db.php'; etc. , but it isn't going to be able to do what you're trying to do here.
If the create() method is defined in the DB class, there's no reason for it to be accessible through an instance of a different class like User, and unless you specifically do something to allow it, the autoloader won't help with that.
The only way for you to access the DB::create() method from an instance of User is for User to extend DB.
class User extends DB { ...
This means that all User objects will also be instances of DB and have access to all of the public and protected methods of DB, not just create().
It looks like this might be what you want. If that's the case, it would be helpful for you to read up on Object Inheritance.

How does OpenCart access its library classes?

Im current trying to learn more about the core of OpenCart and how its classes actually work. Im also trying to advance my OOP skills in general as Im still learning in that area, so perhaps theres something obvious that Im not seeing.
Im wondering how a controller file knows how to find the cart class (for example).
E.g.
In catalog/controller/checkout cart there is (obviously with code removed)
class ControllerCheckoutCart extends Controller {
public function index() {
$this->cart->update();
}
}
The Controller class can be found in system/engine/controller.php
update() can be found system/library/cart.
I assumed that in the controller.php there would be a link to the cart class, or an object made from it. (Im basing that on the use of $this->).
So how is the cart class actually found from the controller?
Thank you
Firstly, your ControllerCheckoutCart extends the Controller class, so this is the class we need to focus on. You can find this class in /system/engine/controller.php.
Inside this class, there are two magic methods we are interested in. The first is the __construct, where the "registry" class is loaded (found in /system/engine/registry.php if you're interested in picking that apart - it's very simplistic).
You can think of this as a lookup of all the classes the store uses, such as model files, library files and so on. In the construct, the registry is passed to the controller so it has a reference to it
public function __construct($registry) {
$this->registry = $registry;
}
The second and more important magic method is the __get method. This is called when a classes property doesn't exist, for you to handle it yourself if you wish to do so. OpenCart uses this to try and get the class with that key from the registry
public function __get($key) {
return $this->registry->get($key);
}
So $this->cart in any controller would try to get the object with the key cart from the registry. If you look at the index.php file you will see this is allocated in there
// Cart
$registry->set('cart', new Cart($registry));
ControllerCheckoutCart extends Controller, which means it inherits all the code in Controller which you are not seeing here. Some code in Controller, likely in Controller::__construct, is creating the $this->cart object. Example:
class Controller {
public function __construct() {
$this->cart = new Cart;
}
}
Since this constructor is inherited by all child classes, they construct their own $this->cart as well and have access to it in their own methods.
As mentioned by Jay Gilford, you need to register your newly added library class file in the index.php and/or admin/index.php (depending on if you are using it in catalog or admin)
$registry->set('yourlibraryclass', new YourLibraryClass());
so that upon system loading, Opencart knows that your class exists, then you can call all its functions by:
$this->yourlibraryfilename->function();
Please note that your library file name is normally the same as your class name, hence it is used in the example here.
After the change has been done in the index.php files, you need to logout and login again to see the changes.

How to organize a Main controller for all the child controllers in codeigniter

I want to make something like this:
Main_Controller -> {
child1_controller
child2_controller
child3_controller
}
the purpose is to execute the constructor of the Main class everytime the child classes have been executed, it looks like some kind of multi extending, for example if i want to check in the main constructor wheter is admin or not, but the problem is when i do that
child1_controller extends Main_Controller
i get an error Class 'Main_Controller' not found
Use the built in core class extending mechanism, if you name your base controller class appropriately (with the default prefix MY_) and put it under application/core CI should pick it up.
Once you got your MY_Controller set up, you can make your child controllers under application/controllers/ extend that, and call for parent::__construct() in their __construct.

Inheritance in CodeIgniter

I am building a series of forms, and I am trying to inherit the functionality of a parent Form class into all the forms. For example,
LeaveForm extends Form (Model)
LeaveFormController extends FormController
I am handling all the leave form specific stuff in LeaveFormController and LeaveForm.
In LeaveFormController constructor, I simply call the parent class constructor, then load the LeaveForm Model. And in FormController constructor, I load Form model.
My problem is, I get an error,
Cannot redeclare class form in Form.php
Have I got my architecture wrong? How do I handle this ?
check if the class has already been initialized like this:
if (!class_exists('classname'))
{
// ok fine create new instance now
}
Possibly when you $this->load->model('Form'), you manually included the models/form.php file?
In your leaveform.php model file, make sure you load the superclass model you extend using codeigniter's model loading mechanism instead of require or include. Codeigniter has a loader that keeps track of already-loaded files to avoid redeclaring classes, but you need to use $this->load to use it. It won't know about files loaded directly with include or require.
So at the top of leaveform.php, use this:
$CI =& get_instance(); $CI->load->model('Form');
This is not related, but you will have pain unless you namespace your CodeIgniter model classes the same way you namespace Controller classes.
Try using FormModel extends CI_Model {}; Instead of Form extends CI_Model {};

Categories