On 09/22/2009 07:30 PM, Steppio wrote:
> Thanks for the response. Basically its a bit like a shopping cart. I
> need a primary key just to keep each entry into the Product Inquiry
> List seperate, for example some of the lines i have in the database at
> the moment are:
>
> ID     |   User ID   |    Pilid    |    pcode    |   quantity   | ...
> 13         58               1             CM22        1
> 14         58               1             CD01        3
> 15         58               1             CM003      6
> 16         58               1             CM1/3       1
> 17         58               1             CM451      3
> When i want to add a new pil i click on the function newpil(), which
> at the moment does this
> 18         58               2              --             --
> This is what i want, but when i use the function newpil() again i want
> the latest pilid for that user to increment again, i.e.
> 19         58               3              --             --
>
> What you have sent me worked if the Pilid was 1, it added a 1 to the
> 'pilid' in the find query, but once it gets
> to 2 it seems to stop, just keeps being pilid = 2.
>
> I know this is a wierd way to do it but it seemed to work in my
> head :/
>
> Again thanks for the response.
>
> On 22 Sep, 23:06, brian<bally.z...@gmail.com>  wrote:
>    
>> The first param for find() should be one of 'first', 'all', 'list', etc.
>>
>> But, if all you want is a single value, use field():
>>
>> function newpil() {
>>          $pilid = $this->Pil->field(
>>                  'pilid',
>>                  array(
>>                          'Pil.user_id' =>  $this->Session->read('User.id')
>>                  )
>>          ) + 1;
>>
>>          ...
>>
>> }
>>
>> That being said, I wonder if you're heading into other problems here.
>> Do you know that your table will have duplicates for pilid? Because
>> they will only ever be unique for a particular user_id.
>>
>> In fact, I don't know why you're not just using the primary key for
>> this. It's an auto_increment field and is designed for just this
>> purpose--increment the value for new records while also ensuring that
>> it's unique across all records.
>>
>> On Tue, Sep 22, 2009 at 5:50 PM, Steppio<stepisgr...@hotmail.com>  wrote:
>>
>>      
>>> Hi everybody, this ones been doing my head in for some time now, hope
>>> you can help me out, im sure its very simple but i just cant get it
>>> working. Firstly the table structure is like this:
>>>        
>>      
>>> CREATE TABLE pil (
>>>         id int(11) unsigned NOT NULL auto_increment primary key,
>>>         user_id int(11) unsigned not null,
>>>         pilid int(11) unsigned NOT NULL,
>>>         pcode varchar(255),
>>>         quantity text not null,
>>>         created DATETIME,
>>>         modified DATETIME
>>> )ENGINE=InnoDB DEFAULT CHARSET=latin1;
>>>        
>>      
>>> And this is inside my pils_controller.php:
>>>        
>>      
>>> function newpil() {
>>>                 $user_id = $this->Session->read('User.id');
>>>                 $pilid = $this->Pil->find(
>>>                 array(
>>>                 'conditions' =>  array('Pil.user_id' =>  $user_id),
>>>                 'fields' =>  array('Pil.pilid'),
>>>                 'order' =>  array('Pil.pilid DESC'),
>>>                 ));
>>>        
>>      
>>>                 $pilid2 = ($pilid['Pil']['pilid'] + 1);
>>>        
>>      
>>> ...
>>> }
>>>        
>>      
>>> What i want is for the above function to pick out the last pil
>>> (product inquiry list) that the user set-up and to add a 1 to that.
>>>        
>>      
>>> Any ideas where im going wrong? Any help will be greatly appreciated.
>>>        
>>      
>>> Thank you
>>> Ste
>>>        
> >
>    

As Brian said, just add 'first' to your code and that should do the job:

$this->Pil->find('first', array(....


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to