Joomla Component data from model to view - model-view-controller

I am creating a component in Joomla for the first time, now i am having a hard time calling data from my model in to my view. I did this as follows:
//In my model
class InternetModelDefault extends JModel {
public function test(){
$this->test='test';
}
}
//In my view
class InternetViewInternet extends JView {
$model = $this->getModel();
$test = $model->test();
var_dump($test);
// Display the view
parent::display($tpl);
}
}
The output gives the following error:
Fatal error: Call to a member function test() on a non-object in
/var/websites/www.infrait.nl/content/components/com_internet/views/internet/view.html.php
on line 66
Where does it went wrong? Please some help..
Current mapstructure:
http://imgdump.nl/hosted/ad9e57de83060b3240f8fc6bba99237b.png
As i am new i can only share you this direct link.

your model.php like this
jimport ('joomla.application.component.modellist');
class InternetModelInternet extends JModelList
{
public function test(){
$this->test='test';
}
}
and in view.html.php get model
jimport ('joomla.application.component.view');
class InternetViewInternet extends JView
{
public function display ($tpl = null)
{
$model = &$this->getModel ();
$test = $model->test();
var_dump($test);
// Display the view
parent::display($tpl);
}
}

You are getting the error because object is not initiated properly.Change class name to InternetModelInternet. And model file name will be internet.php.
Let me know if it does not work.

Related

Laravel 5 Displaying related model data on blade

I would like to create an eager loading on my model but I keep getting and error. Anyways here is my model structure
// table name is drug_logs
class DrugLog extends Model
{
public function drug() {
return $this->belongsTo('App\Drug');
}
public function dispensedDrug() {
return $this->hasOne('App\DispensedDrug');
}
}
// table name is drugs
class Drug extends Model
{
public function drugLogs() {
return $this->hasMany('App\DrugLog');
}
}
// table name is dispensed_drugs
class DispensedDrug extends Model
{
public function drugLog() {
return $this->belongsTo('App\DrugLog');
}
}
But now on my controller I tried using this function
$drug_logs = DrugLog::with('dispensed_drugs')->get();
But I keep getting an error
BadMethodCallException in Builder.php line 2003: Call to undefined method Illuminate\Database\Query\Builder::dispensed_drugs()
Any idea on this guys?

Codeignite 404 error

I ham writing the follow Codeigniter code, but I keep getting a 404 error. Any thoughts?
funding_controller.php
class Funding extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function index()
{
$this->load->model('funding_model');
$data['funds'] = $this->funding_model->getAll();
$this->load->view('funding_list', $data);
}
}
funding_model.php
class Funding extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function getAll() {
$query = $this->db->get('funds');
return $query->result();
}
}
funding_list.php
foreach($funds as $row) {
echo $row->opportunity_name;
echo "<br />";
}
The default route points to funding_controller. Thoughts?
controller name and class name should be same and file name in lower case
funding.php
And try following url
http://domain.com/funding/index
Model class name different from controller name like funding_model or anything else and no need to suffix _model in ->load->model()
Use first letter in capital for controller n model file name.
For example:
Founding_controller.php
Founding_model.php
And class name should be filename (as it is).

Laravel4 - Saving a model with multiple relationships/foreign keys

I've tried to understand a process of saving a model with multiple relationships but I still can't figure out how to do it "kosher" way.
To begin with - I have an Event model that belongs to a category (Eventcat) and a Location:
// Event.php
class Event extends Eloquent {
protected $table = 'events';
public function location()
{
return $this->belongsTo('Location');
}
public function eventcat()
{
return $this->belongsTo('Eventcat');
}
public function users()
{
return $this->belongsToMany('User');
}
}
// Location.php
class Location extends Eloquent
{
protected $table = 'locations';
public function events()
{
return $this->hasMany('Event');
}
}
// Eventcat.php
class Eventcat extends Eloquent
{
protected $table = 'eventcats';
public function events()
{
return $this->hasMany('Event');
}
}
I've seeded the database with a few categories and locations and now I trying to get events saving work. I thought that the $event->eventcat()->associate( $eventcat ) would work but I got a Call to undefined method eventcat() error.
public function postCreateEvent() {
$event = new Event();
$eventcat = Eventcat::find( Input::get('event-create-eventcat[]') );
$location = Location::find( Input::get('event-create-location[]') );
$event->title = Input::get('event-create-title');
$event->description = Input::get('event-create-description');
$event->price = Input::get('event-create-price');
$event->start_date = Input::get('event-create-start_date');
$event->end_date = Input::get('event-create-end_date');
$event->eventcat()->associate( $eventcat );
$event->location()->associate( $location );
$event->save();
}
I've read the documentation, API and a few threads here but I still can't figure out the best way to deal with this.
Thanks for replies!
I would actually bet that you have a conflict in your class name. Laravel contains an Event class and I wonder if that isn't what's being called in your code. As a quick test, you could rename your class FooEvent and see if it works.
The best solution is probably namespacing your model (see http://chrishayes.ca/blog/code/laravel-4-methods-staying-organized for a quick intro) so that your model can still be called Event without conflicting with the builtin class.

error for inserting data in database in view in joomla

I want to insert a data in database but it show error
Fatal error: Call to a member function getform() on a non-object in
\components\com_enquiry\views\form\view.html.php
on line 9
my code is:
enqiry.php:
public function getform($data)
{
$db=JFactory::getDbo();
$db=$this->getDbo();
$query=$db->getQuery(true);
$reg=new stdClass();
$reg->name=$data['name'];
$reg->email=$data['email'];
$reg->phone=$data['phone'];
$reg->comments=$data['comments'];
$reg=$db->insertObject('#__enquiry',$reg);
}
and view.html.php:
public function display()
{
$this->msg = 'enquiry form';
$model=$this->getModel();
$data =$model->getform();
$this->assignRef('data', $data );
parent::display();
parent::display();
}
controller:
class enquiryController extends JControllerLegacy
{
public function display()
{
$vname=JRequest::getCmd('view','form');
JRequest::setVar('view',$vname);
JRequest::setVar('layout','default');
parent::display();
}
public function show()
{
$data['name']=$_POST['name'];
$data['email']=$_POST['email'];
$data['phone']=$_POST['phone'];
$data['comments']=$_POST['comments'];
$sname=JRequest::getCmd('view','thanx');
JRequest::setVar('view',$sname);
print_r($data);
}
}
could someone help me solve this error?
The problem is that your model is not loaded by the line:
$model=$this->getModel();
This either means that your model in enquiry.php is not named correctly
ComponentNameModelEnquiry
or that your view is not named enquiry.
You can try to specify which model you want, like
$model = $this->getModel('enquiry');

Codeigniter model can not connect to db

My controller can connect to the DB but my model can not. I have autoloaded the DB in the autoload.php file, but no luck in the model.
so if I do something like
$this->db->insert('table', $data);
I receive this Call to a member function insert() on a non-object I have used Codeigniter before but have never had this issue, on my other project I did not even use parent::__construct()
class Bucketlist extends CI_Model {
private $data = array();
public function __construct(){
parent::__construct();
}
// Setter Function
public function __set ($var, $val) {
$this->data[$var] = $val;
}
// Getter Function
public function __get($var) {
return (isset($this->data[$var])) ? $this->data[$var] : null;
}
// Create WishList
function createBucketList($bucketlist) {
$this->db->insert('_bucketlist', $bucketlist->data);
}
}
thanks.
You may need to Autoload the Database connection (http://ellislab.com/codeigniter/user-guide/database/connecting.html), as it appears that the db variable is not instantiated before you are trying to use it.

Resources