How to load some associations by default? - cakephp

If I want to get all Articles with comments, I need to do
$articles->find()->contain('Comments')->...
However, I might want to fetch the articles in many places all over my project and in nearly all cases load the comments as well. Currently I need to write a ->contain('Comments') again and again.
Is there a way I can define Comments as contained by default if I don't explicitly say I don't want them to be loaded?

Put it into a behavior into a beforeFind callback.
This way you can attach it to the model, and if needed you could even unload the behavior.

Related

Backbone attributes clone or pass in directly from this?

I need all of the attributes in one model. Is it considered good practice to give them as follows:
this.functionName(this.attributes);
from within the model, i am confused due to what it says in the backbone API under:
http://backbonejs.org/#Model-attributes
it writes:
"to retrieve and munge a copy of the model's attributes, use _.clone(model.attributes) instead."
But I do not follow, should I clone them instead? and if so, why? I do not see any difference in the debugger between the cloned and this.attributes version
Thank you in advance
The keyword here is "munge".
From wiki
Mung or munge is computer jargon for a series of potentially destructive or irrevocable changes to a piece of data or a file.Common munging operations include removing punctuation or html tags, data parsing, filtering, and transformation.
If you simply need a reference to the model attributes, what you're doing is fine, no need to overthink it.
You should clone the attributes when you plan on changing the data and don't want to affect the original attributes.

Is it possible to call entity_view inside a view mode of the same entity?

I'm trying to render a view mode which calls entity_view, both with the same entity. The view mode is different from the view mode called in the entity view function.
I know that a view mode calls entity_view, so it's like calling entity_view inside entity_view (nested), with the same entity, but 2 different view modes. I tried but it doesn't work.
What I want is to know if it's possible, maybe it's something that I'm not doing right. If i'ts impossible I want to know why please :)
Thanks.
If you mean for example render a commerce_product entity, showing other commerce_product entities of the same type, it is possible indeed.
Check the API you use, you will certainly find a function that make a big part of the job.

Reordering Behaviors in CakePHP

I'm writing a plugin which includes a behavior that has a dependency on the ContainableBehavior. In my behavior, I'd like to tweak any query conditions in its beforeFind() callback, but I'm finding that ContainableBehavior::beforeFind() has already been executed so my changes of course are falling on deaf ears, so to speak.
The only solution, as far as I can tell, is to manually change the behavior execution order so that my behavior's beforeFind() method is called before ContainableBehavior::beforeFind(), but I'm having some trouble making that happen.
I don't want to make any assumptions about the apps that may use my plugin so I don't want to create an arbitrary dependency that defines how users have to configure any behaviors they're using. I'd like to just make the necessary adjustment on the fly where I need to make it. What I thought made the most sense is this:
In MyBehavior::setup(), I'm simply trying to detach and reattach the ContainableBehavior so that in the collection of behaviors attached to the parent model, it falls after MyBehavior:
# Assume that a condition checking existence is in place
$model->Behaviors->detach( 'Containable' );
$model->Behaviors->attach( 'Containable' );
If I then dump the list of attached behaviors, I get an array with the attached behaviors ordered the way I want them, but ContainableBehavior::beforeFind() still fires before MyBehavior::beforeFind().
Is there any way to force the execution order of behavior callbacks that isn't a total hack job or that doesn't impose an unrealistic dependency/standard on any apps that may deploy the plugin?
Thanks.

Modify contained models at runtime

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

Django Advantage forms.Form vs forms.ModelForm

There is a question very similar to this but I wanted to ask it in a different way.
I am a very customized guy, but I do like to take shortcuts at times. So here it goes.
I do find these two classes very similar although one "helps" the programmer to write code faster or have less code/repeating code. Connecting Models to Forms sounds like an obvious thing to do. One thing that is not particularly clear in the docs using a ModelForm. What happens if you need to add extra fields that are not in the Model or some way connected to another Model?
I guess you could subclass that out and make it work, but does that really help you save time than just manually doing it with a Form?
So next question may not have a definite answer if I do subclass it out, and use ModelForm. Is ModelForm particularly faster than Form? Does it still use the same Update techniques or is binding significantly faster in one or the other?
If you want a form across two models, you got a couple options:
1) create two modelforms, save each individually when posted, and if one of the two depends on the other (i.e. foreignkey), set that in your view before saving.
2) try Django's inline formset: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-an-inline-formset-in-a-view
3) Add non-model fields to your modelform. On a ModelForm, you can add fields that are not tied to your model. They are available in cleaned_data as any other field would, be but are simply ignored when the model is saved.
One advantage that ModelForm's have over Form's is you can specify the ordering of fields (searching for how to order Form fields brought to your post incidentally). Obvious other advantages are you don't have to rewrite your model saving code

Resources