Friday, May 10, 2013

Find Method On Associated Models

I am having some problems with the find method on a associated model. For some reason my Declinations are populating in policy/view. I have a model named Policy, and I have a model named Declinations that is associated with the Policy model. A policy hasMany Declinations. Now with all of that said, here is what I've got and what I've tried so far. What am I doing wrong or not doing at all. Any input would be greatly appreciated. Thanks!


 Model


     public function getPolicies($conditions = array()) {
$default = array(
'contain' => array(
'Endorsement' => array(
'order' => array(
'Endorsement.endorsement_date' => 'DESC',
'Endorsement.created' => 'DESC'
),
'Document'
),
'Location',
'Declination',
'Company',
'Document'
)
);
$cond = array();
if (array_key_exists('conditions', $conditions) && !empty($conditions['conditions'])) {
$default['conditions'] = $conditions['conditions'];
}
$policies = $this->find('all', $default);
$declination = $this->Declination->find('all');
$states = ClassRegistry::init('State')->states();
if (!empty($policies)) {
foreach ($policies as &$policy) {
$policy['Producing'] = $policy['Filing'] = array();
if (isset($states[$policy['Policy']['producing_id']])) {
$policy['Producing'] = $states[$policy['Policy']['producing_id']];
}
if (isset($states[$policy['Policy']['filing_id']])) {
$policy['Filing'] = $states[$policy['Policy']['filing_id']];
}
}
}
return $policies;
return $declination;
}


Controller

     public function view($id = null) {
$policy = $this->Policy->getPolicy($id);
$group = $this->Policy->Coverage->User->Group->group($this->Auth->user('group_id'));
if (!empty($this->data)) {
$this->redirect(array(
'controller' => 'coverages',
'action' => 'view',
$policy['Policy']['coverage_id']
));
}
$this->set(compact('id', 'policy'));
$this->render('view', null, strtolower($group['name']).'/view');
}



View

     <h4>Declinations</h4>
<?php if (count($policy['Declination'])): ?>
<div class="row">
<?php echo $this->Html->link(
__('Edit', true),
array(
'controller' => 'declinations',
'action' => 'edit',
$coverage['Coverage']['id']
),
array('class'=>'btn fr')
); ?>
</div>

<div class="row-fluid">
<div class="span12">
<?php foreach($policy['Declination'] as $declination): ?>
<div class="span3">
<span class="section-title"><b><?php __($declination['company']); ?></b></span>
<span class="section-note">
<?php if (!empty($declination['other'])): ?>
<?php __($declination['other']); ?>
<?php else: ?>
<?php if (isset($declination['Reason']['name'])): ?>
<?php __($declination['Reason']['name']); ?>
<?php endif; ?>
<?php endif; ?>
</span>
<hr/>
<span class="section-name"><?php __($declination['name']); ?></span>
<span class="block"><?php __($declination['phone_number']); ?></span>
<p><b>Date:</b> <?php __($declination['dated']); ?></p>
</div>
<?php endforeach; ?>
</div>
</div>
<?php else: ?>
<?php $link = $this->Html->link(
__('Add Declinations', true),
array(
'controller' => 'declinations',
'action' => 'add',
print_r($policy)
),
array('class'=>'btn success fr')
); ?>
<p>No declinations for this policy. <?php echo $link; ?></p>
<?php endif; ?>
</section>


In addition to the code above. This method calls the getPolicies method above.


     public function getPolicy($id = null) {
if (!$id) {
return array();
}
$policy = $this->getPolicies(array(
'conditions' => array(
'Policy.id' => $id,
'Policy.is_deleted' => 0
)
));
if (!empty($policy)) {
$policy = $policy[0];
}
return $policy;
}

     

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: