Wednesday, May 21, 2014

Re: displaying 2 tables

Also, I'd always recommend trying to avoid having to use $useTable and following the conventions as best you can.


On 21 May 2014 10:30, Stephen S <hellospeakman@gmail.com> wrote:
Hey

Name refers to the name of the model, you may also be interested in $useTable = 'table_name' to specify a table name other than the plural of your model name.

As for referring to two database tables in one view, you don't necessarily need to have two controllers for this but you should have two models. Let assume you have a User model and a UserType model (Where the users table has a field named user_type_id)

In your users model

public $belongsTo = array('UserType');

In your user type model

public $hasMany = array('User');

The query

$users = $this->User->find('all', array('contain' => array('UserType'))); // Assuming recursive is -1 in your AppModel

Then you can access to the UserType like such

foreach($users as $user) {
    echo $user['UserType']['name'];
    echo $user['User']['username'];
}


Hope this helps


On 20 May 2014 23:56, Andrew Barry <jagguy999@gmail.com> wrote:
Hi,
I have just started cakephp and got working an example.

if you look at a hello world example from cakephp then I have these files.
I have a table name called users and looking at the blog tutorial that name users should refer to a database name .

q1)My example works but what is correct? Is the name 'users' a table or databasename?

q2)How do I refer to 2 database tables in the 1 view as do I need another model and controller for every database table I access?

http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html

<?php
class User extends AppModel
{
var $name='User';
}
?>

<?php
class UsersController extends AppController
{
var $name='Users';
function index()
{
}
}
?>

<html>

<?php

/*http://127.0.0.1/cakephp/users/    */
echo "Hello World";
?>
</html>

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.



--
Kind Regards
 Stephen Speakman



--
Kind Regards
 Stephen Speakman

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: