I have created a custom model (My_Model) containing all the crud functions. now i want to inherit that general model class in other models.
application/core/My_Model.php
<?php
class My_Model extends CI_Model {
protected $_table;
public function __construct() {
parent::__construct();
$this->load->helper("inflector");
if(!$this->_table){
$this->_table = strtolower(plural(str_replace("_model", "", get_class($this))));
}
}
public function get() {
$args = func_get_args();
if(count($args) > 1 || is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->get($this->_table)->row();
}
public function get_all() {
$args = func_get_args();
if(count($args) > 1 || is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->get($this->_table)->result();
}
public function insert($data) {
$success = $this->db->insert($this->_table, $data);
if($success) {
return $this->db->insert_id();
} else {
return FALSE;
}
}
public function update() {
$args = func_get_args();
if(is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->update($this->_table, $args[1]);
}
public function delete() {
$args = func_get_args();
if(count($args) > 1 || is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->delete($this->_table);
}
}
?>
application/models/user_model.php
<?php
class User_model extends My_Model { }
?>
application/controllers/users.php
<?php
class Users extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model("user_model");
}
function index() {
if($this->input->post("signup")) {
$data = array(
"username" => $this->input->post("username"),
"email" => $this->input->post("email"),
"password" => $this->input->post("password"),
"fullname" => $this->input->post("fullname")
);
if($this->user_model->insert($data)) {
$this->session->set_flashdata("message", "Success!");
redirect(base_url()."users");
}
}
$this->load->view("user_signup");
}
}
?>
when i load the controller i get an 500 internal server error but
if i uncomment the line in controller -- $this->load->model("user_model");
then the view page loads,...cant figure out whats happening...plz help..
In CI config file 'application/config/config.php' find and set configuration item
$config['subclass_prefix'] = 'My_';
then the CI load_class function will load CI_Model and My_model when calling $ths->load->model('user_model') in your routine;
Related
I apologize for not framing the question title correctly.
I am working on skeleton Application of zf3 to implement acl.I couldn't figure how to retrieve the row of corresponding email address.I have two controllers AlbumController.php and LoginController.php
AlbumController.php
private $table;
public function __construct(AlbumTable $table)
{
$this->table = $table;
}
public function deleteAction()
{
$user_session=new Container('user');
if(isset($user_session->email))
{
$row=$this->loginTable->getRow($user_session->email);//*Here is the problem*
if($row['role']=='admin')
{
$acl=new Acl();
if($acl->isAllowed('admin','AlbumController','delete'))
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('album');
}
$request = $this->getRequest();
if ($request->isPost()) {
$del = $request->getPost('del', 'No');
if ($del == 'Yes') {
$id = (int) $request->getPost('id');
$this->table->deleteAlbum($id);
}
return $this->redirect()->toRoute('album');
}
return [
'id' => $id,
'album' => $this->table->getAlbum($id),
];
}
}
return $this->redirect()->toRoute('login');
}
}
LoginController.php
public $user_session;
public $loginTable;
public function __construct(LoginTable $loginTable)
{
$this->loginTable = $loginTable;
}
I am calling getRow() method of LoginTable.php present in Model
LoginTable.php. But it is throwing an error Call to a member function getRow() on a non-object
LoginTable.php
class LoginTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function getRow($mail)
{
$email = $mail;
$rowset = $this->tableGateway->select(array('email' => $email));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $email");
}
return $row;
}
You are calling $this->loginTable->getRow() in your AlbumController class, but you didn't define loginTable in this controller. You did it in your LoginController class, but this is not the same objects.
Inject a LoginTable instance in your AlbumController:
AlbumController.php
....
private $albumTable;
private $loginTable;
public function __construct(AlbumTable $albumTable, LoginTable $loginTable)
{
$this->albumTable= $albumTable;
$this->loginTable= $loginTable;
}
....
AlbumControllerFactory.php (adapt to your code):
class AlbumControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new AlbumController(
$container->get(AlbumTable::class),
$container->get(LoginTable::class)
);
}
}
When trying to access a function in another controller, I am getting a __construct() must be an instance of App\Models\Page, none given error message and I am wondering how to solve this...
The controller where the function is called from:
use App\Http\Controllers\Frontend\PagesController;
...
class ModulesController extends Controller {
public function filter(Request $request) {
$items = $this->loadFilteredItems($request->module_id, $request->filters);
$page = new PagesController();
$header = $page->loadElements($request->id, false);
return View::make(
'base.frontend.' . config('folder') . '.includes.filter',
compact('items', 'header'));
}
}
}
The controller that stores the function:
class PagesController extends Controller {
private $page;
public function __construct(Page $page) {
$this->page = $page;
}
private function loadElements($id) {
return 'something';
}
}
Adapted the Controllers into this:
class ModulesController extends Controller {
public function filter(Request $request) {
$items = $this->loadFilteredItems($request->module_id, $request->filters);
$page = new PagesController();
$header = $page->loadHeaderElements($request->id);
return View::make(
'base.frontend.' . config('folder') . '.includes.filter',
compact('items', 'header'));
}
}
}
class PagesController extends Controller {
/**
Removed these lines
private $page;
public function __construct(Page $page) {
$this->page = $page;
}
**/
public static function loadHeaderElements($id) {
return $this->loadElements($id);
}
private function loadElements($id) {
return 'something';
}
}
And it's working now
I was wondering what a general layout in Laravel 5 is like and if it is in L4 so I wrote some code. I've been using teepluss/laravel-theme but I want something of my own. If anyone could give me some ideas it would be useful.
Controller.php:
<?php
abstract class Controller extends BaseController
{
use DispatchesCommands, ValidatesRequests;
protected $layout = null;
function __construct()
{
$this->setLayout();
}
protected function setPageContent($content)
{
if (is_null($this->layout)) {
throw new Exception('layout was not set');
}
return view($this->layout, ['content' => view($content)]);
}
public function setLayout()
{
$lay = DB::table('layout')->where('selected', 1)->first();//i select the layout from a database
return $this->layout = $lay->name . ".layouts." . $lay->name; //create a file with the database name
}
protected function setView($vista)
{
$view = DB::table('layout')->where('selected', 1)->first();
return $view->name . '/' . $vista;
}
}
After that I call getIndex() in MainController.php:
public function getIndex($page = null)
{
if ($page == null) {
return $this->setPageContent($this->setView('index'));
} else {
return $this->setPageContent($this->setView($page));
}
}
routes.php:
Route::controller('/{page?}/{param?}', 'MainController');
I tried a code which I called a parent method in its daughter __construct and itreturns NULL,
I dont know why? I would be very happy if anyone could explain to me why.
Thanks in advance.
Here is my code
<?php
class me
{
public $arm;
public $leg;
public function __construct()
{
$this->arm = 'beautiful';
$this->leg = 'pretty';
}
public function setLeg($l)
{
$this->leg = $l;
}
public function getLeg()
{
return $this->leg;
}
}
class myBio extends me
{
public $bio;
public function __construc()
{
$this->bio = $this->setLeg();
}
public function newLeg()
{
var_dump($this->bio);
}
public function tryLeg()
{
$this->leg = $this->getLeg();
print $this->leg;
}
}
$mB = new myBio();
$mB->newLeg();
$mB->tryLeg();
?>
When I call:
$mB = new myBio();
$mB->newLeg();
, it returns
NULL,
BUT
$mB->tryLeg();
returns e string, 'pretty'.
You have a typo on this line:
$this->bio = $this->setLeg();
You're calling your setter, not your getter, and since the setter doesn't return a value you're getting null instead.
You've also misspelled construct:
public function __construc()
And you need to call the parent constructor.
<?php
class me
{
public $arm;
public $leg;
public function __construct()
{
$this->arm = 'beautiful';
$this->leg = 'pretty';
}
public function setLeg($l)
{
$this->leg = $l;
}
public function getLeg()
{
return $this->leg;
}
}
class myBio extends me
{
public $bio;
public function __construct()
{
parent::__construct();
$this->bio = $this->getLeg();
}
public function newLeg()
{
var_dump($this->bio);
}
public function tryLeg()
{
$this->leg = $this->getLeg();
print $this->leg;
}
}
$mB = new myBio();
$mB->newLeg();
$mB->tryLeg();
Now I am learning CodeIgniter_2.1.4 but I got a php error;
I have a my_model.php file in /data/www/application/core
<?php
class MY_Model extends CI_Model {
const DB_TABLE = 'abstract';
const DB_TABLE_PK = 'abstract';
private function insert() {
$this->db->insert($this::DB_TABLE, $this);
$this->{$this::DB_TABLE_PK} = $this->db->insert_id();
}
private function update() {
$this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK);
}
public function populate($row) {
foreach($row as $key => $value) {
$this->$key = $value;
}
}
public function load($id) {
$query = $this->db->get_where($this::DB_TABLE, array(
$this::DB_TABLE_PK => $id,
));
$this->populate($query->row());
}
public function delete(){
$this->db->delete($this::DB_TABLE, array(
$this::DB_TABLE_PK => $this->{$this::DB_TABLE_PK},
));
unset($this->{$this::DB_TABLE_PK});
}
public function save(){
if(isset($this->{$this::DB_TABLE_PK})) {
$this->update();
}
else {
$this->insert();
}
}
public function get($limit = 0, $offset = 0) {
if($limit) {
$query = $this->db->get($this::DB_TABE, $limit, $offset);
}
else {
$query = $this->db->get($this::DB_TABLE);
}
$ret_val = array();
$class = get_class($this);
foreach ($query->result() as $row) {
$model = new $class;
$model->populate($row);
$ret_val[$row->{$this::DB_TABLE_PK}] = $model;
}
return $ret_val;
}
}
and my domain model is :
<?php
class Publication extends MY_Model {
const DB_TABLE = 'publications';
const DB_TABLE_PK = 'publication_id';
public $publication_id;
public $publication_name;
}
well when I get model in my controller I got this php error:
PHP Fatal error: Class 'MY_Model' not found in /data/www/application/models/publication.php on line 3
I have tried two hours finding the reason but failed ):
I have a my_model.php file in /data/www/application/core
the my_model.php should be renamed to MY_Model.php.
It should be a case-sensitivity issue. Class names must have the first letter capitalized with the rest of the name lowercase.
in your publications.php have the following statement before the class declaration.
require_once "my_model.php";
the error is because you haven't included the definition of My_Model in your publications.php