CakePHP Foreign Key Display Value - php

I know this is probably really easy to do, but I can not figure it out. And I didn't find an example on Google.
I have a table 'statuses' and a table 'events'. 'events'.'statuses_id' connects to 'statuses'.'id'. Now when I make a find('all') on the event model i still get the id i entered into the database. I would like to display the field 'statuses'.'name'.
Could someone help me on how to do this? I know i have to somehow declare the fk-connection in the model, but how?

Use the belongs_to reference ("Event belongsTo Statuses") and cake automatically will bring you both the name and the id

Related

yii relational complex active record task

I'm trying to get used to relational active record but things look too complicated for now.
If it's not difficult please point me in right direction.
I have 4 tables.
Users
userID[pk],userName
Cars
car_id[pk],userID[fk to Users],car_nickname,make_id[fk to Makes],model_id[fk to Models]
Makes
make_id[pk],make_name
Models
model_id[pk],model_name,make_id[fk to Makes]
Now input data is userName,make_name,model_name and task is to get car_nickname from Cars table.
Is this possible using relations or should I do it step by step checking makes,models,users for IDs and then puting all IDs into Cars to get car_nickname ?
You can yous only one Model with reliations to all tables. And create one _form.php in the Views, where there will only fields you need.
Some halpfull information. if you save some data into some table and you need saved data id, you caN use $newid = $model->getPrimaryKey(); and assign to new variable , wich will save into other tables.
I think this is short way to solve problem

Laravel 4 Many to Many update

I'm stuck in a problem and I can't find a solution to this, it's annoying me.
I've two tables, one called contacts and the other one called phonebooks and they are linked with a third table called *contacts_phonebooks*, this is a many-to-many relationship summarize below:
contacts: id (pk)
phonebooks: id (pk)
contacts_phonebooks: contactid (fk), phonebooksid (fk)
Pretty simple and clear, and it works.
I'm using Laravel 4 and Eloquent ORM, everythings works fine when I've to fetch it, insert it and delete it but when I need to update a contact I fail miserably. I've a form that has a number of checkboxes that represent all the phonebooks (every checkbox has phonebook[] as name) so when you check one of those the phonebook id will be saved in the *contacts_phonebooks* with the contact id.
The problem is that this is not true! I mean when I run this code:
$contact = Contact::find($id);
$contact->contact_name = Input::get('newCName');
$contact->contact_surname = Input::get('newCSurname');
$contact->contact_email = Input::get('newCEmail');
$contact->contact_phone = Input::get('newCPhone');
$contact->contact_birth = Input::get('newCDate');
$contact->phonebooks()->sync(Input::get('phonebook'));
if($contact->save())
{
return "TEST DONE?";
}
It deletes every row in *contacts_phonebooks* associated with the contact id and save only the new one checked... This is weird I know, I try to explain it better.
I want to update Mr.x and he actually is in "Stackoverflow" phonebook, I want to add him in "Nerd" phonebook so I click on update and I selected "Nerd", the other one is already selected.
When I update him the system deletes the "Stackoverflow" link and save ONLY the "Nerd" phonebook (with the code above) this things driving me crazy because Laravel 4 Doc says that you should use the sync() method in order to update a many-to-many relationship.
I don't how how to solve it, I hope you will understand what's my problem.
Cheers.
The documentation says "The sync method accepts an array of IDs to place on the pivot table. After this operation is complete, only the IDs in the array will be on the intermediate table for the model:"
So what I think you are probably looking for is attach().
$contact->phonebooks()->attach(Input::get('phonebook'));
Then you will have to use detach() to remove him from the other.
As stated in the docs: The sync method accepts an array of IDs to place on the pivot table.
Your pivot table should be named
contact_phonebook
and it specifies that in Laravel's documentation.

Relation between find('all') and find('first')

I have a test project and I need some help. This is my URL("localhost/ums/relationscls/courseLecturer/1"). When I input "1" on the URL the controller will find student whose id is "1". It goes same with 2 or 3.
I have tables named as
courses
course_name|course_code|course_credit
students
student_name|student_number
lecturers
id|lecturer_name
relationscls
course_id|lecturer_id|student_id
I want to get student's id from the URL and match it from the relationscls table, then fetching course_id and lecturer_id from the same table.
After that, I want to fetch course_name, course_code and credit from the course table with the course_id which we found it from the relationscls table before.
How can I make it possible? Can anyone help, its emergency situation. Thanks.
You're probably looking for CakePHP's Containable Behavior. It lets you do a find then specify which related data you want to automatically retrieve.
You are probably looking to bind and unbind models dynamically. There are models behavior for this.
http://cakebaker.42dh.com/2008/02/02/attaching-and-detaching-model-behaviors-on-the-fly/ might help for you.
You'd probably like to check the HasAndBelongsToMany and 'hasMany-through' model associations in the cookbook, It might prove useful in your case.

Symfony2 / Doctrine model join single table to multiple other tables by related ID and type

I'm trying to work out the model for a fairly simple application, but I haven't been able to find good information regarding my idea for dealing with user comments. I was thinking that I could have a Comment table, with "related ID" and "related type" fields. These fields would be the composite foreign key back to whatever other table I wanted to link to. For example, you could leave a comment about a User, or a Location, or really any other entity. Is this kind of design possible in Symfony2/Doctrine? If so, is there a good example to reference somewhere?
This is what you're looking for http://doctrine-orm.readthedocs.org/en/2.0.x/reference/inheritance-mapping.html
You'll need to use a discriminator field using Single Table Inheritance or Class Table Inheritance

How to make cakePHP retrieve the data represented by a foreign key?

I have a simple database with multiple tables. I can't figure out how to make cakePHP display the values associated with a foreign key in an index view. Or create a view where the fields of my choice (the ones that make sense to users like location name - not location_id can be updated or viewed on a single page).
I have created an example at http://lovecats.cakeapp.com that illustrate the question. If you look at the page and click the "list cats", you will notice that it shows the location_id field from the locations table. You will also notice that when you click "add cats", you must choose a location_id from the locations table. This is the automagic way that cakePHP builds the app. I want this to be the field location_name.
The database is setup so that the table cats has a foreign key called location_id that has a relationship to a table called locations.
This is my problem: I want these pages to display the location_name instead of the location_id. If you want to login to the application, you can go to http://cakeapp.com/sqldesigners/sql/lovecats and the password 'password' to look at the db relationships, etc.
How do I have a page that shows the fields that I want? And is it possible to create a page that updates fields from all of the tables at once?
This is the slice of cake that I have been trying to figure out and this would REALLY get me over a hump. You can download the app and sql from the above url.
Hah, you figured it out correctly! Cake uses the $displayField variable do decide what to..well, display.
If I remember correctly, by default, cake looks for 'title' and 'name' fields, and if those two are not available, it will simply show your primary key field. Luckily, you can override it the way you figured out yourself ;)
I guess you need to echo $cat['Location']['location_name'] in your view.

Categories