OK you need to be a bit more specific as I am new to this.
Say I have 2 tables table1 and table2 and each has its own model.
table1 has a 1 to many relationship with table2.
table1 has a id
table2 has the same id as a foreign key.
public $belongsTo = array('Table2');
In my table2 model
public $hasMany = array('Table1');
The query
$everything = $this->Table1->find('all', array('contain' => array('Table2')));
// Assuming recursive is -1 in your AppModel ??????????????? where is this please
Then I can access the data from a view
foreach($everything as $data) {
echo $user['Table1']['name'];
echo $user['Table2'']['username'];
}
Is this correct?
On Wednesday, May 21, 2014 7:30:24 PM UTC+10, Stephen S wrote:
HeyName 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 helpsOn 20 May 2014 23:56, Andrew Barry <jagg...@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+u...@googlegroups.com .
To post to this group, send email to cake...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php .
For more options, visit https://groups.google.com/d/optout .
--
Kind RegardsStephen Speakman
Please consider the environment before printing this email.
This e-mail and any attachments to it (the "Communication") is, unless otherwise stated, confidential, may contain copyright material and is for the use only of the intended recipient. If you receive the Communication in error, please notify the sender immediately by return e-mail, delete the Communication and the return e-mail, and do not read, copy, retransmit or otherwise deal with it. Any views expressed in the Communication are those of the individual sender only, unless expressly stated to be those of National Training and Solutions Provider Pty Ltd ABN 34 123 831 023, or any of its related entities. NTSP does not accept liability in connection with the integrity of or errors in the Communication, computer virus, data corruption, interference or delay arising from or in respect of the Communication.
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:
Post a Comment