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 cake-php@googlegroups.com
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