How disable getting data from related model in Yii - activerecord

I don't need to get data from related model.
I have model Post
Also i have model Comment.
Every Post has comments.
I make related between models so:
class Post
public function relations()
{
return array(
'comments' => array(self::HAS_MANY, 'Comment', 'post_id')
);
}
public function scopes()
{
return array(
'orderDesc'=>array(
'order' => 'post_id DESC',
),
);
}
public function findAllPosts()
{
return $this->orderDesc()->findAll();
}
If i get post from db I need comments - no problem.
Post::model()->findByPk()
But if i get all Posts - I don't need comments
Post::model()->findAllPosts()
But i get posts with comments. I think - it's not good for database - use additional joins and it's interesting fo me how disable getting data from related model.
I tried make so through scenario and change behaviour in method relations, but in method relations i got always - $this->scenario is empty.

In Yii the defined relations are by default loaded lazy loading.
It means that Yii will fetch the related models only when you call them in your code.
So if you do
Post::model()->findAll();
The related models (ie: comments) won't be loaded. But if your a calling
Post::model()->findAll();
CVarDumper::dump($posts[0]->comments);
Then a second DB request will be performed to fetch the related comments. This is why the code display the comments.
If you know that you are going to need a related model, the best solution is to use eager loading: it consists in loading the related models in the same request that load the initial model. To do it you need to specify the with method in your code.
Example:
Post::model()->with('comments')->findAll();
This method with can also be put in a defined scope in your model or in the default scope. If it's in the default scope then every time a model is loaded, his related models will be loaded in the same request.
A last note:
When you are using eager loading, one request is performed to fetch the related models, but this technique could not be perfect for every relation.
For example if you have a post and you want to load the author profile, since there is only one author, the request will be fast, returning only one line so the eager loading is good.
But then you want to load the comments. Since it's performing only one request, you'll have severals lines containing a lot of similar informations (all the informations about the post). In this case the pure eager loading is not the best solution.
The best way to handle those relations is to specify in the relations array the params together to false.
If you do so, 2 request will be performed: a first one to fetch the post and a second one to fetch the related comments.
Example:
Post::model()->with('comments' => array('together' => false))->findAll();
Source: Yii Guide

Related

How to use relationship data outside of relationship field in Laravel Nova?

I am trying to figure out how to do something seemingly simple with Laravel Nova, but I can't figure this out.
What I want to do is reference relationship data in a text field. I see in Nova, and understand how to reference a relationship via the HasOne, HasMany ... facades. But what I want to do is get relationship data like this:
Text::make('State', $this->state->name)
This doesn't work, and something I noticed when trying to debug is that each function in a Nova resource seems to run multiple times. Here is the logging I added:
public function fields(Request $request) {
logger($this->state->name)
}
When I do this, there are 3 logging instances, the first 2 containing the state name, and the third not. I think that may have something to do with it not working, but don't know what might be causing this.
Thank you in advance for help!
There's a simple way to get relationship data into a Nova Text field, just use a closure:
Text::make('State', function() {
return $this->state->name;
})
As for the multiple calls to the fields function, the answer has to do with the question: Do you have another Resource in your Nova folder that is related to the Resource we are discussing? If so, that is why -- it needs to call fields to display it properly. You can examine the following query string parameters to get more insight: viaResource, viaResourceId, and viaRelationship.

CakePHP - Render views using elements vs ajax

If views from different controllers use the same element, requiring different controllers to pass the same datas (hence maybe doing the same processing) to the views, wouldn't it be better to make an AJAX call to a single controller?
Let's say we have this:
Model/Post.php
Model/User.php
Model/Service.php
Controllers/UsersController.php
Controllers/ServicesController.php
Views/Users/view.ctp
Views/Services/view.ctp
Views/Elements/list_users_post.ctp
A user belongs to a service, and a user has many posts.
In Views/Services/view.ctp, I want to display a list of each user of a particular service, and for each user, some related infos and a list of his 10 last posts.
In Views/Users/view.ctp, I want to display user's related infos and a list of his 10 lasts posts.
The element Views/Elements/list_users_post.ctp allows me to factor the code displaying a table of a user's posts. It needs the var $userPostList to be set, and to be structured the same as the result of $this->Post->find('all', array('conditions' => array('user_id' => $userId))).
So in my UsersController::view($userId) and ServicesController::view($serviceId) actions, I end up with some duplicated code retrieving users' posts.
I thought to refactor the code so the action ServicesController::view($serviceId) don't make any find on Post model, but instead, the view Services/index.ctp makes AJAX calls to the action UsersController::view($userId) for each user. That way, no more duplicated code, but with the overhead of AJAX calls.
Any thoughts?
A good way to avoid duplicate code in your case is to move it to the model layer. So instead of this in the controller:
$this->Post->find('all', array('conditions' => array('user_id' => $userId)))
You would have this in the controllers:
$this->Post->getUserPosts($userId);
And this in the Post model:
function getUserPosts($userId){
return $this->find('all', array('conditions' => array('user_id' => $userId)));
}
AJAX calls are also a good solution but even with them it would be best to keep the business logic in the models if possible.
Other ways to avoid duplicate code include(but are not limited to):
Using Helpers in Views
Using Components in Controllers
Using Behaviors in Models
That should get you started on keeping your code DRY.

