0

Possible Duplicate:
Call to a member function on a non-object

What is wrong with this line of code?

$this->Post->saveField('slug', $this->Text->truncate(Inflector::slug($post['Post']['title']),25,array('exact'=>false,'html'=>false,'ending'=>'')));

As it gives me the error: Fatal error: Call to a member function truncate() on a non-object in /nfs/c03/h01/mnt/12345/domains/jazz.com/html/app/Controller/PostsController.php on line 161

Should I be using something else? Like $this->request->data['title'];

Community
  • 1
  • 1
Cameron
  • 27,963
  • 100
  • 281
  • 483
  • `$this->Text` is not an object. That is what the error message says. Everything else matters on what you want to do an how. – hakre Apr 06 '12 at 11:44
  • especially with helpers being used in a controller context. – mark Apr 06 '12 at 11:44
  • This is NOT a duplicate! Just because I'm getting the same error doesn't mean it's fixed with the same answer! In fact closing this would lose the answer below. – Cameron Apr 06 '12 at 12:01

2 Answers2

1

The error means that $this->Text is not an object, and you can't call the truncate (or any other) method on it.

Is Text supposed to be a model? If you want to use several models in a controller you can add it to the uses array:

var $uses =  array('Post','Text');

If it's a component, you should add it to the components array.

You can use the String class if you are looking for the truncate method that TextHelper provides. See the example given for String::truncate here: http://book.cakephp.org/2.0/en/core-utility-libraries/string.html

Joni
  • 108,737
  • 14
  • 143
  • 193
0

just add a condition to check whether $this->Text is available or not

if ($this->Text) {
       $this->Post->saveField('slug', $this->Text->truncate(Inflector::slug($post['Post']['title']),25,array('exact'=>false,'html'=>false,'ending'=>'')));
}
Sandeep Manne
  • 6,030
  • 5
  • 39
  • 55