I am trying to do a CRUD as mentioned in Jobeet tutorial(http://agiletoolkit.org/learn/tutorial/jobeet/day3). I have also added a generate.php inside page directory with the code mentioned in the link. When I try to access it via browser by http://localhost/atk4.1.2/?page=generate I am getting the following error,
Exception_ForUser
You should call parent::init() when you override it
Additional information:
- object_name: gift_project_generate
- class: page_generate
I also have added a page named crud.php with the following contents inside the page directory the contents of which are as follows,
<?php
class page_crud extends Page{
function init(){
parent::init();
$tabs=$this->add('Tabs');
$tabs->addTab('Gifts')->add('CRUD')->setModel('Gift');
}
}
The following is the Gift.php inside the Model directory,
<?php
class Model_Gift extends Model_Table {
function init(){
parent::init();
$this->addField('id');
$this->addField('name')->type('text');
$this->addField('url')->type('text');
}
}
Now when I try to access the crud page via http://localhost/atk4.1.2/?page=crud, I see the following errors,
Exception_InitError
You should define entity code for Model_Gift
C:\xampp\htdocs\atk4.1.2\atk4\lib\BaseException.php:37
But the database already has a table named gift and $this->dbConnect(); is not commented in Frontend.php.
Am I missing something here?
Add this to your model definition:
public $enity_code='gift';
This should be exactly same as the name of your table in SQL.
The other error you are getting about init() not being called is a bug: https://github.com/atk4/atk4/issues/22
Related
I am a beginner in CodeIgniter framework and I have problem with links. I found a couple of pages in which is explained how to link pages, but from some reason I have problem to link two pages. I did this:
Insert Labwork
and that should call controller_proffesor method :
function index(){
$this->load->view('proffesor_view_insert_labwork');
}
i try also this:
Insert Labwork
but when i click on that link this is what i get:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Controller_proffesor::$load
Filename: controllers/controller_proffesor.php
Line Number: 7
Backtrace:
i have also include this line in my autoload file :
$autoload['helper'] = array('url','form');
What am I doing wrong?
Within the constructor have you added:
parent::__construct();
Try adding it inside __construct(). It invokes the constructor of inherited parent class CI_Controller.
Your code should look like this..
class Controller_proffesor extends CI_Controller {
public function __construct()
{
parent::__construct();
//followed by your helper, model load statements
}
NOTE: Refer this https://stackoverflow.com/a/21339891/5176188 for more details. This may be a duplicate question.
Try this:
In your config file you must set something like this:
$autoload['helper'] = array('url','form','html');
In the controller, you must forget to extend it and the name also must concide with the name of your class and it is also like the model extends it to CI_Model
Class Class_Name extends CI_Controller {
public function __construct () {
parent::__construct
// you can load here the session and models
}
}
Hope it helps.
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.
i just created a controller like this:
<?php
class UsuarioController extends AppController {
}
?>
I went to http://urubu.zz.mu/controller/UsuarioController.php to test, and this was the result:
Error: ControllerController could not be found.
Error: Create the class ControllerController below in file: app/Controller/ControllerController.php
}
Theres no class ControllerController in my project.
I found a line in AppController(From CakePHP) :
App::uses('Controller', 'Controller');
But i dont know what it means and how to solve it.
Can anyone help? thanks
TLDR:
Read the wealth of documentation in the Online CakePHP Book to better understand CakePHP.
Accessing a Controller's index() action
In CakePHP, you access the index action of your Controller via the URL like this:
http://www.mydomain.com/usarios
You do NOT need to add "controller" to the URL.
The above would run the index() action within the UsariosController.
Accessing another action within a Controller
http://www.mydomain.com/usarios/register
This would run the register() action within the UsariosController.
Passing variables to actions within a Controller
http://www.mydomain.com/usarios/view/1
This would pass 1 to the register($userId) { action in your UsariosController.
class SomeController extends Controller
{
public function actionIndex() {
echo 'This is some controller';
}
}
class AnotherController extends SomeController
{
public function actionIndex() {
echo 'This is another controller';
}
}
This works:
index.php?r=some
but ...
index.php?r=another
says:
PHP warning
include(SomeController.php): failed to open stream: No such file or directory
Both of the files are in
test\protected\controllers\
BTW in the past I also tried using the Gii Controller Generator with "SomeController" as the base class...
It said:
The controller has been generated successfully. You may try it now.
Generating code using template
"C:\xampp\htdocs\yii\framework\gii\generators\controller\templates\default"...
generated controllers\YetAnotherController.php
generated views\yetAnother\index.php
done!
When I clicked on "try it now" it also said:
PHP warning
include(SomeController.php): failed to open stream: No such file or directory
Edit:
Classes inside protected/controllers are not autoloaded, therefore you'll have to import the parent class file before extending from it:
In AnotherController.php:
Yii::import('application.controllers.SomeController');
public class AnotherController extends SomeController {
// ...
}
Incase you need to access the base class from url also, you can use the above method. Otherwise you can put your base class inside protected/components as you have already figured out.
Yii autoloading works only when you have the same name for the file as the class that the file contains. Meaning class SomeController should be within SomeController.php file.
Make those changes and it should work.
A helpful wiki: Understanding Autoloading Helper Classes and Helper functions.
Guide link:
Class files should be named after the public class they contain.
To extend any class just go to the config file and add the class in the import section
'import' => array('application.controllers.SomeController')
this will make it available in the entire application without importing explicitly.
I have a model called Task defined like this (fields not relevent to question removed)
<?php
class Model_Task extends Model_Table {
public $entity_code='vscrum_task';
public $table_alias='tk';
function init(){
parent::init();
// debug causes error in Ajax in ATK v4.1.1
// $this->debug(true);
$this->addField('id')->system(true)->visible(false);
$this->addField('task_desc')->mandatory(true)->visible(true);
$this->addField('tasktype_id')->mandatory(true)->refModel('Model_TaskType');
$this->addField('team_id')->system(true)->visible(false);
and the refModel tasktype is defined like this (fields not relevent to question removed)
<?php
class Model_TaskType extends Model_Table {
public $entity_code='vscrum_tasktype';
public $table_alias='ty';
function init(){
parent::init();
$this->addField('id')->mandatory(true);
$this->addField('name')->mandatory(true);
$this->addField('team_id');
}
}
I have a CRUD which is based on task and is now (thanks to help from Jancha and Romans on stackoverflow) is working fine.
I want to limit the options in the drop down for TaskType to only those tasktypes defined for the user's team. I tried putting an addCondition in the TaskType Model referencing a session variable i had previously memorized
$this->addCondition('team_id',$p->api->recall('team_id'));
and also using a direct call to a value for the logged in use
$this->addCondition('team_id',$p->api->auth->get('team_id'));
but this results in showing the Tasktype fine in the Grid
but leaves it empty for both Edit and Add in the Ajax dialog.
If i remove the addCondition line from the TaskType Model, it shows all values in the list but i will always want this restricted to a subset.
As this is the referred Model and not the Model that the CRUD is based on, any suggestions on how i get this to work as expected ?
I tried Roman's suggestion of having a model which is the TaskType and a new model extended from that which is the TaskType_Team with the addCondition in it like this
class Model_TaskType_Team extends Model_TaskType {
function init(){
parent::init();
$this->addCondition('team_id',$p->api->auth->get('team_id'));
}
for which i needed to create a subdirectory undel Model called TaskType otherwise it didnt find the new Model but the end result is the same. I think this is related to another issue i previously had where the Ajax dialog loses access to $p->api and so doesnt display the restriction (and this is why it works fine for the grid on the same page as that isnt in an ajax dialog but i dont want to use a stickyGet to resolve this for security (dont want to be able to modify the URL to see other teams data) and session variables ($p->auth->memorise and $p->auth->recall) also dont seem work in this case - any further suggestions ?
Remember that you can extend your models like that. In fact, this is very often used in larger projects.
class Model_TaskType_Team extends Model_TaskType {
function init(){
parent::init();
$this->addCondition('team_id',$this->api->auth->get('team_id'));
}
}