Thursday, August 28, 2014

Re: Basic data fetching

In my opinion:
- 1 set() or 2 set() doesn't matter.
- Doing a manual foreach to create an array for devices list seems to be going against the framework, if there builtin 1 liner for it ..
- also the check your sql_dump debug output of queries, on my example there should be 2 very simple sql like "select * from user id = X" and "select * rom devices where user_id = X"
if you don't use containable you could be fetching unnecessary data from changes table for example.

controller:
$this->set('devices',$this->Customer->Device->find('list',array('conditions' => array('customer_id'=>$customerId))));

view:
<?php echo $this->Form->input('device_id', array('empty' => '-----')); ?>

Andras Kende


On Aug 28, 2014, at 6:36 AM, Tristan Plumley <plumleytristan@gmail.com> wrote:

> Thanks for the reply.
>
> I was actually doing your second example until i decided to build an array manually. The reason was if i just send the entire customer info using find('first') with no recursive, i would get all the data i needed for the form in 1 set(). Is it good practice to send more than 1 set if it can be avoided or it just doesnt matter?
>
>
>
> On Wednesday, 27 August 2014 16:47:33 UTC-4, Andras Kende wrote:
> 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 <plumley...@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+u...@googlegroups.com.
> > To post to this group, send email to cake...@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.

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