I've created a simple database view in MySQL 5.x

CREATE VIEW users_profile_view AS
SELECT
  users.id,
  users.username,
  DATE( users.last_login ) as last_login,
  users.city,
  users.state,
  users.country,
  users.comments,
  users.theme,
  SUM( collections.owned ) as pieces_in_inventory
FROM users
LEFT OUTER JOIN collections ON collections.user_id = users.id
GROUP BY users.id;

and modeled it in cake.  It, too, is very simple.  Here it is:

<?php
  class UsersProfileView extends AppModel {

    var $name = 'UsersProfileView';
    var $displayField = 'user_id';

  }
?>

Simple.  Straightforward.  In my controller, I'm calling the model
thusly:

<?php
class UsersController extends AppController {

  var $name = 'Users';
  var $uses = array( 'Users', 'UsersProfileView' );

  function usersview() {

    $this->UsersProfileView->recursive = -1;
    $this->set('users',
$this->UsersProfileView->findAll(null,null,null,5,1));

  }
}

Again, simple, straightfroward.  When I set the debugging level high
enough, this is the query that CakePHP executes:

SELECT `UsersProfileView`.`id`, `UsersProfileView`.`username`,
`UsersProfileView`.`last_login`, `UsersProfileView`.`city`,
`UsersProfileView`.`state`, `UsersProfileView`.`country`,
`UsersProfileView`.`comments`, `UsersProfileView`.`theme`,
`UsersProfileView`.`pieces_in_inventory` FROM `users_profile_view` AS
`UsersProfileView` WHERE 1 = 1 LIMIT 5


So far, so good.  However, this is the structure of the result set that
is returned from the fetchAll() method on line 555 of dbo_source.php in
the cake library:

Array
(
    [0] => Array
        (
            [users] => Array
                (
                    [id] => bob
                    [username] => bob
                    [city] => bob
                    [state] => bob
                    [country] => bob
                    [comments] => bob
                    [theme] => bob
                )

            [UsersProfileView] => Array
                (
                    [last_login] => 2002-10-18
                    [pieces_in_inventory] => 1
                )

        )
  )

What's going on here?  If I'm using the UsersProfileView model, why is
there a 'users' key in the result set array?  Perhaps it's because
UsersProfileView is a database view using, in part, the users table?  I
thought that but 'last_login' is also a field in the users table just
as al the other fields that are in the value of the 'users' key in the
array above.  So if there was just some sort of clash where the 2
models had the same fields, then I would have thought that 'last_login'
would be up with the other fields leaving 'pieces_in_inventory' as the
only key in the 'UsersProfileView' array.  But since that didn't
happen, I don't think that this is because of some clash.

So what is causing this?  The 'users' key in the result set is causing
other problems in CakePHP, specifically on line 625 of dbo_source.php.
$classname is resolving to the wrong value.  As a consequence, I'm
getting the following error:

Notice: Undefined property: UsersProfileView::$users in
/cake_framework/cake/libs/model/datasources/dbo_source.php on line 628

Is this just a case where the framework doesn't like to work with
database views?  Has anyone gotten views to work properly?

thnx,
Christoph


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Cake PHP" 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