Codeigniter with datamapper save error - php

So I was trying to make a CRUD for my app. I have setup a controller, like this:
class Clients extends CI_Controller {
public function __construct() {
parent::__construct();
session_start();
$this->c = new Client();
$this->u = new User();
}
function index() {
$data['current_view'] = 'client_view';
$data['header'] = 'Меню клиентов';
$data['clients'] = $this->getClients();
$this->load->view('main_view',$data);
}
function getClients() {
$this->u->where('username',$_SESSION['username'])->get();
$c = $this->c->where('user_id',$this->u->id)->get();
return $c;
}
function delClient() {
$this->c->where('client_id', $this->uri->segment(3))->delete();
$this->c->skip_validation()->save();
$this->index();
}
}
However, when I'm trying to perform a delete on a client, i get a db error:
You must use the "set" method to update an entry.
Filename: C:\OpenServer\domains\localhost\system\database\DB_active_rec.php
Line Number: 1174
What might be the cause of this? I found a similar question here, but I don't think that's the case.
EDIT: Client model:
class Client extends DataMapper {
public $has_one = array('user');
public function __construct($id = NULL) {
parent::__construct($id);
}
}

you have not passed the uri segment to the function delClient() and this is your model right...
function delClient($id) {
//$id is the value which you want to delete
$this->c->where('client_id', $id)->delete();
$this->c->skip_validation()->save();
$this->index();
}

Related

Autoload model and call function inside this model to whole project

I need function (literally i need the $data) from this model in whole project.
when i call in some controller $data = $this->multi_language_model->multi_lang(); it work fine. But is it possible to call in one place, so I can use in every controller and view.
I autoload the model
$autoload['model'] = array('multi_language_model');
class Multi_language_model extends MY_Model
{
public function __construct() {
parent::__construct();
}
public function multi_lang() {
$data['menu_delivery'] = $this->lang->line('menu_delivery');
$data['menu_quotations'] = $this->lang->line('menu_quotations');
$data['menu_customer_service'] = $this->lang->line('menu_customer_service');
return $data;
}
}
Put this helper function in your any loaded helper:
function get_multi_lang(){
$CI = & get_instance();
$data = array();
$CI->load->helper('language');
$CI->lang->load('menu','english');
$data['menu_delivery'] = $CI->lang->line('menu_delivery');
$data['menu_quotations'] = $CI->lang->line('menu_quotations');
$data['menu_customer_service'] = $CI->lang->line('menu_customer_service');
return $data;
}
Controller:
class Yourclassname extends CI_Controller {
var $menu_delivery = "";
var $menu_quotations = "";
var $menu_customer_service = "";
function __construct() {
parent::__construct();
$data = get_multi_lang();
$this->menu_delivery = $data['menu_delivery'];
$this->menu_quotations = $data['menu_quotations'];
$this->menu_customer_service = $data['menu_customer_service'];
}
public function index(){
echo $this->menu_delivery.'<pre>';
echo $this->menu_quotations.'<pre>';
echo $this->menu_customer_service.'<pre>';die;
}
}
If you dont want to use helper function then copy helper function lines in controller __construct() [replace $data to $this->]so direct use global variables do same in model and for view pass this variable via controller
I don't know how codeigniter works but maybe you could do this method static and use it like:
class Multi_language_model extends MY_Model
{
private static $data = [];
public function get_multi_lang()
{
return [
'menu_delivery' => $this->lang->line('menu_delivery'),
'menu_quotations' => $this->lang->line('menu_quotations'),
'menu_customer_service' => $this->lang->line('menu_customer_service'),
];
}
public static function multi_lang()
{
if (empty(self::$data)) {
self::$data = (new self)->get_multi_lang();
}
return self::$data;
}
}
Then, whenever you need it, you can use $data = Multi_language_model::multi_lang();
However, I don't see anything wrong in injecting it from the container, wherever you need it. Doing so would be much easier to build tests.
BTW you don't need to overwrite the class constructor if there are no custom parameters set to the extending class. You can safely remove:
public function __construct() {
parent::__construct();
}

How to access data of one function to another in laravel

Okay the issue is something like this
I have a function in AController
public function index()
{
$store = Store::(query)(to)(rows)->first();
return view('store.index', compact('store'));
}
Now in the same controller I have another function
public function abc()
{
return view('store.abc');
}
Now to this function I also want to send the compact('store') to the view abc I can just add the query again in the abc() function but that would be lazy and make performance issues. Is there a way that I can access $store object in other functions too?
If I understand you correctly you want to access the same query from two places. So extract getting stores to another method like
private function store()
{
$minutes = 10; // set here
return Cache::remember('users', $minutes, function () {
return Store::(query)(to)(rows)->first();
});
}
Additionally I have cached the query. So it get executed once at a defiened time.
Then access it from other two methods like,
public function index()
{
$store = $this->store();
return view('store.index', compact('store'));
}
public function abc()
{
$store = $this->store();
return view('store.abc', compact('store'));
}
class StoreController extends Controller
{
public function index()
{
return view('admin.store',['data' => $this->getSetting()]);
}
public function getStoreData()
{
//get your data here, for example
$data = Store::where('status',1)->first();
//get all data
//$data = Store::all();
return ($data);
}
}
Try the following. Not testing but it should work for you.
class AController
{
public function getStore()
{
$store = Store::(query)(to)(rows)->first();
return compact('store');
}
public function index()
{
return view('store.index', $this->getStore());
}
public function abc()
{
return view('store.abc', $this->getStore());
}
}

CodeIgniter use a global session variable from contructor

I need to use a session information in various functions of my controller, but I can't initialize it in the constructor, because I get an error. Message: Undefined property: Soporte::$session
class Soporte extends MY_Controller {
function __construct(){
parent::__construct( $module, $functionality );
}
public function actualizarSolicitud( $id_solicitud ){
$session_data = $this->session->userdata('session_user');
$user = $session_data['usuario'];
...
}
public function adminHistorico(){
$session_data = $this->session->userdata('session_user');
$user = $session_data['usuario'];
$config = array();
...
}
...
}
There's a way to initialize a global variable $user?
Try like below, model is quite complicated so I'm not providing it's code, but you should get the point. Any questions let me know.
/**
* This class is used for performing all read/write session operations
* Native php session is utilized (MY_Session library)
*/
class SessionManager extends BaseLibrary {
private $oUser;
public function __construct() {
parent::__construct();
$this->CI->load->model('User');
}
public function setUser(User $oUser) {
$this->CI->session->set_userdata('userId', $oUser->getId());
}
public function getUser() {
if ($this->oUser === null) {
$this->oUser = new User();
if ($this->CI->session->userdata('userId')) {
$this->oUser->setId($this->CI->session->userdata('userId'));
}
}
return $this->oUser;
}
public function logout() {
$this->CI->session->set_userdata('userId', NULL);
}
}

php new class object not being instanced

I am trying to create a new model object from my mvc controller but the page doesn't generate. Is there any reason why I can't do this? Surely I should be able to create an object inside an existing one?
Sorry to be so simplistic, and I know I sound like an idiot, but I'm not sure how to explain what I am doing wrong.
class controller_landing extends controller_base
{
public function __construct($get,$post)
{
parent::__construct($get,$post);
$this->model = new model_landing; <-----problem line here
}
}
abstract class controller_base
{
//store headers
protected $get;
protected $post;
//store layers
protected $view;
protected $model;
protected function __construct($get,$post)
{
//store the header arrays
$this->get = $get;
$this->post = $post;
//preset the view layer as an array
$this->view = array();
}
public function __destruct()
{
//extract variables from the view layer
extract($this->view);
//render the view to the user
require_once('view/'.$this->get['controller'].'_view.php');
}
}
class model_landing extends class_mysqli
{
public function __construct
{
echo "landing model";
}
}
class class_mysqli
{
public function __construct
{
echo "mysqli";
}
}
I don´t know, but I think you are missing brackets.
There
public function __construct
{
echo "landing model";
}
should be
public function __construct()
{
echo "landing model";
}

phil's rest server before action

I am creating a rest service. I have done all the required methods, what I want to do is some authorization. I have created the table where I store the api-keys, i load them in each method, and it works quite well.
What I need now, is to do some before action that would be called before each method, so i don't have to check if the user is successfully authorized on each method? In simple CI_Controller or in FuelPHP that can be done using public function before, but I dont know how to achieve the same thing in REST_Controller?
Thank you in advance
Here are two controllers. May give you some idea
class MY_Controller extends CI_Controller
{
protected $before_filter = array();
protected $after_filter = array();
private function run_filter($who, $params=array())
{
$filter = $this->{"{$who}_filter"};
if (is_string($filter)) {
$filter = array($filter);
}
if (method_exists($this, "{$who}_filter")) {
$filter[] = "{$who}_filter";
}
foreach ($filter as $method) {
call_user_func_array(array($this, $method), $params);
}
}
public function _remap($method, $parameters)
{
if (method_exists($this, $method))
{
$this->run_filter('before', $parameters);
$return = call_user_func_array(array($this, $method),$parameters);
$this->run_filter('after', $parameters);
}else{
show_404();
}
}
}
class MY_Controller extends CI_Controller
{
public $before_filter = array('check_login');
public $after_filter = array();
private function dashboard()
{
/* other code here */
}
public function check_login()
{
/* Login checking Code here */
}
}

Categories