A HABTM relationship will fetch data differently then a belongsTo  
relationship.
HABTM will get you an array with multiple results, belongsTo just a  
"flat" array.
$habtm = array(
        [0] => array(['id'] =>, ...),
        [1] => array(['id'] =>, ...)
)
$belongsTo = array(['id'] =>, ...)

When you specify multiple associations, you'll have to specify unique  
names for them.
var $belongsTo = array(
        'Owner' => array(...)
);
var $hasAndBelongsToMany = array(
        'Users' => array(...)
);

You shouldn't/can't mix different associations under the same name.  
Your output is screwed up because you're mixing the "flat" array with  
the "multi" array. You can either fix that manually (also see Set  
Class) or just keep it simple and name your associations properly.
BTW, your debug output is virtually impossible to read, use  
debug($array) or pr($array) to spot these kinda things a lot easier.

On 11 Oct 2008, at 23:58, Keith wrote:

>
> Hi Villas,
>
> I tried clearing out the cache and that didn't fix it unfortunately.
>
> I have the max debug level set.  The code appears to be operating as
> programmed.
>
> I tried a simple habtm with belongs_to in a scenario where I only had
> those two models and still got the same results.
>
> I think it's a problem with how CakePHP is creating the array.  The
> first element in the array has no numeric identifier.  If you look
> back at the screenshot in the initial post you'll see that the first
> entry in the array is not [0] but rather is called [id] as it would be
> when there is a belongs_to and not a HABTM as well.
>
> Unless I can figure something out I'm going to go ahead and just
> report it to cakePHP as a bug because there are legit reasons, like
> the one I'm building, where you'd want a habtm with a belongs_to in
> the same model.
>
> - Keith
>
> On Oct 11, 8:14 am, villas <[EMAIL PROTECTED]> wrote:
>> Hi Keith
>>
>> Thanks for the extra info although it still isn't very clear to me
>> from that what's going on.  However,  I would mention:
>> 1. Are you clearing your cache?  You might have old data hanging
>> around.
>> 2. Did you try to set a higher debug level to get more info?
>> 3. Did you try to experiment with Bake?  You can try different
>> scenarios which you should be able to get working without too much
>> sweat.
>>
>> Best regards.
>>
>> On Oct 11, 6:31 am, Keith <[EMAIL PROTECTED]> wrote:
>>
>>> Just as a clarification on why I have it set up this way...
>>
>>> The belongs_to is there because the users that creates the project
>>> becomes the owner.
>>> The has_and_belongs_to_many is there so that if a user wants someone
>>> outside their department to access it they can manually add them.
>>
>>> The problem looks like it's stemming from how cakePHP is handling  
>>> the
>>> array...
>>
>>> for the belongs_to the array keys are based on field names like  
>>> [id] &
>>> [first_name], etc.
>>> for the HABTM array the array keys are sequential as the "related
>>> users" foreach loop expects.
>>
>>> - Keith
>>
>>> On Oct 11, 1:18 am, Keith <[EMAIL PROTECTED]> wrote:
>>
>>>> It looks to me like this is what is happening, but it's very
>>>> strange...
>>
>>>> Even when no users are associated I'm getting 8 blank associated  
>>>> users
>>>> which is exactly how many elements are in the array...
>>
>>>> So...
>>
>>>> It looks like it's taking the first character from each part of the
>>>> first entry in the array and putting it there...
>>>> K = Keith = First Name
>>>> M = Medlin = Last Name
>>>> etc. for each of the fields.
>>
>>>> So...I'm totally confused why the following code would produce  
>>>> that:
>>
>>>> SQL Query:
>>>> SELECT `User`.`id`, `User`.`first_name`, `User`.`last_name`,
>>>> `User`.`email`, `User`.`date_created`, `User`.`last_login`,
>>>> `User`.`username`, `User`.`password`, `ProjectsUser`.`user_id`,
>>>> `ProjectsUser`.`project_id` FROM `users` AS `User` JOIN
>>>> `projects_users` AS `ProjectsUser` ON  
>>>> (`ProjectsUser`.`project_id` = 1
>>>> AND `ProjectsUser`.`user_id` = `User`.`id`) WHERE 1 = 1
>>
>>>> Which returns a normal looking data set in SQL...
>>
>>>> Relevant View Code:
>>>> <?php
>>>>                 $i = 0;
>>>>                 foreach ($project['User'] as $user):
>>>>                         $class = null;
>>>>                         if ($i++ % 2 == 0) {
>>>>                                 $class = ' class="altrow"';
>>>>                         }
>>>>                 ?>
>>>>                 <tr<?php echo $class;?>>
>>>>                         <td><?php echo $user['id'];?></td>
>>>>                         <td><?php echo $user['first_name'];?></td>
>>>>                         <td><?php echo $user['last_name'];?></td>
>>>>                         <td><?php echo $user['email'];?></td>
>>>>                         <td><?php echo $user['date_created'];?></ 
>>>> td>
>>>>                         <td><?php echo $user['last_login'];?></td>
>>>>                         <td><?php echo $user['username'];?></td>
>>>>                         <td><?php echo $user['password'];?></td>
>>
>>>> project['User'] is what you see dumped above the related users  
>>>> table
>>>> so you can see that the array doesn't look strange in any way.  Is
>>>> this possibly a bug that needs reporting to the CakePHP trac?
>>
>>>> - Keith
>>
>>>> On Oct 10, 11:58 pm, Keith <[EMAIL PROTECTED]> wrote:
>>
>>>>> Thanks for the response.
>>
>>>>> The documents table exists but I'm not having any issues with that
>>>>> relationship as it's a simple has_many and belongs_to with my user
>>>>> model.
>>
>>>>> The real issue is why, what appears to be a valid array is  
>>>>> rendering
>>>>> the way it is in the related users table on the view for the  
>>>>> projects
>>>>> page.
>>
>>>>> To get the array that's there I just did a print_r on the $users  
>>>>> array
>>>>> that is automatically generated by CakePHP.  As you can see  
>>>>> there are
>>>>> no missing keys and the hash lines up correctly with the data.
>>
>>>>> The thing I cannot figure out is why I have those garbage rows  
>>>>> in the
>>>>> table.  My database is clean.  It has 3 users with normal looking
>>>>> data.  The projects_users join table is clean with:
>>
>>>>> user_id | project_id
>>>>> 3 | 1
>>>>> 2 | 1
>>
>>>>> The user_id associated with the table has a corresponding user  
>>>>> as you
>>>>> can see in the array.  So I'm not sure why it's chunking up data  
>>>>> that
>>>>> way it is in the related users table.
>>
>>>>> Any ideas?  Or can you think of anything specifically I should  
>>>>> check
>>>>> on outside of running the queries directly in SQL which I've  
>>>>> done and
>>>>> they return the correct data.  I think the problem is in the  
>>>>> view or
>>>>> how the users array is being interpreted possibly.
>>
>>>>> - Keith
>>
>>>>> On Oct 10, 6:48 pm, villas <[EMAIL PROTECTED]> wrote:
>>
>>>>>> Hi Keith
>>
>>>>>> Not sure you've provided enough info here,  and what we see  
>>>>>> looks a
>>>>>> bit confusing.
>>>>>> Is there also a 'documents' table?
>>>>>> Did you set up a join table for the HABTM?
>>>>>> What is the logic of having a Project belonging to a User in  
>>>>>> addition
>>>>>> to using a join table so that many Projects may belong to a User?
>>>>>> Maybe what you have written makes more sense to others,  but it
>>>>>> suggests to me that you probably didn't read the book.cakephp.org
>>>>>> carefully enough.  Have a good look again at the Models section  
>>>>>> and
>>>>>> then post a followup question,  bearing in mind my comments  
>>>>>> above and
>>>>>> we can try our best to assist you better.
>>
>>>>>> Kind regards.
>>
>>>>>> On Oct 10, 9:44 pm, Keith <[EMAIL PROTECTED]> wrote:
>>
>>>>>>> I am running into a strange issue when I've got the following  
>>>>>>> model
>>>>>>> structure:
>>
>>>>>>> User:
>>>>>>> Has_Many: Projects
>>>>>>> HABTM: Projects
>>
>>>>>>> Projects:
>>>>>>> Belongs_To: User
>>>>>>> HABTM: Users
>>
>>>>>>> So...everything works fine.  When I create a new project and  
>>>>>>> select
>>>>>>> multiple users they are indeed associated with the project as  
>>>>>>> is the
>>>>>>> correct user who "owns' the project as per the Belongs_To model
>>>>>>> relationship.
>>
>>>>>>> However...
>>
>>>>>>> In the scaffolded view I get a really strange result that the  
>>>>>>> SQL
>>>>>>> query doesn't actually generate when I run it in SQL...
>>
>>>>>>> I get a really bizarre output in the Related Users.  Here's a
>>>>>>> screenshot:http://www.keithmedlin.com/habtm_error.png
>>
>>>>>>> Any idea on why I've got what looks like a normal array  
>>>>>>> rendering
>>>>>>> pretty garbled?
>>
>>>>>>> I've run the SQL from the debug mode and the output looks  
>>>>>>> exactly like
>>>>>>> I expect it should look with the 2 users Rich & Lisa  
>>>>>>> associated with
>>>>>>> the document properly.
>>
>>>>>>> Thank you in advance for any help you might be able to provide!
> >


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

Reply via email to