Hi Guys,
-- I've been trying to get this query going for a couple of days - I wrote some really hacky (non cakey php) code to lookup a 'name' field given the 'id' field that I am getting out of the query - it sort of worked but it was a real mess.
I know that this can be done, and it can be done better than I currently am so I'm striving to learn and do better:
I have many many many models (generated by a cake bake all command) - so I think this might be where my problem lies.
The 'normal' behaviour of my generated view files is what I am trying to replicate: When viewing an index - for example: domain.com//DeviceRepairs I see the column with ID "Repair Types" - on each row instead of just showing the 'ID' ( repair_types_id ) I am seeing an actual textual lookup (the 'name' field from the RepairTypes model).
I am trying to get the name of 'RepairTypes' given an ID I get from a find query - and that's where all my problems start.
Here is my controller sending data out:
public function getRepairsByDevice() {$dev_id = $this->request->data['Device']['id'];$this->loadModel('RepairType');// @TODO - working on this section right now - need to lookup repairtype name from device id ? not working ? */$deviceRepairs = $this->RepairType->DeviceRepairs->find( 'list' , array('conditions' => array('DeviceRepairs.device_id' => $dev_id),'recursive' => 1));$this->set('repairs',$deviceRepairs);$this->layout = 'ajax';}
Which receives a $dev_id by ajax post (verified this has been set ok).
This code sends back an array to the view just containing the .id field from the RepairType(s) model, I want to access the .name field.
Like I say the 'index' action/method of the DeviceRepairs controller shows the forward lookup value of id > name but I cannot seem to replicate it.
I don't really know much about the 'has many' and 'belongs to' relationships, but right now:
RepairType model:
RepairType model:
public $belongsTo = array('DeviceRepairs' => array('className' => 'DeviceRepairs','foreignKey' => 'id','conditions' => '','fields' => '','order' => ''));
DeviceRepair model:
public $belongsTo = array(
'Device' => array(
'className' => 'Device',
'foreignKey' => 'device_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'RepairTypes' => array(
'className' => 'RepairTypes',
'foreignKey' => 'repair_types_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $hasMany = array(
'RepairerPricing' => array(
'className' => 'RepairerPricing',
'foreignKey' => 'device_repair_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
Device model:
public $belongsTo = array('DeviceTypes' => array('className' => 'DeviceTypes','foreignKey' => 'device_types_id','conditions' => '','fields' => '','order' => ''),'Manufacturer' => array('className' => 'Manufacturer','foreignKey' => 'manufacturer_id','conditions' => '','fields' => '','order' => ''));public $hasMany = array('DeviceRepair' => array('className' => 'DeviceRepair','foreignKey' => 'device_id','dependent' => false,'conditions' => '','fields' => '','order' => '','limit' => '','offset' => '','exclusive' => '','finderQuery' => '','counterQuery' => ''));
The controller 'pub' sends data out via ajax (and i've made it myself it wasn't baked):
PubController.php:
<?php
/*primarily used just for front end ajax functions and nothing else */
App::uses('AppController', 'Controller');
App::uses('AppModel', 'Model');
class PubController extends AppController {
public $uses = array('Manufacturer','Devices','RepairTypes','DeviceRepairs');
public function getRepairsByDevice() {$dev_id = $this->request->data['Device']['id'];$this->loadModel('RepairType');// @TODO - working on this section right now - need to lookup repairtype name from device id ? not working ? */$deviceRepairs = $this->RepairType->DeviceRepairs->find( 'list' , array('conditions' => array('DeviceRepairs.device_id' => $dev_id),'recursive' => 1));$this->set('repairs',$deviceRepairs);$this->layout = 'ajax';}
}
Any ideas? - My thought is that this issue stems from an incorrect model relationship but I've tried it all different ways and just cannot seem to correct my problem without resorting to creating extra lookup functions - which doesn't seem logical given the size of this framework.
Thanks for any ideas!
P
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.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment