Hey that looks correct to me, I probably should have been more specific
over recursive.

By default recursive is switched on, which means when you query a specific
model, it will find associated tables along with it (up to 1 level I
believe), you can change recursive to find more associated tables or none
at all. I find it best practice to turn this off immediately as I like to
specify which models and fields should be queried to be more efficient.

http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive

I do this by adding public $recursive = -1; in my AppModel.php file.

This brings us to contain which is what you should use when you want to
find associated tables in your query (if recursive isn't switched off you
may not need to do this), I forgot to mention you'll likely need to enable
this behaviour (I do this in AppModel).

http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containable


On 22 May 2014 23:53, Andrew Townsley <[email protected]> wrote:

> Hi,
>
> 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.
>
>
> *In my table1 model*
>
> 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:
>
>> 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'];
>> }
>>
>> http://api.cakephp.org/2.5/source-class-Model.html#243-249
>> http://book.cakephp.org/2.0/en/models/associations-
>> linking-models-together.html
>>
>> Hope this helps
>>
>>
>> On 20 May 2014 23:56, Andrew Barry <[email protected]> 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 [email protected].
>>> To post to this group, send email to [email protected].
>>>
>>> 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
>>
>
>  <http://itfutures.edu.au?referer=email>
> *P: (03) 9866 7737*
> *E: [email protected] <[email protected]>*
> *A: Suite 5 / Level 1 / 424 St Kilda Rd, Melbourne, 3004
> <https://www.google.com.au/url?sa=t&rct=j&q=&esrc=s&source=web&cd=15&cad=rja&uact=8&ved=0CKQBEPUXMA4&url=http%3A%2F%2Fmaps.google.com.au%2Fmaps%3Fgl%3Dau%26hq%3Ditfutures%26daddr%3D1%2F424%2BSt%2BKilda%2BRd%2C%2BMelbourne%2BVIC%2B3004%26panel%3D1%26f%3Dd%26fb%3D1%26geocode%3DFdKovv0dMSWkCCnfp02hHWjWajGLbygrlMPvUw%26cid%3D6048267865728511883&ei=hHUYU_abH8-okgXV7oGYDg&usg=AFQjCNEBpgXrGRhNzMxOzPLHRVSQkdpXUQ&bvm=bv.62577051,d.dGI>*
>
>
> *Connect with us: *
>
> <http://plus.google.com/+itfutureseduau> 
> <http://linkedin.com/company/it-futures>
>   <http://youtube.com/user/itfutureseduau>  <http://twitter.com/itfutures>
>   <http://facebook.com/itfutures>
>
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Reply via email to