yii use a model related function in a view - methods

I have no reputation to comment on a question, so I am making a new one. But my question is related to another question: How to convert model data objects array to dataProvider
So.... if you read the question, my doubt is:
I need to ask for help to a new thing that this subject rises. I would like to get a function result as the data of a column, but this function is inside the Friend object. Its not possible to do:
array(
'name'=>$model->friends->getAttributeLabel('column_of_friend_model'),
'value'=> $model->friends->takeDataFromFriendModelThatIsActuallyInOtherModel(),
),
Can someone please help!? Thanks a lot!

Related

Laravel - Difference between Model::create and save()

I've come across two different ways of inserting records into the database, the first being:
Model::create([
'field1' => $request['field1'],
'field2' => $request['field2']
]);
and the second way:
$model = new Model;
$model->field1 = $request['field1'];
$model->field2 = $request['field2'];
$model->save();
What is the difference between these two? Are they the same? When should I use one over the other?
Both do the same thing. See the create method code and you will understand. It's just a shorthand.
Sometimes I don't use the create method, for example when I need to set different values upon some condition.
I know this has already been answered. What I've noticed to be the main difference in both are these.
Both does the same thing. It "Saves" data. But create required you to have $fillable in your model. Without defining fillable fields, create will not work. The data will not be passed.
However, Save on the other hand, does not 'require' fillable and can store the data in to the table.
Hope someone else finds this helpful. Happy Coding :)

Ember Sort my Records so the null for a column are retuned first

I want to filter and then sort my ember records such that the null records for a column are returned first. I'm not much familiar with ember and have a rails back ground.
filteredByData = myLeads.filterBy('status', 'open').filterBy('doNotCall', false).filterBy('phoneValid', true)
filtered = filteredByData.sortBy 'last_dialed_at', 'last_name', 'first_name', 'id'
Right now, my records are getting ordered according to the id.
What i want is to implement the NULLS LAST/FIRST from sql or postgres in any query (such a s SELECT * FROM t1 ORDER BY c1 DESC NULLS FIRST here in my sort so i get the records such that the last_dialed_atnull records come first.
I know the same can be implemented in rails like
Foo.order('last_dialed_at DESC NULLS FIRST') but i have no idea for same in Ember
Any help will be appreciated. Thanks in Advance.
P.S: I'll try to create a ember twiddle meanwhile to explain myself better.
Although you can resort to a SortableMixin/ArrayProxy, those are lower-level APIs than you should really need to use. sortBy works fine for a single value, but I generally use computed.sort when I need to sort multiple things. Take a look at the code and link in this answer https://stackoverflow.com/a/46788377/334913 they should get you going.
One of the common ways Ember suggests doing things is to do computed properties for each step (or set of steps) as it makes it far easier to reason about.
So I would do something along the lines of:
filteredLeads: Ember.computed('leads', function() {
return filteredResultsHere;
}),
sortDefinition: [], // put your entries from your SortableMixin here
sortedFilteredLeads: Ember.computed.sort('filteredLeads', 'sortDefinition'),
I solved my issue by using the Ember.SortableMixin instead of sortBy. I ended up with the following code and this worked perfectly for me.
leads = Ember.ArrayProxy.createWithMixins Ember.SortableMixin, content: filteredByData, sortProperties: ['lastDialedAt', 'firstName', 'lastName', 'id'], sortAscending: true
I'm not sure or much aware why this worked and the fundamentals to an extent that i can explain this behaviour. If there's any explanation for same from anyone, please feel free to put your answer below and i'll accept it as the answer.
Thanks

laravel newbie- syntax for making/setting variable in view with a function in the controller

I've started my first project in laravel (following along with the beginner laracasts series). I've successfully made a 'joggers' scope and get_joggers() function in the controller to pass that set of data to a view- great!
So now- I have a view where I pass all the people, and a partial to show different subsets of them, like so:
#include ('partials._people_list', ['person_group' => $joggers]);
My question: On a view page, what's the syntax declare/set a variable to the results of a function in the controller? I would imagine something like this, but since a view page wants HTML I'd at least have to put it in some php tags or something (though there's probably a better way):
$person_group = action('PeopleController#get_joggers');
This seems so painfully basic, but the first 27 laracasts didn't get there and I've been hunting around for while. Thanks in advance for any help!
Update! I talked to a buddy at work, and the answer was to make/set $joggers in the PeopleController, and then pass it all along like so:
public function index()
{
$people = Person::latest('created_at')
->displayable()
->orderBy('last', 'asc', 'first', 'asc')
->get();
$joggers= Person::joggers('created_at')->get();
return view('person.index', compact('people', 'joggers'));
I've returned different types of data before, and it hadn't occurred to me that I could send two different collections of people. Mystery solved :)

How to link objects using objectIds in parse

parse gives this example...
Internally, the Parse framework will store the referred-to object in
just one place, to maintain consistency. You can also link objects
using just their objectIds like so:
var post = new Post();
post.id = "1zEcyElZ80";
myComment.set("parent", post);
say i want to link a new comment to an existing post, how would i go about this? given i have the post objectId.
Ian, unless that target page was edited, your answer is incorrect. As per that page:
A good rule of thumb is, use a pointer for a one-to-one or one-to-many
relationship. Use a relation when you have a many-to-many
relationship.
It would seem there is some fuzziness in this area that many are not happy about.
Like I said in my comment, I prefer to set it up before hand in the parse data browser, but you would want to use a relation between the post and the comment not a pointer as I previously stated.
This is because a pointer should be used for a one-to-one relationship and a relation for a one-to-many relationship as a comment would be to a post.
https://parse.com/questions/pointer-vs-relation
This is in Java using Pointers to build relationship.
ParseObject favorite = new ParseObject("favorite");
myComment.put("parent", ParseObject.createWithoutData("post_id", "1zEcyElZ80"));
myComment.saveInBackground();

cakephp 2.1 view associated data across all add.ctp, edit.ctp, view.ctp

I have an "examinations" table, its consultation_id relates to "consulsations" table, which in turn its consulation_id relates to the "patients" table.
Now, when I am in the add.ctp, edit.ctp or view.ctp of the "Examinations" Views I need to pull the "patients" details in so that some patient info can appear as to who the form pertains to as patient.
I have tried joins. Not to say they dont work. I am new to cakephp and I really need help as to how it will appear within the controller and how the view.ctp will display it.
I thought of elements but they are just .ctp files right?
Please if anyone can help regarding this it would be so appreciated. I've been trying to do this now for a week and I know there is something simple I am dont doing or thinking rights about.
So you just want to pull in related data? Pretty simple.
In your ExaminationsController methods.
$patients = $this->Examination->Consultation->Patient->find('all',
array('conditions'=>array('consultation_id'=>$id,'examination_id'=>$e_id)));
Something similar to this, not quite sure on which id you need to pass, as it will depend on how your models are linked up. http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
However, if your models are linked up properly, you should get this data anyway. If not set your models recursion to be higher.
$this->Model->recursive = 2;

Resources