i think i have all 'baked' all my relationships correctly but my 'related' table shows id's instead of values.
my db has 3 tables. candidates, qualificationlookups, qualifications.
qualificationlookups, links candidates to qualifications using the id of the candidate and the id of the qualification.
in my view view, for 'candidates', i have a 'related qualificationlookups' table. (generated by baking a candidate hasmany qualificationlookups and a qualificationlookups belongsto candidate relationship)
in my edit view, for 'qualificationlookups', i can correctly set up the candidates and qualifications fields as dropdowns so i know 'qualificationlookups's relationships are fine.
So how do i ask cakephp to list the name of the qualification (from 'qualifications' table) in the 'related qualificationlookups' table on a candidate's page?
i must be missing something...
could someone please point me in the right direction?
Thanks, Vauneen
Whenever CakePHP automagically fetches lists from your tables, it uses the id key for the value and the $displayField for the text.
If your table has a name or title field, CakePHP automatically displays it as the display field. So, either rename the field that you want as your display field (say, candidate_name to just name) or set the $displayField variable in your model:
class Candidate extends AppModel {
var $displayField = 'candidate_name';
}
HTH.
If there is no other data being stored in the qualificationlookups table, change the relationship to candidates -> HABTM -> qualifications.
To do this, you first need to drop the qualificationlookups table. Create a new table called candidates_qualifications with two indexes, candidate_id and qualification_id.
In your Candidate and Qualification models, define a HABTM Relationship. You do not need to make a new CandidatesQualification Model, cake will do it on the fly.
Related
I have two tables clients and users, users belong to clients. In UsersTable.php I want to set $ this-> setDisplayField ('Clients.name'); but it does not work. What is the way to do this?
You should try to set display field not by classname, but by name of contained entity:
$this->setDisplayField('client.name');
Of course in this case, you should contain Clients model in your query.
$result = $this->Users->find('list')->contain('Clients');
Its possible also in deeper relations.
// model
$this->setDisplayField('client.address.street');
//query
$result = $this->Users->find('list')->contain('Clients.Addresses');
I tested it only in belongsTo relation.
How i do a crud with a relationship between a user and services order? With the code, i can't add users in a team for service order. When cakebake, the controller TeamHasUsersController is created, but the form add don't have any field.
The crud has generated with cake bake.
In service_orders: change requester_users_id into user_id, the icon in front of the field will change to a red key (foreign key). Cake conventions need a foreignkey to be named [model]_id.
The name of table with associations should be "team_users".
Three database tables are required for a BelongsToMany association. In the example above we would need tables for articles, tags and articles_tags. The articles_tags table contains the data that links tags and articles together. The joining table is named after the two tables involved, separated with an underscore by convention. In its simplest form, this table consists of article_id and tag_id.
habtm relationship
Cake newb here.
I have two tables. Users and Events. An user can subscribe to Multiple events.
What is the best way to implement this?
Do I have to create another table and link them or is there any other better approach.
If I do create a new table, how do i link them in cake model?
As said by jQuery.PHP.Magento.com you should use HABTM relationship but the name of the third table should be events_users because the table names should be in alphabetical order.
From the doc:
Table names are in alphabetical order by convention. It is possible to
define a custom table name in association definition.
You should use HABTM relationship.
Reason
See users will subscribe to Multiple events and
One event have multiple users subscribed for.
So this is two way relationship. Therefore you need following tables
users : To store user's data,
events : To store user's data,
events_users : To store Which user joined Which event and Vice versa(Events with n number of users)
So users_events will have 2 fields user_id , event_id , both are foreign keys and here you dont need primary key in HABTM relationship.
I am having trouble understanding how to use ForeignKey and what exactly it does. Does it link two tables? Or does it link specific columns between each table? Here is a very basic example:
class Seller(models.Model):
name = models.CharField()
email = models.CharField()
zipcode = models.CharField()
class ItemsForSale(models.Model):
item = models.CharField()
description = models.CharField()
zipcode = models.ForeignKey(Seller)
What I'm trying to do is when someone fills out a form to sell an item, I want the zipcode for that item to be automatically set as the sellers zipcode (so the end user won't even have to fill out a zipcode in the form).
Why is it ForeignKey(Seller) instead of ForeignKey(Seller.zipcode)? I guess I don't understand am I linking the entire 'Seller' table? Or just the zipcode column?
Since I don't understand how ForeignKey works, I don't know how to write a view method that sets the zipcode for ItemsForSale. so I really think the key to helping me understand all this would be to see a proper view method that sets the ItemForSale's zipcode to the Sellers zipcode.
In Django a ForeignKey is a relationship between Objects.
In a database a ForeignKey is a field (column) in a table that links to a row of another table.
Visit the django docs and wikipedia for more information about ForeignKeys.
Now to your Django example:
Every single Item (ItemsForSale) has a Seller. (Note: Model names should always be singular.)
So we add a ForeignKey in your ItemsForSale-model that relates to the item's seller:
class ItemsForSale(models.Model):
...
seller = models.ForeignKey(Seller)
Now you can access the item's seller (and of course all of his attributes (including his zipcode) this way:
ItemsForSale.get(item='Foo').seller.zipcode
I have two models: Store and Review. Users of my site can leave a review on a store. In my database, I have a join table, reviews_stores that associates a review with a store.
How do I link my models? I assume a Store should haveMany Review, and a Review should belong to a Store, but the join table is causing issues with CakePHP as CakePHP is assuming my reviews table has a column called store_id.
The reason I'm using a join table is because many parts of my site can be reviewed. For example, brands. A new Review record will be created and a record will be inserted into a brands_reviews table to associate the two records.
Any help would be much appreciated.
Why are you not simply using one Review model and a reviews table with a field "foreign_key" and another field "model"? By this you do not need to duplicate tables and inherit or duplicate models. This will also remove the need for a join table.
If you want to continue to use your db design then you'll have to use a HABTM association over the hasMany association. But even in the case you want to keep that jointable, again, you can use the foreign_key/model and simply have one join table and one reviews table.
By the way, your join table review_store does not follow the conventions, it should be reviews_stores. But then it differs to the schema you've used for brands_reviews. ;)
Seems to me it isn't a many-many relationship but a grouped 1-many relationship. Id lose the join tables and simply have an extra table outlining which 'group' the review belongs to. So the review table would have review_id, link_id(the foreign key for the relevant brand or store), review_type_id(foreign key depicting whether the review is for a brand or store and so on). Then the review_type table only needs to have review_type_id, review_type(varchar).
There's no need for all the join tables for each model you can review, simple store the model name itself in a field in the Review table.
Setup your relationship like so:
class Store extends AppModel {
public $hasMany = array(
'Review' => array('conditions' => array('Review.model' => 'Store')
);
}
You can then do this with as many extra models as you like without having to touch the database.