Welcome Guest, Not a member yet? Register   Sign In
What's wrong with my model?
#1

[eluser]Kemik[/eluser]
Hello all,

I'm pulling a list of fixtures from my database using a model.

Here's a simple version of my view file:

Code:
<?php $this->load->model('Menu_model', 'menu'); ?>

        <div id="rightbar">
            &lt;?php $query = $this->menu->get_fixtures(); ?&gt;
            <div class="boxheader">UPCOMING MATCHES</div>
            <div class="boxtext">
            &lt;?php if ($query->num_rows() < 1): ?&gt;
            <a href="#" class="boxlink">- No upcoming matches</a>
            &lt;?php else: ?&gt;
            &lt;?php foreach ($query->result_array() as $row): ?&gt;
            <a href="&lt;?php echo site_url("challenge/info/$row->challenge_id"); ?&gt;"><img src="&lt;?php echo base_url(); ?&gt;images/icons/zoom.png" width="16" height="16" border="0" alt="Challenge Info" /></a> <a href="&lt;?php echo site_url("clan/profile/$clana_id"); ?&gt;">[&lt;?php echo $clana; ?&gt;]</a> vs <a href="&lt;?php echo site_url("clan/profile/$clanb_id"); ?&gt;">[&lt;?php echo $clanb; ?&gt;]</a>
            &lt;?php endforeach;
            endif; ?&gt;
            
            
            </div>
</div>

Now from my menu_model.php
Code:
&lt;?php
class Menu_model extends Model {

    function Menu_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function get_fixtures()
    {
        $query = "SELECT lc.challenge_id, lc.datetime, lc.clan_id_a, lc.clan_id_b, ca.clan_id as clanid_a, ca.tag as clana, cb.clan_id as clanid_b, cb.tag as clanb
               FROM ladder_challenges AS lc
               LEFT JOIN clans AS ca ON lc.clan_id_a = c.clan_id
               LEFT JOIN clans AS cb ON lc.clan_id_b = c.clan_id
               WHERE accepted = '1'
               ORDER BY datetime
               LIMIT 0, 4";
        return $query = $this->db->query($query);
    }
}
?&gt;

I keep getting Fatal error: Call to a member function on a non-object in menus.php

Anyone have any idea where I'm going wrong?

Thanks.
#2

[eluser]deviant[/eluser]
You are making $row an array using result_array then trying to access it like an object from what I can see.
#3

[eluser]Kemik[/eluser]
Ah yes. Nice spot I need to actually add $row-> instead of $variable. I'll fix that, but the error is on line "&lt;?php $query = $this->menu->get_fixtures(); ?&gt;"
#4

[eluser]Michael Wales[/eluser]
I didn't really look over the code to much, but you are definitely destroying the MVC convention in an entirely new and interesting way.

Why isn't 90% of the code from your view in the controller?

Controller:
Code:
$this->load->model('Menu_model', 'menu');
$query = $this->menu->get_fixtures();
if ($query !== FALSE) {
  $data['fixtures'] = $query->result();
}
$this->load->view('theview', $data);

View:
Code:
<div id="rightbar">
  <div class="boxheader">UPCOMING MATCHES</div>
    <div class="boxtext">
    &lt;?php if (isset($fixtures)) { ?&gt;
      <a href="#" class="boxlink">- No upcoming matches</a>
    &lt;?php } else { ?&gt;
      &lt;?php foreach ($fixtures as $row): ?&gt;
      <a href="&lt;?php echo site_url("challenge/info/$row->challenge_id"); ?&gt;"><img src="&lt;?php echo base_url(); ?&gt;images/icons/zoom.png" width="16" height="16" border="0" alt="Challenge Info" /></a> <a href="&lt;?php echo site_url("clan/profile/$clana_id"); ?&gt;">[&lt;?php echo $clana; ?&gt;]</a> vs <a href="&lt;?php echo site_url("clan/profile/$clanb_id"); ?&gt;">[&lt;?php echo $clanb; ?&gt;]</a>
      &lt;?php } ?&gt;
    &lt;?php }?&gt;
</div>

Model:
Code:
function get_fixtures() {
  $query = "SELECT lc.challenge_id, lc.datetime, lc.clan_id_a, lc.clan_id_b, ca.clan_id as clanid_a, ca.tag as clana, cb.clan_id as clanid_b, cb.tag as clanb
               FROM ladder_challenges AS lc
               LEFT JOIN clans AS ca ON lc.clan_id_a = c.clan_id
               LEFT JOIN clans AS cb ON lc.clan_id_b = c.clan_id
               WHERE accepted = '1'
               ORDER BY datetime
               LIMIT 0, 4";
  if ($query->num_rows() < 1) {
    return FALSE;
  } else {
    return $this->db->query($query);
  }
}
#5

[eluser]Kemik[/eluser]
Haha, I apologise. It's because the menu is a included view file. I have header, footer, menus and then the main content page.

At the moment I do:

$data['header'] = 'Challenge';
$data['page'] = 'ladder_challenge';
$this->load->view('container', $data);

The container then pulls ladder_challenge_view (for example) and sets the page header as Challenge. I don't know of any other way to display the views correctly without a lot of code per function. I understand views are meant to contain practically no php code and defiantly no queries, but there isn't any other way to do it with my system.

If I find a way to include views and have the MVC setup work 100% then I'll implement that. I got my current setup from http://ellislab.com/forums/viewthread/44916/
#6

[eluser]xwero[/eluser]
I think one row doesn't do it because it's possible there is more than one fixture, you need to use

Code:
$query = $this->db->query($query);
return $query->result_array();

In the view code you can do the following

Code:
&lt;?php if(count($query) == 0){ ?&gt;
<a href="#" class="boxlink">- No upcoming matches</a>
&lt;?php }else{
foreach($query as $row){
?&gt;
<a href="&lt;?php echo site_url("challenge/info/".$row['challenge_id']); ?&gt;"><img src="&lt;?php echo base_url(); ?&gt;images/icons/zoom.png" width="16" height="16" border="0" alt="Challenge Info" /></a> <a href="&lt;?php echo site_url("clan/profile/".$row['clana_id']); ?&gt;">[&lt;?php echo $row['clana']; ?&gt;]</a> vs <a href="&lt;?php echo site_url("clan/profile/".$row['clanb_id']); ?&gt;">[&lt;?php echo $row['clanb']; ?&gt;]</a>
&lt;?php }} ?&gt;

I hope this helps.

Ok too late Smile
#7

[eluser]Kemik[/eluser]
Thanks xwero. I've added your changes. I'm still getting the error though Sad I'm certain it has something to do with the way I'm loading the model.

Can you check to make sure the model is named correctly, file names, and loaded ok please? I've rattled my head and read the userguide on models loads of times, but I cannot see the difference between the examples there and mine.

Just to confirm. It's loading the model fine as otherwise I'd get an error saying that model doesn't exist. It's just giving me "Call to a member function on a non-object" on the &lt;?php $this->menu->get_fixtures(); ?&gt; line.

There's a function called get_fixtures() in the Menu_model.

menus.php
Code:
&lt;?php $this->load->model('Menu_model', 'menu'); ?&gt;
<div id="rightbar">
            &lt;?php $query =  $this->menu->get_fixtures(); ?&gt;
            <div class="boxheader">UPCOMING MATCHES</div>
            <div class="boxtext">
            &lt;?php if(count($query) == 0): ?&gt;
            <a href="#" class="boxlink">- No upcoming matches</a>
            &lt;?php else: ?&gt;
            &lt;?php foreach($query as $row): ?&gt;
            <a href="&lt;?php echo site_url("challenge/info/$row[challenge_id]"); ?&gt;"><img src="&lt;?php echo base_url(); ?&gt;images/icons/zoom.png" width="16" height="16" border="0" alt="Fixture Info" /></a> <a href="&lt;?php echo site_url("clan/profile/$row[clana_id]"); ?&gt;">[&lt;?php echo $row['clana']; ?&gt;]</a> vs <a href="&lt;?php echo site_url("clan/profile/$row[clanb_id]"); ?&gt;">[&lt;?php echo $row['clanb']; ?&gt;]</a>
            &lt;?php endforeach;
            endif; ?&gt;
            
            
            </div>
</div>
menu_model.php
Code:
&lt;?php
class Menu_model extends Model {

    function Menu_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function get_fixtures()
    {
        $query = "SELECT lc.challenge_id, lc.datetime, lc.clan_id_a, lc.clan_id_b, ca.clan_id as clanid_a, ca.tag as clana, cb.clan_id as clanid_b, cb.tag as clanb
               FROM ladder_challenges AS lc
               LEFT JOIN clans AS ca ON lc.clan_id_a = c.clan_id
               LEFT JOIN clans AS cb ON lc.clan_id_b = c.clan_id
               WHERE accepted = '1'
               ORDER BY datetime
               LIMIT 0, 4";
        $query = $this->db->query($query);
        return $query->result_array();
    }
}

?&gt;
#8

[eluser]xwero[/eluser]
i think we all overlooked one important thing. you have to add true as the third argument when you load the model (models are not linked to the database by default).

Code:
&lt;?php $this->load->model('Menu_model', 'menu',true); ?&gt;

That should do the trick.
#9

[eluser]Kemik[/eluser]
EDIT: Lesson of the day. Models cannot be loaded in views. Anyone know of a way to load it automatically in all controllers? For now, I'll put it in the controllername function.
#10

[eluser]xwero[/eluser]
I think there is a hack somewhere in the forum which enables you to autoload models but you can make a basecontroller where you load your models.

Code:
&lt;?php
class Base_Controller extends Controller
{

function Base_Controller()
{
   parent::Controller();
   $this->load->model('model','',true);
}

}
?&gt;

In your controllers you now call the base_controller instead of the normal controller.




Theme © iAndrew 2016 - Forum software by © MyBB