Wednesday, August 27, 2014

Re: Basic data fetching

Try something like

$this->set('customer info',$this->Customer->find('first',array('recursive' => -1, 'conditions' => array('id'=>$customerId))));
adding 'recursive' => -1 will only get the customer data..

to get e select list for a customer devices
$this->set('customer info',$this->Customer->Device->find('list',array('conditions' => array('customer_id'=>$customerId))));

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

Andras Kende


On Aug 27, 2014, at 12:25 PM, Tristan Plumley <plumleytristan@gmail.com> wrote:

> Hello, I'm fairly new to cakephp and have a few pretty basic questions about fetching data from arrays:
>
> here is what i have set up
>
> Models:
>
> class Customer extends AppModel {
>
>
> public $hasMany = array('Device','Change');
>
> }
>
> class Device extends AppModel {
>
>
> public $belongsTo = 'Customer';
> }
>
>
> class Change extends AppModel {
>
>
> public $belongsTo = 'Customer';
> }
>
>
> now anytime I
> $this->set('customerinfo',$this->Customer->find('first',array('conditions' => array('id'=>$customerId))));
>
> or find('all'), i get everything on that customer, including all devices and all thousands of changes.
>
> One of my questions is how should i manage that relationship since i dont want to pull that much change data just to get a customer ID, name and address?
>
> Have I built my model relationships too simple to allow for me for limiting what is being sent?
>
>
> question #2
>
> doing the above statement, i get the following returned:
>
>
> array(
> 'Customer' => array(
> 'id' => '33',
> 'name' => 'customer#33',
> 'totchngavail' => '5',
> 'totalchanges' => '0'
> ),
> 'Device' => array(
> (int) 0 => array(
> 'id' => '6',
> 'customer_id' => '33',
> 'name' => 'device4',
> 'type' => 'server',
> 'os' => 'aix',
> 'ip' => '123.123.123.123'
> ),
> (int) 1 => array(
> 'id' => '31',
> 'customer_id' => '33',
> 'name' => 'dev2',
> 'type' => 'server',
> 'os' => 'linux',
> 'ip' => '123.123.123.123'
> )
> ),
> 'Change' => array(
> (int) 0 => array(
> 'id' => '2',
> 'customer_id' => '33',
> 'name' => 'change12',
> 'number' => '1234567890'
> ),
> (int) 1 => array(
> 'id' => '4',
> 'customer_id' => '33',
> 'name' => 'change14',
> 'number' => '1234567890'
> )
>
> ///many many more changes
> )
> )
>
>
>
> When i try and retrieve all the devices for that customer in a select box for example i try with:
>
> echo $this->Form->input('device_id',array('label'=> 'Device Name','style' => 'width: 150px;','type' => 'select',
> 'options' => displayDevices($customerinfo)));
>
> and here is the displayDevices function:
>
> function displayDevices($customerinfo){
> //a list 'header' so no device is selected by default
> $var =array(NULL=>'---');
> foreach($customerinfo['Device'] as $device)
> {
> $var+=array($device['id']=>$device['name']);
> }
> return $var;
> };
>
>
> So my question is: shouldnt there be a better way of displaying that select list? maybe by some form of $customerinfo['Device']['name'] right into the form input? When i do this i get an index error. I dont see how i should have to manually build an array to populate the select list when the complete customer array with devices comes in from the controller.
>
>
> thanks in advance for your advice.
>
>
> --
> 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.

--
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: