CodeIgniter Forums
Trying to get property of non-object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Trying to get property of non-object (/thread-40970.html)



Trying to get property of non-object - El Forum - 04-24-2011

[eluser]GesterX[/eluser]
I'm sure this is something trivial but I've spent the last hour scratching my head on this one!

I have several functions in my Model that all look like this (that find various things):

Code:
function getNPCName($npc)
    {
      $this->db->select('name')->from('npcs')->where('name', $npc)->limit(1);
      $query = $this->db->get();
      $row = $query->row();
      return $row->name;
    }

Then in my controller they are put in the view like this:

Code:
function getNPCData($npc)
    {
      $npcDialogue = $this->NPCModel->getNPCDialogue($npc);
      $npcQuestDialogue = $this->NPCModel->getNPCQuestDialogue($npc);
      $npcNoQuestDialogue = $this->NPCModel->getNPCNoQuestDialogue($npc);
      $npcQuestCompleteDialogue = $this->NPCModel->getNPCQuestCompleteDialogue($npc);
      $npcName = $this->NPCModel->getNPCName($npc);
      $npcQuest = $this->NPCModel->getNPCQuest($npc);

      $npcData = array(
                        'npcDialogue' => $npcDialogue,
                        'npcQuestDialogue' => $npcQuestDialogue,
                        'npcNoQuestDialogue' => $npcNoQuestDialogue,
                        'npcQuestCompleteDialogue' => $npcQuestCompleteDialogue,
                        'npcName' => $npcName,
                        'npcQuest' => $npcQuest
                        );

      $statData = $this->PlayerModel->createPlayerInfoArray();
      $this->load->view('game/statpanelView',$statData);
      $this->load->view('game/npcView', $npcData);
    }

The function is called from a different view (the game view) like this:
Code:
foreach ($listnpcs as $value)
     {
      echo anchor('npc/getNPCData/' . $value['name'], $value['name']). "<br>";
     }

Finally, the npc view looks like this:

Code:
echo "<b>" . $npcName . "</b><br><br>";
     echo "<b>" . $npcDialogue . "</b><br><br>";

So if my NPC is called "Bob the Scrapyard Man"; my "getNPCData()" function is called with the $npc parameter set as "Bob the Scrapyard Man". This means that the model functions are called to find the various data for Bob. However, when the npc view is loaded I get the error "Trying to get property of non-object" for every method in the controller!

Bob the Scrapyard Man is definitely in my npc table and all the other fields have data assigned. I tried calling $row = $query->result() but got the same error.

Any ideas? I'm sure this is something simple I've missed.
Thanks in advance!


Trying to get property of non-object - El Forum - 04-24-2011

[eluser]InsiteFX[/eluser]
Did you load the database and your model?

InsiteFX


Trying to get property of non-object - El Forum - 04-24-2011

[eluser]GesterX[/eluser]
[quote author="InsiteFX" date="1303695791"]Did you load the database and your model?

InsiteFX[/quote]

Yup. The wierd thing is that I have a very similar function in a different model which works completely fine. The constructor for both Models are identical:

Code:
function __construct() {
        parent::__construct();
        $this->load->library(array("session", "form_validation"));
        $this->load->helper(array('url'));
        $this->load->model('NPCModel', '', TRUE);
        $this->load->model('PlayerModel', '', TRUE);
    }

The db is loaded automatically and works fine on my other pages...


Trying to get property of non-object - El Forum - 04-24-2011

[eluser]InsiteFX[/eluser]
I would run the CI Profiler to see what is going on, you could also changing the logging in appliication/config/config.php to like 4 and turn logging on to also see whats going on.

InsiteFX


Trying to get property of non-object - El Forum - 04-24-2011

[eluser]GesterX[/eluser]
[quote author="InsiteFX" date="1303700852"]I would run the CI Profiler to see what is going on, you could also changing the logging in appliication/config/config.php to like 4 and turn logging on to also see whats going on.

InsiteFX[/quote]

I'm reasonably new to CI. How do I go about turning on and using the profiler?


Trying to get property of non-object - El Forum - 04-24-2011

[eluser]osci[/eluser]
userguide - errors
userguide - profiling


Trying to get property of non-object - El Forum - 04-24-2011

[eluser]InsiteFX[/eluser]
Code:
// Enable Profiler - Place in your controller!
$this->output->enable_profiler(TRUE);

// Disable Profiler
$this->output->enable_profiler(FALSE);

InsiteFX


Trying to get property of non-object - El Forum - 04-26-2011

[eluser]GesterX[/eluser]
[quote author="InsiteFX" date="1303705029"]
Code:
// Enable Profiler - Place in your controller!
$this->output->enable_profiler(TRUE);

// Disable Profiler
$this->output->enable_profiler(FALSE);

InsiteFX[/quote]

After running the profiler I spotted the error. The spaces in the name were being turned into 's. I cleaned this up with the str_replace php function.

Thanks for your help.