I created a few files in app/Lib folder and would like to access one of my models from the library classes:
<?php
App::uses('CrawlerBase','Lib');
App::uses('Deal', 'Model');
class SampleCrawler extends CrawlerBase {
public $uses = array('Deal');
function __construct(){
$this->Deal->create();
However, cake cant seems to find the Deal model and im getting a call to member function create() on a non-object in the model creation line.
Appreciate the help.
Always include models manually if not in a controller/shell:
$this->Deal = ClassRegistry::init('Deal');
and then
$this->Deal->create(); // etc
The advantage: You let Cake load and init the model for you, so if you already did that earlier it will try to reuse it.
EDIT: for the sake of completeness, inside a controller/shell you can simply do
$this->loadModel('Deal');
$this->Deal->create();
Other way to also do this:
APP::import('Model', 'Deal');
$this->Deal = new Deal();
$this->Deal->create();
Try;
$deal = new Deal(); // to create Deal Object
//if that doesnot work then, do
ClassRegistry::init("Deal");
$deal = new Deal();
Related
I am using codeigniter 3
in application/config/config.php file I have added this autoload code for model
function __autoload($class) {
if (file_exists(APPPATH."models/".strtolower($class).EXT)) {
include_once(APPPATH."models/".strtolower($class).EXT);
}
}
to autoload model
and I am using model in controller like this
public function index()
{
$post = new post();
}
but it is showing error
Class 'post' not found
I do have post model in model folder already created
I am using the autoload code from source
http://code.tutsplus.com/tutorials/6-codeigniter-hacks-for-the-masters--net-8308
but it is not working like shown in blog.
Do I need anything else to update more for this?
If you need to autoload a model in your CI3 app, just go in application/config/autoload.php and find the line :
$autoload['model'] = array();
Then, add the model you want to autoload :
$autoload['model'] = array('my_model', 'my_second_model');
Then in your controller, you don't need to create a new instance of your model class. Example :
$res = $this->my_model->myfunction();
Use capital letter for your class name.
Btw. I agree with first answer.
In my CodeIgniter project I am using codeigniter-base-model and extending all my models from there.
In my user model I want to ensure that super users don't get included, unless specified otherwise.
Something like:
$this->db->where('uacc_issuper', 0));
And I suspect I can use the observer $before_get for this. But I'm not sure how go about it.
Any suggestions pointing me in the right direction will be appreciated.
in $before_get you have give array of method name called before any get query, you need to add those methods in your child model
class page_model extends MY_Model{
protected $before_get = array('method_name');
function __construct(){
parent::__construct();
}
function method_name(){
$this->db->where('uacc_issuper', 0));
}
}
now if you try to get anything with page model this method will be called every time
$page = $this->page->get(1);
I have some data array which I need for all method's in a controller.
$data['project_ongoing_res_limit']=$this->admin_model->show_project_ongoing_residential_limit();
$data['project_ongoing_com_limit']=$this->admin_model->show_project_ongoing_commercial_limit();
$data['project_upcoming_res_limit']=$this->admin_model->show_project_upcoming_residential_limit();
$data['project_upcoming_com_limit']=$this->admin_model->show_project_upcoming_commercial_limit();
$data['project_completed_res_limit']=$this->admin_model->show_project_completed_residential_limit();
$data['project_completed_com_limit']=$this->admin_model->show_project_completed_commercial_limit();
Problem is I cant DRY this. so I have paste this $data array in each method.
I have a view page for this. so when I load this view , I have to
load above $data array each time/method. this is disgusting when controller
methods are too much.
I want 1 piece of this code like constructor. How can I do this.
you can use traits for this.
Define your methods in a trait, and then use the trait in the controllers.
You can make one helper class in which make a function and put your above code in it but make sure you can't access model using $this so, you need to create CI instance and than access it. after that in your controller in construct method you just need to call this function but don't forget to load the helper class and store it in a variable and pass it along with view.
Just create private data variable in your controller class. Than set your data in constructor. Now you can access your data in any method you want.
class Pages extends CI_Controller {
// ...
private $data;
// ...
public function __construct() {
parent::_construct();
$this->data = array();
$this->data['project_ongoing_res_limit']=$this->admin_model->show_project_ongoing_residential_limit();
$this->data['project_ongoing_com_limit']=$this->admin_model->show_project_ongoing_commercial_limit();
$this->data['project_upcoming_res_limit']=$this->admin_model->show_project_upcoming_residential_limit();
$this->data['project_upcoming_com_limit']=$this->admin_model->show_project_upcoming_commercial_limit();
$this->data['project_completed_res_limit']=$this->admin_model->show_project_completed_residential_limit();
$this->data['project_completed_com_limit']=$this->admin_model->show_project_completed_commercial_limit();
}
// ...
}
First please be gentle i am a beginner and im only coding for practise.
I try to pass an instance to the model but i always get this error
Argument 1 passed to Store::__construct() must be an instance of Illuminate\Filesystem\Filesystem, none given
my model
<?php
use Illuminate\Filesystem\Filesystem as File;
class Store extends Eloquent
{
public $timestamps = false;
public function __construct(File $file)
{
$this->file = $file;
}
}
Could please somebody tell me what i am doing wrong?
thank you
EDIT
I just used simply like this in my Controller
public function index()
{
$store = new Store;
return View::make('store', $store);
}
The File class is one of Laravels Facades, which means you do not need to pass it into your models construct.
You can access it from anywhere in Laravel using File::someMethod(). If you use namespaces then you have to access via the root namespace \File::someMethod().
Within your store view you can access the File facade directly with the aforementioned method.
Take a look at the documentation on the file system here http://laravel.com/api/class-Illuminate.Filesystem.Filesystem.html
So you can use File::copy() without having to instantiate a class as it is called from a static method.
While trying to access a user class object from codeigniter helper file, its throwing error like Class 'User' not found. My code is something like
$u = new User();
$u->get();
I am able to use this in library files but not in helper files. Can somebody help me.
In order to use model in helper you have to:
// Get a reference to the controller object
$CI = get_instance();
// You may need to load the model if it hasn't been pre-loaded
$CI->load->model('User');
// Call a function of the model
$CI->User->get();
hope it will help!