Zend: Unable to access form in controller - php

First I created a project using Zend_Tool. Then created a controller AuthenticationController in default module.
Created Form in projectfolder/application/forms/Login.php
class Forms_Login extends Zend_Form {
public function _construct() {
// add elements
}
}
Accessing Form in myproject/application/controllers/AuthenticationController.php
public function loginAction() {
$this->view->form = new Form_Login();
}
I am getting following error:
Fatal error: Class 'Application_Forms_Login' not found in /var/www/student/application/controllers/AuthenticationController.php on line 19
How can I access it with same form class name without including this file in AuthenticationController??
May be I have to tell zend in Bootstrap.php about this but I wan unable to find a sample code.
Thanks

you should declare them in your bootstrap.php file like so:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload()
{
$autoLoader=Zend_Loader_Autoloader::getInstance();
$resourceLoader=new Zend_Loader_Autoloader_Resource(array(
'basePath'=>APPLICATION_PATH,
'namespace'=>'',
'resourceTypes'=>array(
'form'=>array(
'path'=>'forms/',
'namespace'=>'Forms_'
)
)
));
$autoLoader->pushAutoloader($resourceLoader);
}
}

Related

Message: Undefined property: Material_model::$load Filename: libraries/Form_validation.php

I am a newbie to Codeigniter and trying to build a service registration page. Not just the page but the whole website has this error.
$autoload['model'] = array('User_model','Material_model');
$autoload['libraries'] = array('database','form_validation');
class services extends CI_Controller {
public function service1()
{
$this->form_validation->set_rules('type','Type',trim|required);
$this->form_validation->set_rules('area','Area',trim|required);
$this->form_validation->set_rules('quantity','Quantity',trim|required);
$this->form_validation->set_rules('details','Details',trim|required);
$this->form_validation->set_rules('name','Name',trim|required);
$this->form_validation->set_rules('number','Number',trim|required);
$this->form_validation->set_rules('location','Location',trim|required);
if($this->form_validation->run()==FALSE){
$data['main_view']="page_view";
$this->load->view('layouts/main',$data);
} else{
$this->user_model->create_user();
redirect('/');
}
I'm getting this error in user model from material model. Th
Actually, you should load the model in your parent construct.
For example:
class services extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('your_directory_model, 'your_initialize_model');
}
}
And then, if you want to use your model. you can build code like i write below in your method.
$this->your_initialize_model->your_model_method();

CI_Model not found in extending class

I can't instantiate my models manually on my controller using require_once, following is the problem:
Controller
<?php
require_once './application/models/portion/portioncreatormodel.php';
class Payment extends CI_Controller {
function sample() {
$pc = new PortionCreatorModel();
echo 'OK';
}
}
Model
<?php
class PortionCreatorModel extends CI_Model {
function __construct() {
parent::construct();
}
}
When I load this model using $this->load->model('portion/portionCreatorModel') it works perfectly (in controllers or other models), when I load using require_once using this very same require_once './application/models/portion/portioncreatormodel'; it works perfectly in other models.
Why doesn't it work properly on controllers? I found this in my error.log:
PHP Fatal error: Class 'CI_Model' not found in /var/www/html/myerp/igniter/application/models/portion/portioncreatormodel.php on line 3
Note: I already tried this require_once:
require_once APPPATH.'/models/portion/portioncreatormodel.php';
Please, don't tell me the only way to solve it is renaming PortionCreatorModel to Portioncreator_Model, it is already working properly on models, the problem is on controllers.
Any glues?
Try using this in your controller:
<?php
class Payment extends CI_Controller {
function sample() {
$this->load->model('PortionCreatorModel', 'pc');
// Reference by using $this->pc
echo 'OK';
}
}
Reference: CodeIgniter: Models

What is wrong with this CodeIgniter code?

I am trying to port an application's few functions from a CodeIgniter application to another existing CodeIgniter application. Both applications on themselves are working very well but when I added this thing it gives the following error:
Fatal error: Call to a member function order_by() on null in …\application\core\MY_Model.php on line 7
In this question I have removed parts unrelated to the error to simplify code.
//MY_Model.php model file
<?php
class MY_Model extends CI_Model {
protected $_order_by = '';
public function get(){
$this->db->order_by($this->_order_by);
}
}
//article_m.php model file
<?php
class Article_m extends MY_Model
{
protected $_order_by = 'pubdate desc, id desc';
}
//frontend.php controller file
<?php
class Frontend extends MY_Controller
{
function __construct()
{
$this->load->model('article_m');
}
function index()
{
$this->article_m->get();
}
}
Please help. Thank you!
whenever calling any $this->db ... you have to make sure to load your database library. Check in application\config\autoload.php for the following:
$autoload['libraries'] = array('database');

Extends Model in CodeIgniter

Well, I'm newbie in CodeIgniter Framework and i'm trying building a generic Model Class. See:
class Basic_Model extends CI_MODEL {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
I want to extends all Models based on Basic_Model, like this:
class Pagina_Model extends Basic_Model {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
The problem is when i try to call "pagina_model" from a Controller a got the following error:
Fatal error: Class 'Basic_Model' not found in /var/www/myproject/application/models/pagina_model.php on line 12
If i use "basic_model" in controller everything works fine.
EDIT 1:
I created a file named MY_Basic_Model.php in "/application/core" and changed the class name to "MY_Basic_Model". But i got the error:
Fatal error: Class 'MY_Basic_Model' not found in /var/www/myproject/application/models/pagina_model.php on line 12
For this you have to create Core System Classes (this is also known as Method Overriding).
Create a file MY_Model.php in application/core/ directory which will extend the base CI_Model class:
<?php
class MY_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Now you can extend this in your models (../applicaton/models/):
<?php
class Pagina_Model extends MY_Model {
function __construct()
{
parent::__construct();
}
}
Few things to note here:
1) The class declaration must extend the parent class.
2) Your new class name and filename must be prefixed with MY_ (this item is configurable).
How to configure:
To set your own sub-class prefix, open your application/config/config.php file and look for this item:
$config['subclass_prefix'] = 'MY_';
Documentation:
https://ellislab.com/codeigniter/user-guide/general/core_classes.html
You can do it this way.
lets assume you have basic_model.php inside your model folder .
Now add the code for class Basic_Model that you have written
class Basic_Model extends CI_MODEL {
function __construct() {
// Call the Model constructor
parent::__construct();
}
}
Now make a pagina_model.php inside your model folder and add the code that you written. Just include the first line like bellow
<?php
require APPPATH.'/models/basic_model.php';//just add this line and keep rest
class Pagina_Model extends Basic_Model {
function __construct()
{
parent::__construct();
}
}
hope this will solve your problem
You can do this ... MY_ model is really good but should you wish a sub model to extend a different sub model you can always do:
require(APPPATH.'models/Other_model.php');
class New_model extends Other_Model {
In my instance Other_Model actually extends MY_model.

Call function in another controller in Yii

I've created 2 controllers in my Yii application: FirstController.php and SecondController.php in default controller path.
FirstController.php:
<?php
class FirstController extends Controller {
public static function returnFunc() { return 'OK'; }
}
SecondController.php:
<?php
class SecondController extends Controller {
public function exampleFunc() {
$var = First::returnFunc();
}
}
When I try to execute exampleFunc() in SecondController, Yii throw the error:
YiiBase::include(FirstController.php) [<a href='function.YiiBase-include'>function.YiiBase-include</a>]: failed to open stream: No such file or directory
Calling FirstController::returnFunc() similarly don't work.
I'm newbee in OOP and Yii framework. What's the problem?
I've solved this problem. The autoloader doesn't load controllers.
It was in config/main.php:
'import' => array(
'application.models.*',
'application.components.*',
),
All work with this:
'import' => array(
'application.models.*',
'application.components.*',
'application.controllers.*',
),
class ServiceController extends Controller
{
public function actionIndex()
{
Yii::import('application.controllers.back.ConsolidateController'); // ConsolidateController is another controller in back controller folder
echo ConsolidateController::test(); // test is action in ConsolidateController
class ServiceController extends Controller
{
public function actionIndex()
{
Yii::import('application.controllers.back.CservicesController');
$obj =new CservicesController(); // preparing object
echo $obj->test(); exit; // calling method of CservicesController
When you create a Yii project, each of your controllers extend the Controller class, and that class extends the built in Yii class CController.
This is nice because Controller is a class within your application (it can be found in the components folder).
If you want a method to be accessible by both of your controllers, put that method in the Controller class, and since they both extend it. They'll both have access. Just make sure to declare it either public or protected.

Categories