Looking up column in related model - cakephp

I have 3 tables. Here are they with their columns:
Result : id, title
Prospect: id, result_id, customer_id
Customer: id, name, address
Result hasMany Prospect.
Prospect belongsTo Customer.
Customer hasMany Prospect.
Everything's ok. If I view the Result, it would display fine along with its related model Prospect.
But in that related model, I'd like to display the customer name instead of customer_id (by looking up). How to do that?
And how to paginate the related model?
Thank you so very much!

add in Prospect belongsTo Result and you will be able to get the info both directions.
if you want to do more crazy things like finding results based on customer name then you might need to read up on the following
linkable behavior
bindModel
adhoc-joins

Related

Database design - Managing documents

I asked a more generic question yesterday and done some work based on this. This question is more to the point in regards to something I am trying to work out.
So my application has a departments table to handle departments. So I am able to make different departments within my application such as Marketing and Finance.
The problem is, I know what departments I need to make, and these will be created beforehand (but I have made it like a CMS so an admin can edit departments etc). With the departments created, I envision something like this
So a user can choose the department from a dropdown (remember, this is after departments are created). When they do this, the document dropdown should populate.
This is my problem, how can I associate specific documents to a department? Each document requires different inputs, so I would imagine they need to be different tables? At the moment I have
But this doesnt really solve my problem whereby I can state that the Marketing department has a Brief document and Overview document.
How could I go about doing this seeing that I do not specifically have a table for each department? Would I need to create one for each department?
Thanks
You can do the following:
If the variety of the document's information you need to store is to large
create a table which has 5 columns:
id,department_id,crated_at, updated_at, property
so for each property you will have a record in the table e.g:
id department_id crated_at updated_at property
1 454 2015-08-20 2015-08-22 x:34
2 454 2015-08-26 2015-08-26 z:234
3 934 2015-08-25 2015-08-26 y:45
This way you won't need table for each document type
EDIT: another option is adding one column for property name and one for it's value
id,department_id,crated_at, updated_at, property_name, property_value

Cake 2 data model for complex habtm relationship - saving and loading

Abstract extra simplified example of issue:
DB structure is (simple version):
orders {
id,
name
}
items {
id,
name
}
items_orders {
order_id,
item_id,
quantity # problem item - many to many tables don't usually have this can't get cake to upport
}
So I have controllers and models for orders and items, they both habtm each other via the joinTable items_orders with the foreign keys specified, and I can load an order and all it's items, or an item and all the orders it is in, and save them, etc, however I can't figure out how to save/recover the quantity.
I am assuming I'll need to create an extra model to handle this, but can't find any documentation or similar posts here that explain how, or if there's an easy shortcut?
Using extra fields in join table is easy and explained in the cookbook.
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model

Cake php model relationships difficulty, esp. hasMany Through

I apologize in advance... I'm a complete noob to cakephp. As such, I'm having great difficulty in getting my models related correctly. To be more specific, I'm having the most trouble with the "hasMany Through" relationship. I've been beating my head against the wall, and scouring the cookbook, but haven't found it as clear as i had hoped.
I'm currently writing a web app with many complex relationships. I'll start with stripped down versions of my tables.
Users:
id, name
Groups:
id, name, department_id
Departments:
id, name, district_id
Districts:
id, name
Roles
id, name
Note that roles are what I call my levels of access(user, admin, superuser).
groups_users
-group_id, user_id
departments_users:
department_id, user_id, role_id
RELATIONSHIPS:
User hasAndBelongsToMany Department
Department hasAndBelongsToMany User
User hasAndBelongsToMany Group
Group hasAndBelongsToMany User
Department hasMany Group
Group belongsTo Department
District hasMany Department
Department belongsTo District
Now, here's where i get a bit more confused.
Since users can be in multiple departments, in multiple districts, as well as have varying roles within each department (e.g. Department admin in DEPT A of DISTRICT A, and District Admin in DEPT B in DISTRICT B), I chose to place the user's level of access in the departments_users table. Since I'm storing more than foreign keys for the named models, ie role_id, this seems like a candidate for the hasMany through relationship, but cake's docs also mention a keepExisting option for the model relationship.
so i tried this, using the example from the cake site. leaving all of the initial datatables and relationships in place, I added a DepartmentPosition model and controller, like the following:
user.php
public $hasMany = array('DepartmentPosition');
department.php
public $hasMany = array('DepartmentPosition');
department_position.php
public $belongsTo = array('User','Department');
I also added a new data table:
department_position:
id, department_id, user_id, role_id
OK, so what i got, likely mistakenly from the cake docs, is that when I add a user to a department, i can include a role_id, and that will all be saved in the department_position data table, if I do saveAll().
I'm not sure if that is at all the right thing to do. I feel more confused now, having written this out, than i did before. lol. I'm very confused as to how to get these relationships correctly set-up. And, still, once I do, I feel i'll have trouble accessing data from associated models. I'm getting errors galore and am unable to retrieve much associated model data. eek. like I said, i'm not the strongest cake person.
Any suggestions, before i have an aneurysm?! lol.
Thanks loads. Any help will be a lot of help!
If a User can have different Role in different Group something is needed in between, HABTM or hasManyThrough table.
Before Cake version 2.1, it was not possible to have HABTM relation with extra colums, (here below role_id). The column would get lost when saving. This is not the case anymore if you set the unique param to keepExisting, but I prefer the hasManyThrough relation over HABTM. I guess it's a question of taste.
User hasMany GroupRole
GroupRole belongsTo User, GroupRole belongsTo Groups, GroupRole belongsTo Role (GroupRole is hasManyThrough table)
Group hasMany GroupRole, Group belongsTo Department
Department hasMany Group, Department belongsTo District
District hasMany Department

CakePHP 2.1 HABTM Not Getting Associated Info

I have a question that's been bugging me all afternoon:
I'm making a guitar gear site, so I am using a gear items table, a user table, and gear-to-user bridge table. That is, a user can own multiple items and items can belong to multiple users. So HABTM.
An item belongs to a brand (e.g. Fender - Stratocaster), so I set up a belongsTo relationship in the item model as well as a HasMany relationship in the brands model. When I check the output in the items controller, the gear and its associated brand's data is all there as it should be.
The user control panel (and similar areas) basically list all of the user's owned items. After setting up the HABTM relationship between users and items, I checked the controller's output. While the item's information and the bridge table information all appeared, the item's associated brand information did not. The results should essentially be a list of items, including brand information, as if it were "where user_id = x". Instead, it seems to only be grabbing the item information and none of its relationships.
Is there something I'm missing or a dumb mistake? Thanks.
Did you consider setting/changing the recursive attribute when performing find() on the User?
The higher recursive is set, the deeper associations will be found.
An alternative might be to use the Containable behavior on User:
$this->User->find('first',array(
'conditions'=>array('User.id'=>1),
'contain' => array('Item'=>array('Brand'))
));
Set the recursive level up or you can use ContainableBehavior

CakePHP search in hasMany relations

I have a model Content which belongsTo Categories, hasMany Publishers, and Publisher belongsTo city.
There is also a search form where someone selects from a drop-down box which category to view and which city.
But how can I combine these two in a single paginate condition? I mean I cannot do something like:
$this->paginate('Content',array('conditions' =>array('Category.id'=>$category,
'City.id'=>$city)));
because to get cities cake performs a different query.
Nor can I do something like:
$this->paginate('Content',array('conditions' =>array('Category.id'=>$category),
'contain'=>array('Publisher.City'=>array('conditions'=>array(City.id'=>$city)))));
because this will search according to category and filter the city results according to $city.
I know I can do something like:
$this->Content->Publisher->City->find(...)
but this will change the output of my paginated data.
What I usually do is write my custom query where I LEFT join all models and filter the results in WHERE. But I wanted to aks if there is a more cake (sic) way!
thanks
i've experienced the same thing when i first try to create a simple search engine with hasmany relationship.
i used multiple $this->find() and assigned it in a variable then on the paginate code i used$this->paginate(array_merge(name of the variables used in $this->find()));.
hope this will help you...
~gio

Resources