Thursday, April 28, 2011

Re: Some problem on the hasmany relationship

ahh.. okay, let me try then.

Thanks.


Regards

On Apr 28, 7:00 pm, Jeremy Burns | Class Outfit
<jeremybu...@classoutfit.com> wrote:
> That's probably because the first value is the array key and you can't have duplicate array keys (your desired result is impossible in PHP). Imagine it as a select list; each option would have the same value (1), which defeats the object. Cake is (in essence) simply doing a loop, adding each value it finds to the resulting array and that will always replace the last value found (it's always array key '1'); hence you end up with the 'last' record. Would it work for you to switch the columns around? That would give you the three values you need.
>
> Jeremy Burns
> Class Outfit
>
> jeremybu...@classoutfit.comhttp://www.classoutfit.com
>
> On 28 Apr 2011, at 11:13, jackgoh wrote:
>
>
>
> > When use find('list',....), it is:
> > Array
> > (
> >    [1] => 4
> > )
>
> > But the result suppose is :
>
> > Array
> > (
> >    [1] => 7
> >    [1] => 14
> >    [1] => 4
> > )
>
> > It is always get the last record.
>
> > Thanks
>
> > On Apr 28, 3:14 pm, Jeremy Burns | Class Outfit
> > <jeremybu...@classoutfit.com> wrote:
> >> What are the results of find list?
>
> >> Jeremy Burns
> >> Class Outfit
>
> >> jeremybu...@classoutfit.comhttp://www.classoutfit.com
>
> >> On 28 Apr 2011, at 07:19, jackgoh wrote:
>
> >>> Hi Jeremy,
>
> >>> - There is a missing ' before category_id after
> >>> 'associationForeignKey'
> >>> => sorry, my typo in this content only. sorry.
>
> >>> - Your conditions in the find->list look wrong; try 'conditions' =>
> >>> array('stock_id' => $this->Stock->id)
> >>> => The results are the same. both ways are working, but your
> >>> suggestion way is better. ;)
>
> >>> - The convention is to name your joining table CategoryStock (the two
> >>> models are in alphabetical order). Although your arrangement ought to
> >>> work, I generally stay with conventions unless there is a compelling
> >>> reason not to.
> >>> => I changed it as what you suggested. (in Stock model & StockCategory
> >>> model)
>
> >>> - Do you have a model for StockCategory?
> >>> => Yes:
> >>> class CategoryStock extends AppModel
> >>> {
> >>>    var $name = CategoryStocks;
> >>> }
>
> >>> - What happens when you use find 'all' rather than 'list'?
> >>> => i got the correct array!!  But the result look like:
> >>> Array
> >>> (
> >>>    [0] => Array
> >>>        (
> >>>            [CategoryStock] => Array
> >>>                (
> >>>                    [stock_id] => 1
> >>>                    [business_category_id] => 4
> >>>                )
>
> >>>        )
>
> >>>    [1] => Array
> >>>        (
> >>>            [CategoryStock] => Array
> >>>                (
> >>>                    [stock_id] => 1
> >>>                    [business_category_id] => 7
> >>>                )
>
> >>>        )
> >>>    [2] => Array
> >>>        (
> >>>            [CategoryStock] => Array
> >>>                (
> >>>                    [stock_id] => 1
> >>>                    [business_category_id] => 14
> >>>                )
>
> >>>        )
>
> >>> )
>
> >>> - What happens when you die(debug($selectedRecord)) in your controller
> >>> before passing it to your view?
> >>> => same as above result.
>
> >>> Out of interest, what does the resulting SQL look like?
> >>> => (i simplified) :SELECT CategoryStock.* FROM `stock_CategoryStocks`
> >>> AS `CategoryStock` WHERE `stock_id` = 1 , effected rows is 3 , which
> >>> is always correct, when use the find('list'), the effected rows is
> >>> still 3, but debug($selectedRecord) show only 1 record in the array.
> >>> Weird.
>
> >>> Thanks.
>
> >>> Regards
> >>> Jack
>
> >>> On Apr 28, 12:03 pm, Jeremy Burns | Class Outfit
> >>> <jeremybu...@classoutfit.com> wrote:
> >>>> These might be typos but some of the code looks *slightly* odd.
>
> >>>> - There is a missing ' before category_id after 'associationForeignKey'
> >>>> - Your conditions in the find->list look wrong; try 'conditions' => array('stock_id' => $this->Stock->id)
> >>>> - The convention is to name your joining table CategoryStock (the two models are in alphabetical order). Although your arrangement ought to work, I generally stay with conventions unless there is a compelling reason not to.
> >>>> - Do you have a model for StockCategory?
> >>>> - What happens when you use find 'all' rather than 'list'?
> >>>> - What happens when you die(debug($selectedRecord)) in your controller before passing it to your view?
>
> >>>> Out of interest, what does the resulting SQL look like?
>
> >>>> Jeremy Burns
> >>>> Class Outfit
>
> >>>> jeremybu...@classoutfit.comhttp://www.classoutfit.com
>
> >>>> On 27 Apr 2011, at 22:51, jackgoh wrote:
>
> >>>>> Hi cricket,
>
> >>>>> The $this->Stock->id is set correctly. And the SQL is working
> >>>>> perfectly, i can get the correct number of records (which 3 of them:
> >>>>> 4, 7, 14)
>
> >>>>> On Apr 27, 1:53 am, cricket <zijn.digi...@gmail.com> wrote:
> >>>>>> Is $this->Stock->id set to something? What does the SQL look like? Set
> >>>>>> debug to 2 to see it.
>
> >>>>>> On Mon, Apr 25, 2011 at 1:25 PM, jackgoh <kockh...@gmail.com> wrote:
> >>>>>>> Hi,
>
> >>>>>>> I am facing some problem when deal with a realtionship tables, the
> >>>>>>> record is not display :
>
> >>>>>>> // in Stock model:
> >>>>>>>        var $hasAndBelongsToMany = array(
> >>>>>>>                'Category' => array(
> >>>>>>>                        'className' => 'Category',
> >>>>>>>                        'joinTable' => 'category',
> >>>>>>>                        'foreignKey' => 'stock_id',
> >>>>>>>                        'associationForeignKey' => category_id',
> >>>>>>>                        'with' => 'StockCategory',
> >>>>>>>                ),
> >>>>>>>        );
>
> >>>>>>> // in Stock controller:
> >>>>>>> $selectedRecord = $this->Stock->StockCategory->find('list',
> >>>>>>> array( 'fields'=>array('stock_id','category_id'),
> >>>>>>> 'conditions'=>array('stock_id='.$this->Stock->id) ) );
> >>>>>>> $this->set(compact('selectedRecord'));
>
> >>>>>>> I try to copy the SQL by using debug() to phpmysql, and i get 3 lines
> >>>>>>> of records. but when i try to PRINT_R the $selectedRecord, i can only
> >>>>>>> get 1 record, example:
>
> >>>>>>> Array
> >>>>>>> (
> >>>>>>>    [1] => 4
> >>>>>>> )
>
> >>>>>>> Suppose the result have to be :
>
> >>>>>>> Array
> >>>>>>> (
> >>>>>>>    [1] => 4
> >>>>>>>    [1] => 7
> >>>>>>>    [1] => 14
> >>>>>>> )
>
> >>>>>>> Values : 4,7,14 are category_id, [1] is stock_id. There is a table
> >>>>>>> call "category" to store all the category name.
>
> >>>>>>> Please point out whats wrong to my code or logic??
>
> >>>>>>> Thanks
>
> >>>>>>> Best Regards.
>
> >>>>>>> --
> >>>>>>> Our newest site for the community: CakePHP Video Tutorialshttp://tv.cakephp.org
> >>>>>>> Check out the new CakePHP Questions sitehttp://ask.cakephp.organdhelpotherswith their CakePHP related questions.
>
> >>>>>>> To unsubscribe from this group, send email to
> >>>>>>> cake-php+unsubscribe@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php-Hidequotedtext -
>
> >>>>>> - Show quoted text -
>
> >>>>> --
> >>>>> Our newest site for the community: CakePHP Video Tutorialshttp://tv.cakephp.org
> >>>>> Check out the new CakePHP Questions sitehttp://ask.cakephp.organdhelpothers with their CakePHP related questions.
>
> >>>>> To unsubscribe from this group, send email to
> >>>>> cake-php+unsubscribe@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php-Hidequoted text -
>
> >>>> - Show quoted text -
>
> >>> --
> >>> Our newest site for the community: CakePHP Video Tutorialshttp://tv.cakephp.org
> >>> Check out the new CakePHP Questions sitehttp://ask.cakephp.organdhelp others with their CakePHP related questions.
>
> >>> To unsubscribe from this group, send email to
> >>> cake-php+unsubscribe@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php-Hide quoted text -
>
> >> - Show quoted text -
>
> > --
> > Our newest site for the community: CakePHP Video Tutorialshttp://tv.cakephp.org
> > Check out the new CakePHP Questions sitehttp://ask.cakephp.organd help others with their CakePHP related questions.
>
> > To unsubscribe from this group, send email to
> > cake-php+unsubscribe@googlegroups.com For more options, visit this group athttp://groups.google.com/group/cake-php- Hide quoted text -
>
> - Show quoted text -

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: