I'm currently optimizing my application and one thing is selecting only the things from database which are really needed.
For example when I'm retrieving a User object I normally do it like this:
$user = User::where($attribute, '=', $value)->first();
This code will give me back a full user object with all the columns. My users table is pretty big, so I thought it is a good idea to only select the columns which I need like this:
$user = User::where($attribute, '=', $value)->first($columns);
This is also working as expected. But my problem is, as soon as I specify columns, the relationships of the $user variable are not working anymore, because it is no longer an object.
My question: Is it possible to select only a few columns but still be able to use relations like
$user->parents
Thanks!
Try This
$user = User::where($attribute, '=', $value)->select($columns)->first();
Just add all foreign keys for the relationships you want to get. For example, if you want to use parent() relationship, just add the key to the $columns:
array_push($columns, 'parent_id');
After that, you'll be able to use the parent() relationship.
Yes it is possible to load relationship with selected columns but for that you have to select foreign keys of that relationship table.
So here you also have to select the column of user table which is foreign key in parents table and also mention relationship using with(),
For example if you have user_id column in parents table as foreign key then you have to select id column of user while you are mentioning columns array in first() method as below:
$user = User::with('parents')->where($attribute, $value)->first(['id','email','image']); // also select 'id' for foreign key to load relationship
After using above code you can get relationship of parents as: $user->parents, and don't forget to put relationship in your User Model.
If you have foreign key on user table as parent_id which belongs to parents table then you have to select that parent_id column as below code:
$user = User::with('parents')->where($attribute, $value)->first(['parent_id','email','image']);
Related
In a database with natural composite keys we would like to remove the surrogate key (Id) in the database, but not change the REST api too much. (e.g. still cache fields with Id, and get based on an Id)
Then have a view that abstact the difference, it could look like this:
create MyView
as
select key1 + '#' + key2 as Id, Name
and a query like this
select * from MyView where Id = '3#5'
And that would perform very poorly without some kind of hint or different view.
Is there a standard way to help the database resolve this?
I have a contacts table and a contact_category table. I am trying to reflect in table that one contact can belong to several categories.
Is there any recommended design pattern for implementing this? What comes to my mind is just creating a string in an additional field for every contact and concat the categories this contact belongs to.
For ex.:
"cat1,cat3" would mean that a contact belongs to cat1 and cat3
But, isn't there any proper way of designing this?
Generally speaking, a comma-delimited text field with multiple values is a bad idea in database design, in my rarely-humble opinion.
I'd recommend something like this (I code in SQL Server, so that's what my syntax will look like):
Contact
ID -- primary key
-- other contact fields
Category
ID -- primary key
-- other category fields
Contact_Category
Contact_ID -- foreign key to Contact
Category_ID -- foreign key to Category
The above allows you to associate a contact to multiple categories and a category to multiple contacts. Let me know if you have any questions.
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.
Can two primary keys refer to the same attribute in another table for example
customer id from table customer and user id from table user which refer to field id in table billing and i think i can differentiate between them by adding type field.
I see no reason why not but I also see your adding confusion for the next guy that has to work on this. Why not treat them separately with customer ID and User ID as different values? Good luck.
You can, but probably shouldn't. It is generally clearer to have linking tables instead; for example, if you have "customers", "users", and "billing" tables, have "customer_billing" and "user_billing" tables, rather than having a customer-or-user-id and type in the billing table.
A better solution is to have one supertype table containing the key for all users and customers. Reference the supertype table from your billing table.
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.