I have an Employees table, which consists of data from another source
which is read-only. I have a second table, Profiles, which contains
data that a user can edit. The Employees model hasOne Profile, and the
Profile belongsTo the Employee. Each Profile also belongs to a
Category.
class Employee extends AppModel {
var $name = 'Employee';
var $hasOne = array(
'Profile' => array('className' => 'Profile',
'foreignKey' => 'employee_id')
);
}
class Profile extends AppModel {
var $name = 'Profile';
var $belongsTo = array(
'Employee' => array('className' => 'Employee',
'foreign_key' => 'employee_id'),
'Category' => array('className' => 'Category',
'foreignKey' =>
'category_id'
)
);
}
My issue stems from the fact that not all Employees have a Profile.
data examples:
With profile:
Array
(
[Employee] => Array
(
[id] => 131348
[name] => Jane Smith
)
[Profile] => Array
(
[id] => 1
[employee_id] => 131348
[category_id] => 4
[Category] => Array
(
[id] => 4
[descr] => Full-time
)
)
)
without profile (the hasOne relationship attaches a blank profile
record):
Array
(
[Employee] => Array
(
[id] => 131464
[name] => John Doe
)
[Profile] => Array
(
[id] =>
[employee_id] =>
[category_id] =>
)
)
The data for the Employees table comes, as i said, from an outside
source. Profiles are then added and edited by the users. At first i
had problems referring to the Category description in the Employees
view, since the Profile record which has the category_id does not
exist for some Employees. I got around that by checking in the
employees_controller view function for the existence of a Profile. If
it exists, an Employee view is rendered which has the Profile (and
Category) information in it. In that view an edit button is provided
to edit the profile record. If the profile does not exist, a view is
rendered which doesn't have any profile information, and the 'edit
button' actually refers to the add function in the profiles_controller
(taking the employee.id has an argument).
Like i said, this is working, but it doesn't seem like the most
elegant solution. Am i missing an easier way to handle this situation?
My second issue (which is similar), comes into play when i have the
user run a list of employees. This breaks because, again, not all
employees have information in their profile, so there is no related
category record. I got around this by doing this in the view:
empty($employee['Profile']['category_id'])?'':$employee['Profile']
['Category']['descr']
That is, basically checking for the existence of the category_id and
echoing a blank space if it doesn't exist.
Is what i am doing here my best choice, or is there a better way? I
feel like i may be missing something. I appreciate the feedback.
thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---