Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9220017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:15:02+00:00 2026-06-18T03:15:02+00:00

I’m struggling to get my Behavior class to use an object instance in the

  • 0

I’m struggling to get my Behavior class to use an object instance in the callbacks.

class SomethingBehavior extends ModelBehavior
{
     public function setObject($obj)
     {
         // do stuff
     }

     public function afterFind(Model $model,$results,$primary)
     {
        // use the $obj reference set above
     }
}

Now I need the Model class to call setObject(..) before any find operations are performed. So ideally I would just assign the object I need in the constructor.

class Document extends AppModel
{
    //.....
    public function __construct($id,$table,$ids)
    {
        parent::__construct($id,$table,$ds);
        $this->Something->setObject(new MyClass());
    }
}

My problem is that the Behavior object isn’t yet configured, and I get a not an object error when trying to use it.

I can’t find any callback method for Models like in Components. For example, there is no setup or initialize method.

How can I assign the object I need to the Behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:15:03+00:00Added an answer on June 18, 2026 at 3:15 am

    You don’t seem to have worked with behaviors much. Try to use the containable, tree or other core or plugin behaviors, then you will soon figure out the basics.

    First of all, behaviors are attached to models (and since 2.3: loaded), not the other way around. A model then gets “richer” in functionality.

    Either statically be using public $actsAs or dynamically using

    $this->Behaviors->attach('Something'); // since 2.3: load() instead of attach()
    

    It can directly access the behavior methods. Lets say we have a method foo() in your behavior.
    You can then call it from your model as

    $this->foo($foo, $bar);
    

    Or from your controller as

    $this->Document->Behaviors->attach('Something')
    $this->Document->foo($foo, $bar);
    

    Awesome, right?
    The behavior method usually has this declaration:

    public function foo(Model $Model, $foo, $bar) {
        $alias = $Model->alias;
        // do sth
    }
    

    As you can see, you always pass the model into it implicitly (as first argument automatically passed).
    You can access all its attributes.

    And do not touch the constructor of the model. no need to do that.

    If you really need to pass an object in at runtime, why does your approach not work?

    public function setObject(MyClass $obj) {
        $this->Obj = $obj;
    }
    

    Now you can internally use the object from your behavior methods

    public function doSth(Model $Model) {
        $this->Obj->xyz();
    }
    

    Also this might not be the most elegant approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.