Error: Call to a member function find() on a non-object

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 3065 Views - Last Post: 25 April 2012 - 02:55 PM Rate Topic: -----

#1 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 08:40 AM

I am having a small problem here....I keep getting Call to a member function find() on a non-object error message when I try to debug my Web App. With that said, I think that the problem lies within my naming conventions. Here is my code....




<?php 

class TeamsController extends AppController {

	var $name = 'Team';

	function index() {
		
		
		$this->set('teams', $this->team->find('all'));
	}

	function Welcome() {

	}
}

?>




With that said, I have a model named teams...

Is This A Good Question/Topic? 0
  • +

Replies To: Error: Call to a member function find() on a non-object

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 08:56 AM

Hi,

the line:
$this->set('teams', $this->team->find('all'));



the second argument to set() is the results of a method call(find()) on a property called team. The error you are getting is because the team property is not set to an instance of an object that has a accessible method called find().

For this to work you would need to have a property declared in your class, or in a class yours inherits from, that is set to an instance of an object that has the method find().

What kind of object do you expect $this->team to be?
Was This Post Helpful? 0
  • +
  • -

#3 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 09:02 AM

I expected it to be records from mysql database....I am trying to display them in my view

This post has been edited by BarNunBoi: 25 April 2012 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 09:06 AM

Hi,

Is a team property defined and set in your class or a parent class?
Was This Post Helpful? 0
  • +
  • -

#5 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 09:09 AM

I dont believe so....can you give me an example...and please excuse me Im a self taught noobie!!
Was This Post Helpful? 0
  • +
  • -

#6 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 09:22 AM

Someone just point me in the right direction....I am so stuck right now!!
Was This Post Helpful? -1
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 09:23 AM

You really need to read that CakePHP book thoroughly.

Assuming the model associated with the Teams controller is Team, then to access that model you must use

$this->Team->find('all')

Was This Post Helpful? 0
  • +
  • -

#8 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 09:27 AM

$this->Team->find('all')

[/quote]



My code is the exact same!!
Was This Post Helpful? 0
  • +
  • -

#9 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 09:28 AM

Hi,

Properties are variables that belong to a class or instance of an object. Just like any other variable they can be of different types. In your code you have a property called "name". Code inside a class can access non static properties with the syntax:
$this->property_name



You can set properties like so:
$this->property_name = value



In your code you are treating $this->team as if it was an object of some kind. What kind of object I can't say from the code that you provided. What made you think that using $this->team->find() would do what you wanted?
Was This Post Helpful? 0
  • +
  • -

#10 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 09:37 AM

Actually I was following a tutorial with the exception of a couple more records.

Oh and thanks Jack for the -feedback.....Just go ahead and slam the noobie! Thats just swell!
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 09:44 AM

You got negative rep from me because that post was pointless.

CakePHP adheres to a strict naming convention. If you deviate from that, you're making life more difficult.

Table names are plural
Model names are singular.
Controller names are plural.

Therefore, if your table is named teams, then your model should be Team and your controller should be TeamsController. Your $name variable in the controller should be 'Teams'.
Was This Post Helpful? 1
  • +
  • -

#12 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 09:50 AM

Ok my naming conventions were correct...
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 10:23 AM

So you changed this?

Your $name variable in the controller should be 'Teams'. 

Was This Post Helpful? 0
  • +
  • -

#14 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 10:38 AM

yes I changed it. Not the fix though.......I am going over the documentation right now trying to figure out how to set and instance of an object with the find method....hopefully I will figure this out
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Error: Call to a member function find() on a non-object

Posted 25 April 2012 - 10:54 AM

Post your model code, the full controller code, and the names of the files for these classes.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2