Accessing model from controller?

I want to get data from my database and then in my model I wish to do some php stuff to the data before passing it to my view.
Normally I would do this to get all of my data:
->with('content', Content::all());
But I have set up a function in my model called test:
public function test(){
//get and modify data here
}
How can I access this using:
->with
from my controller?
Is this the one you are looking for?
Model:
public static function foo($bar) {
return static::where('foo', '=', 'bar');
}
Controller:
->with('content', Foo::foo("test"));
What you are trying to do isn't really explained correctly but I'll try to answer anyway.
From what I understand, you want to do some data transformation in the model before it is used. But your comment in the test() function say that you want to GET and modify data.
In laravel, functions for getting and modifying data are separated:
For getting data, you can define a query scope ( http://four.laravel.com/docs/eloquent#query-scopes ). A query scope is usually a shortand for adding some parameters to an existing query.
For modifying data before it is read from the model, or before assinging it to the model, Eloquent provide a system called mutators (http://four.laravel.com/docs/eloquent#accessors-and-mutators ). A mutator is a method called when reading or assiging value to a field. It is usualy to convert php type to a database type (for example array to json on save, then json to array on read). A mutator can be used to populate some extra value or do some extra checks to the value.
I hope I've helped you a bit. If you don't find it is a correct answer, please clarify your question so I can help you a little more.

Loading external data into Ember template using ember-data/DataStore

Here is what I'm tyrnig to do:
Make ajax request to retrieve JSON data from a PHP script
Insert that information into DataStore Models
Store those models within a controller
Display the information using {{#each}} with a handlebars template
Does ember-data have a built in way of retrieving data? If not, where
should the AJAX request be implemented?
What is the best way to insert the JSON data into the DS model?
What is the best way to then sync the models up with a Controller?
Any examples that implement all of the 4 steps would also be very helpful, since I can't seem to find any.
<edit>
Like I said in the comments, this questions asks a lot at once, so to follow up, here's a work in progress fiddle: http://jsfiddle.net/schawaska/dWcUp/
This is not 100%, but covers some of your questions. It uses the FixtureAdapter.
I'll be updating it as I find time.
</edit>
1 Make ajax request to retrieve JSON data from a PHP script
Ember-Data will take care of that for you. Consider the following:
window.App = Em.Application.create();
App.Store = DS.Store.extend({
revision: 12,
adapter: 'DS.RESTAdapter'
});
App.Product = DS.Model.extend({
name: DS.attr('string'),
imageUrl: DS.attr('string')
})
The code above defines a data store (almost like an ORM) within your client app. Ember uses convention over configuration (heavily), so as per configuration this code expects your backend to have a resource in the same domain as /products which talks to GET, POST, PUT and DELETE.
2 Insert that information into DataStore Models
Considering what I said above, by calling one of the following:
App.store.find(App.Product) or App.Product.find()
EmberData will fire a GET request through AJAX targeting your resource /products, and if you say App.Product.find(1), it will target /products/1.
Your app store will use its adapter and the adapter's serializer to translate the JSON result and materialize its data into your App.Product model.
3 Store those models within a controller
This is done when defining your application router. Regardless of what you do, Ember will run its own workflow, but it provides you several hooks, giving you the control of that specific action. Consider the following:
App.Router.map(function() {
this.resource('products');
});
App.ProductsRoute = Ember.Route.extend({
model: function() {
return App.Product.find();
}
});
The code above populates the model used in the products route (which you would access at http://yourdomain.com/#/products). Internally it will generate the code for your ProductsController, or you can define your own, which should extend ArrayController.
Controllers will have a content property which is an alias to the model or model collection. Again, convention over configuration.
4 Display the information using {{#each}} with a handlebars template
Providing you're following the conventions, in your handlebars template, you should iterate through your collection like this:
{{#each product in controller}}
{{#linkTo 'product' product}}
{{product.name}}
{{/linkTo}}
{{/each}}
Does ember-data have a built in way of retrieving data? If not, where
should the AJAX request be implemented?
Yes, simply call App.Product.find() for a product model and it return you a blank ModelArray while firing the AJAX request to the products resource in your backend, then materialize/populate your data into each model once it receives the data back from the server.
What is the best way to insert the JSON data into the DS model?
You shouldn't be concerned about this if you're using ember-data. The framework does that for you in most cases. That's why we love it. You might, however, have to configure mapping, namespace and plurals depending on your backend.
What is the best way to then sync the models up with a Controller?
Something similar to this:
var product = App.Product.createRecord({
name: 'laptop',
imageUrl: 'path/to/image.png'
});
product.save();
This should fire a POST or PUT request to your backend API.
You should definitely check:
http://emberjs.com/guides/
https://peepcode.com/products/emberjs
http://toranbillups.com/blog/archive/2013/01/03/Intro-to-ember-js-and-the-new-router-api/
Making the AJAX request
// Find all pets.
var pets = App.Pet.find();
// Find pet with ID of 1.
var pet = App.Pet.find(1);
Into DataStore Models
pets above will be a DS.RecordArray containing many App.Pet models, whereas pet is just one App.Pet.
Store in Controller
App.IndexRoute = Ember.Route.extend({
model: function() {
return App.Pet.find(4);
}
});
The router is used to setup the controller, and so we specify here that the IndexController should hold one App.Pet with the ID of 4. This can of course be dynamic. Since your controller represents only one model, it should be of the type ObjectController, but if it was used to store many pets, then it should be of the type ArrayController.
By specify the model, you will have access to it in your IndexController and index view (data-template-name="index"). This is because when you move into your index route, the IndexController is located/instantiated, IndexView is instantiated and placed into the DOM, all after consulting the IndexRoute for setting up the controller.
You can now do something like this in your template (although model. is not necessary): {{model.name}}, which will get you your pet's name.
Display using #each
Find all your pets using a modified version of the above code, but returning all of the pets. Remember, this is done by specifying no arguments to your find method:
App.IndexRoute = Ember.Route.extend({
model: function() {
return App.Pet.find();
}
});
Now we can do loop through all of the pets in the index template. Whilst there are many ways to loop, such as including content./model., excluding .content/model, using this, controller, et cetera..., it's not necessary, but that's for another day. What matters at the moment is that this will work for you, and will be the most self-intuitive:
{{#each pet in controller}}
{{pet.name}}
{{/each}}
I'll put together a jsFiddle for this if you would like. Please let me know.
Questions
Does ember-data have a built in way of retrieving data? If not, where
should the AJAX request be implemented?
Yes, that's Ember Data module which has some good guides on EmberJS.com.
What is the best way to insert the JSON data into the DS model?
Using Ember Data as per the examples up above.
What is the best way to then sync the models up with a Controller?
Using the model hook in the appropriate route to specify which model(s) your controller represents.

Form from another model in a view

So I'm trying to extend the Blog tutorial adding some comments:
Post hasMany Comments
I want to display the add comment form in the same view as the 'post view'. Thing is I don't know the best way to get this approach. I thought about three ways:
Creating a function in Comments Controller to handle the data.
Creating a function in Post Controller to handle the data.
Deal with the data in the same function that deals with the post views.
The main problem with the two first 'solutions' is that the validation errors doesn't show up in the form unless I do some messy hacking of saving the invalidated field in a session variable and then parsing the variable on the beforeFilter callback, like this:
function beforeFilter () {
if ($this->Session->check('comment_error')) {
$this->Post->Comment->validationErrors = $this->Session->read('comment_error');
$this->Session->delete('comment_error');
}
}
What I basically do is adapt the invalidated fields to the actual view and allow it to show properly. This works really well, but it seems so ugly to me. What would be the best approach?
Another related question: should a controller reflect a view? I mean on that example, I thought about only having a Comment Model and dealing with all the data in the controller where's the form to add a comment (even though it's in the Post Controller).
Sounds like you're looking for the Mutlivalidatable behaviour: http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model
This allows you to define more than 1 validation ruleset per model. Use your controller to determine which one to apply upon posting something.
P.S. I have only ever used this on a Cake 1.3 project, not sure if it'll work on 2.0.
I see it this way:
Under every post there is an input box "Add comment" with a button to submit.
After submitting some text a form redirects to comments_controller where the comment is saved with this post_id, body, author, date etc.
After the comment is saved and all the logic is done it takes you back to the post.
Under each post there are all related comments displayed (having the same post_id sorted by date or whatever).

Resources