I am writing a datasource to access a external web service, this works fine as expected, but after the web service has run, I get the following error:
CakeException: Cannot use modParams with indexes that do not exist.
in /project/lib/Cake/Utility/ObjectCollection.php on line 128
After a little googling, I found this similar question:
What does this error actually mean?
Unfortunately, this didn't help me solve my answer.
I understand that the error is expecting me to remove (or add extra) params, but I don't understand where I am setting these params to edit them.
(answering my own question to help others with the same issue!)
The Issue
The issue I had that in my AppModel, I have set public $actsAs = array('Containable');, I do this so all my models attach the containable behaviour, (I then set recursive to -1 by default for all models, and specify the recursion as required (with the call).
So in this case, my datasource model (which I use to interact with the datasource), had the containable behaviour attached to it (and so CakePHP thought this was correct and processed it as per normal model (which is not the case as this has no relations or database table).
The solution
The solution was to simply add public $actsAs = false; in my datasource model (which removed the inherited containable behaviour).
;)
Related
I have models with the the following relations, defining a situation where users can belong to many groups, and multiple groups can be granted access to a project.
User HABTM Group HABTM Project
I would like to set things up so that any find() done on the Project model will only return results to which the current user has access, based on her group membership.
My first thought is to use the beforeFind() callback to modify the query. However, the two-level association has me stumped. I solved a similar problem (see this question) by rebinding models. However, that was for a custom find method—I don't think that approach will work in a general situation like this where I need to modify arbitrary queries.
Using afterFind() to filter results isn't a good idea because it will confuse pagination (for example) when it doesn't return the right number of records.
Finally, I have a nagging suspicion that I'm trying to re-invent the wheel. The access control I've seen in CakePHP (e.g. Cake ACLs) has been at the controller/action level rather than at the model/record level, but I feel like this should be a solved problem.
Edit: I eventually decided that this was over-complicated and just added a getAccessibleByUser($id) method to my Project model. However, I'm still curious whether it's possible to globally add this kind of restriction to all find() operations. It seems like exactly the sort of thing you'd want to do in beforeFind(), and I suspect (as DavidYell suggests below) that the answer may lie with the Containable behavior.
You should look at the Containable behaviour. If you are using CakePHP 2.x then it comes in the box.
This behaviour allows you to manage the model relations and the data which is returned by them, along with allowing you to pass conditions, such as a group_id into your contain.
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
I'm in the process of building a plugin that includes a behavior and several related models. My goal is to make this as easy as possible for the developer using the behavior. My perfect world has the dev simply attaching the behavior to any relevant models and configuring it.
The behavior interacts directly with one of the models and a hasOne association is being created on the fly, but the other models contain supporting data that is important. What I'd like to do is to have that model pull in its related data by modifying the Containable models.
In short:
MyModel (which actsAs the behavior) gets bound to top level model during the behavior's setup method.
The supporting models are directly associated to the top level model
In MyBehavior::beforeFind, I'd like to ensure that supporting model data is returned without the user having to know to ask for it when calling MyModel::find( ... ).
I haven't found the right keys that will allow me to modify these things at runtime. Maybe it's not even possible given that I want to essentially interact with another behavior (Containable).
Any thoughts would be appreciated.
This code automatically adds some contains to the find before it is run, you just have to make sure that your behavior is attached before the containable behavior or it will not work. The beforeFind callback for a behavior is only run once, so once containable has been called adding something like this does nothing. Took me a while to get it going because of that.
https://github.com/infinitas/infinitas/blob/dev/core/contents/models/behaviors/contentable.php#L65
Im writing ClearCache behavior.
It's purpose is to delete some of custom cache files on every afterSave and afterDelete event of the model.
In order to delete right files i need to know name of controller and the name of action that called ModelWithClearCacheBehavior->save() or ModelWithClearCacheBehavior->delete()
My question is:
How to get those names inside behavior?
There is no an elegant solution about this (at least I don't know it).
You can do it with a Configure::write class for example:
in your AppController's beforeFilter() you can add the following code:
Configure::write('current_controller', $this->name);
Configure::write('current_action', $this->action);
later on in your behavior you can access them with
Configure::read('current_controller');
Configure::read('current_action');
You can access it because you set them before any model iterations.
For sure it's not elegant but it's working.
Not something I've really done anything with, but a brief reading of the book seems to indicate that the model is (or should be) available inside the behaviour -
When creating behavior methods you automatically get passed a reference of the calling model as the first parameter. All other supplied parameters are shifted one place to the right.
You should then be able to access the model via $Model
this is a bit late but for future reference, in cakephp 2.0 can be done this way in a behavior (using CakeRequest)
beforeFind(&$model, $query){
global $Dispatcher;
$request = new CakeRequest();
$request = $Dispatcher->parseParams($request, $additionalParams = array());
pr($request->params->controller);
return $query;
}
I'm having a slight problem that I can't figure out, but should be really simple.
I have the following model structure in my cakePHP (1.3) app:
ProspectiveQuote [hasMany] QuoteUnit [belongsTo] Unit
but in my ProspectiveQuotesController the line:
$this->ProspectiveQuote->QuoteUnit->Unit->find('list');
gives me the following error:
Undefined property: AppModel::$Unit
Of course, it shouldn't be looking at AppModel, it should be looking at QuoteUnit.
If I do $this->ProspectiveQuote->QuoteUnit->find('all') it seems to get results (allbeit without any related model data...) so it obviously finds the QuoteUnit well enough, and I have double-checked its relationship with Unit, and it all seems fine...
Seems like a simple enough problem. From what I can see people with this problem usually have their model names wrong (or plural) but this is not the case here...
What could I be doing wrong?
I would say to double check over the syntax of your model associations to make sure they are correct. Or back them up, and bake out some new models to test with, just to ensure that it's how you expect it.
Another great thing is to grab the DebugKit http://www.ohloh.net/p/cakephp-debugkit Which will help you to see your variables and your sql queries.
As mentioned in Leo's comment I would try and avoid uses() as it puts, or did put in 1.2 a bit of a big overhead onto your stack.
Have you set var $uses = array('ProspectiveQuote','QuoteUnit','Unit'); in your controller? (although there are slightly more efficient ways of doing this) - see http://book.cakephp.org/2.0/en/controllers.html#controller-attributes
If you do this you can access your associated models like:
$this->Unit->find('list');
or
$this->ProspectiveQuote->QuoteUnit->Unit->find('list');
I know which I prefer.
I'm new to PHP and decided to use the cakePHP framework to help me get started.
I can't figure out one thing though, I want to call methods on the RequestHandlerComponent class to update a users last used IP address and other information, I figured the best place to put this would be in the beforeSave() method on the User model.
I can't figure out how to call the getClientIP method though.
The normal code that would otherwise go in the controller doesn't work. Is there another way to call this class if you're in the model and not the controller?
Class Level:
var $components = array('RequestHandler');
And in the function:
$this->data['User']['lastActiveIP'] = $this->RequestHandler->getClientIP();
Gives:
Undefined property: User::$RequestHandler
Call to a member function getClientIP() on a non-object
Components, by design, aren't available to models (without bypassing MVC convention - which you can do, of course). If you chose to force it to be available, look into ClassRegistry::init(). A better solution, I think, would be to employ the RequestHandler component in your controller (where it's meant to be used), set the lastActiveIp value in the controller (exactly as you've shown in your own example code) and pass the entire data array along to the model.
Now your component is being used where it should be and the model gets to remain ignorant about where it gets its data. At the risk of oversimplification, all the model should know is what to do with the data once it arrives; let the controller worry about collecting and packaging the data.
In addition to Rob's answer, maybe it's enough to put a bit of code together yourself that uses the general env('REMOTE_ADDR') or similar variables. Look at the RequestHandler code, it's not doing anything terrifically complicated.
You may even be able to call the component statically, which is slightly better than instantiating it in the model (still in violation of MVC though). Untested, but should work:
App::import('Component', 'RequestHandler');
RequestHandlerComponent::getClientIp();