Retrieve data from associated database in cakephp










1















I have an Entries table with users_id as a foreign key to the Users table.



I have set the belongsTo association in the EntriesTable like so:



$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);


After that I wrote this in the EntriesController:



$entries = TableRegistry::get('Entries');
$query = $entries->findByTitle($title)
->contain(['Users']);
$entry = $query->first();
$this->set('entry', $entry);


In the VIEW template of the Entries I have to show the username field of the user that wrote the Entry.



I am aware that in previous versions of CakePHP 1, I could just write $entry['User']['username'] to retrieve the username of the User who wrote the Entry. Now that I am using CakePHP 3.6, it doesn't seem to work. How do I perform the same task in CakePHP 3?










share|improve this question






















  • check in your controller debug($entry); Then see what is the data showing.

    – Alimon Karim
    Nov 13 '18 at 5:32











  • Would you add your view method too ?

    – Alimon Karim
    Nov 13 '18 at 5:39















1















I have an Entries table with users_id as a foreign key to the Users table.



I have set the belongsTo association in the EntriesTable like so:



$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);


After that I wrote this in the EntriesController:



$entries = TableRegistry::get('Entries');
$query = $entries->findByTitle($title)
->contain(['Users']);
$entry = $query->first();
$this->set('entry', $entry);


In the VIEW template of the Entries I have to show the username field of the user that wrote the Entry.



I am aware that in previous versions of CakePHP 1, I could just write $entry['User']['username'] to retrieve the username of the User who wrote the Entry. Now that I am using CakePHP 3.6, it doesn't seem to work. How do I perform the same task in CakePHP 3?










share|improve this question






















  • check in your controller debug($entry); Then see what is the data showing.

    – Alimon Karim
    Nov 13 '18 at 5:32











  • Would you add your view method too ?

    – Alimon Karim
    Nov 13 '18 at 5:39













1












1








1








I have an Entries table with users_id as a foreign key to the Users table.



I have set the belongsTo association in the EntriesTable like so:



$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);


After that I wrote this in the EntriesController:



$entries = TableRegistry::get('Entries');
$query = $entries->findByTitle($title)
->contain(['Users']);
$entry = $query->first();
$this->set('entry', $entry);


In the VIEW template of the Entries I have to show the username field of the user that wrote the Entry.



I am aware that in previous versions of CakePHP 1, I could just write $entry['User']['username'] to retrieve the username of the User who wrote the Entry. Now that I am using CakePHP 3.6, it doesn't seem to work. How do I perform the same task in CakePHP 3?










share|improve this question














I have an Entries table with users_id as a foreign key to the Users table.



I have set the belongsTo association in the EntriesTable like so:



$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);


After that I wrote this in the EntriesController:



$entries = TableRegistry::get('Entries');
$query = $entries->findByTitle($title)
->contain(['Users']);
$entry = $query->first();
$this->set('entry', $entry);


In the VIEW template of the Entries I have to show the username field of the user that wrote the Entry.



I am aware that in previous versions of CakePHP 1, I could just write $entry['User']['username'] to retrieve the username of the User who wrote the Entry. Now that I am using CakePHP 3.6, it doesn't seem to work. How do I perform the same task in CakePHP 3?







cakephp cakephp-3.0






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 4:52









JAVAnewbieJAVAnewbie

124




124












  • check in your controller debug($entry); Then see what is the data showing.

    – Alimon Karim
    Nov 13 '18 at 5:32











  • Would you add your view method too ?

    – Alimon Karim
    Nov 13 '18 at 5:39

















  • check in your controller debug($entry); Then see what is the data showing.

    – Alimon Karim
    Nov 13 '18 at 5:32











  • Would you add your view method too ?

    – Alimon Karim
    Nov 13 '18 at 5:39
















check in your controller debug($entry); Then see what is the data showing.

– Alimon Karim
Nov 13 '18 at 5:32





check in your controller debug($entry); Then see what is the data showing.

– Alimon Karim
Nov 13 '18 at 5:32













Would you add your view method too ?

– Alimon Karim
Nov 13 '18 at 5:39





Would you add your view method too ?

– Alimon Karim
Nov 13 '18 at 5:39












2 Answers
2






active

oldest

votes


















1














You can write like below



In controller



public function view($title)

$entry = $this->Entries->findByTitle($title)->contain(['Users']);
$entry = $entry->first();
$this->set('entry', $entry);



OR



public function view($title)

$query = $this->Entries->find('all', [
'where' => ['Entries.title' => $title],
'contain' => ['Users']
]);
$entry = $query->first();

$this->set('entry', $entry);



In view



<?= $entry->user->username ?>





share|improve this answer

























  • thanks! The problem is that the parameter of my view method is not $id, but $title. it seems like get() doesn't work with fields that are not primary key.

    – JAVAnewbie
    Nov 13 '18 at 6:32












  • Thanks I figured how to fix this problem by adding bits to your code: $entries = TableRegistry::get('Entries'); $query = $entries->findByTitle($title); $q = $query->first(); $id = $q->id; $entry = $this->Entries->get($id, [ 'contain' => ['Users'] ]); $this->set('entry', $entry);

    – JAVAnewbie
    Nov 13 '18 at 6:36












  • I updated my ans.

    – Alimon Karim
    Nov 13 '18 at 6:53











  • @Karim Ahh, that is much better than my messy code. thanks

    – JAVAnewbie
    Nov 13 '18 at 14:41


















0














So in cakephp you were loading Model of its controller, which is done as default,you can directly create an find() method in same controller,if you want to find from other table/ model you can do is-



a. $this->Entries->otherTableName()->find()->all();



b. $tableName= $this->loadModel('tableName');
$tableName->find();






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274029%2fretrieve-data-from-associated-database-in-cakephp%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can write like below



    In controller



    public function view($title)

    $entry = $this->Entries->findByTitle($title)->contain(['Users']);
    $entry = $entry->first();
    $this->set('entry', $entry);



    OR



    public function view($title)

    $query = $this->Entries->find('all', [
    'where' => ['Entries.title' => $title],
    'contain' => ['Users']
    ]);
    $entry = $query->first();

    $this->set('entry', $entry);



    In view



    <?= $entry->user->username ?>





    share|improve this answer

























    • thanks! The problem is that the parameter of my view method is not $id, but $title. it seems like get() doesn't work with fields that are not primary key.

      – JAVAnewbie
      Nov 13 '18 at 6:32












    • Thanks I figured how to fix this problem by adding bits to your code: $entries = TableRegistry::get('Entries'); $query = $entries->findByTitle($title); $q = $query->first(); $id = $q->id; $entry = $this->Entries->get($id, [ 'contain' => ['Users'] ]); $this->set('entry', $entry);

      – JAVAnewbie
      Nov 13 '18 at 6:36












    • I updated my ans.

      – Alimon Karim
      Nov 13 '18 at 6:53











    • @Karim Ahh, that is much better than my messy code. thanks

      – JAVAnewbie
      Nov 13 '18 at 14:41















    1














    You can write like below



    In controller



    public function view($title)

    $entry = $this->Entries->findByTitle($title)->contain(['Users']);
    $entry = $entry->first();
    $this->set('entry', $entry);



    OR



    public function view($title)

    $query = $this->Entries->find('all', [
    'where' => ['Entries.title' => $title],
    'contain' => ['Users']
    ]);
    $entry = $query->first();

    $this->set('entry', $entry);



    In view



    <?= $entry->user->username ?>





    share|improve this answer

























    • thanks! The problem is that the parameter of my view method is not $id, but $title. it seems like get() doesn't work with fields that are not primary key.

      – JAVAnewbie
      Nov 13 '18 at 6:32












    • Thanks I figured how to fix this problem by adding bits to your code: $entries = TableRegistry::get('Entries'); $query = $entries->findByTitle($title); $q = $query->first(); $id = $q->id; $entry = $this->Entries->get($id, [ 'contain' => ['Users'] ]); $this->set('entry', $entry);

      – JAVAnewbie
      Nov 13 '18 at 6:36












    • I updated my ans.

      – Alimon Karim
      Nov 13 '18 at 6:53











    • @Karim Ahh, that is much better than my messy code. thanks

      – JAVAnewbie
      Nov 13 '18 at 14:41













    1












    1








    1







    You can write like below



    In controller



    public function view($title)

    $entry = $this->Entries->findByTitle($title)->contain(['Users']);
    $entry = $entry->first();
    $this->set('entry', $entry);



    OR



    public function view($title)

    $query = $this->Entries->find('all', [
    'where' => ['Entries.title' => $title],
    'contain' => ['Users']
    ]);
    $entry = $query->first();

    $this->set('entry', $entry);



    In view



    <?= $entry->user->username ?>





    share|improve this answer















    You can write like below



    In controller



    public function view($title)

    $entry = $this->Entries->findByTitle($title)->contain(['Users']);
    $entry = $entry->first();
    $this->set('entry', $entry);



    OR



    public function view($title)

    $query = $this->Entries->find('all', [
    'where' => ['Entries.title' => $title],
    'contain' => ['Users']
    ]);
    $entry = $query->first();

    $this->set('entry', $entry);



    In view



    <?= $entry->user->username ?>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 7:05

























    answered Nov 13 '18 at 5:42









    Alimon KarimAlimon Karim

    2,25263157




    2,25263157












    • thanks! The problem is that the parameter of my view method is not $id, but $title. it seems like get() doesn't work with fields that are not primary key.

      – JAVAnewbie
      Nov 13 '18 at 6:32












    • Thanks I figured how to fix this problem by adding bits to your code: $entries = TableRegistry::get('Entries'); $query = $entries->findByTitle($title); $q = $query->first(); $id = $q->id; $entry = $this->Entries->get($id, [ 'contain' => ['Users'] ]); $this->set('entry', $entry);

      – JAVAnewbie
      Nov 13 '18 at 6:36












    • I updated my ans.

      – Alimon Karim
      Nov 13 '18 at 6:53











    • @Karim Ahh, that is much better than my messy code. thanks

      – JAVAnewbie
      Nov 13 '18 at 14:41

















    • thanks! The problem is that the parameter of my view method is not $id, but $title. it seems like get() doesn't work with fields that are not primary key.

      – JAVAnewbie
      Nov 13 '18 at 6:32












    • Thanks I figured how to fix this problem by adding bits to your code: $entries = TableRegistry::get('Entries'); $query = $entries->findByTitle($title); $q = $query->first(); $id = $q->id; $entry = $this->Entries->get($id, [ 'contain' => ['Users'] ]); $this->set('entry', $entry);

      – JAVAnewbie
      Nov 13 '18 at 6:36












    • I updated my ans.

      – Alimon Karim
      Nov 13 '18 at 6:53











    • @Karim Ahh, that is much better than my messy code. thanks

      – JAVAnewbie
      Nov 13 '18 at 14:41
















    thanks! The problem is that the parameter of my view method is not $id, but $title. it seems like get() doesn't work with fields that are not primary key.

    – JAVAnewbie
    Nov 13 '18 at 6:32






    thanks! The problem is that the parameter of my view method is not $id, but $title. it seems like get() doesn't work with fields that are not primary key.

    – JAVAnewbie
    Nov 13 '18 at 6:32














    Thanks I figured how to fix this problem by adding bits to your code: $entries = TableRegistry::get('Entries'); $query = $entries->findByTitle($title); $q = $query->first(); $id = $q->id; $entry = $this->Entries->get($id, [ 'contain' => ['Users'] ]); $this->set('entry', $entry);

    – JAVAnewbie
    Nov 13 '18 at 6:36






    Thanks I figured how to fix this problem by adding bits to your code: $entries = TableRegistry::get('Entries'); $query = $entries->findByTitle($title); $q = $query->first(); $id = $q->id; $entry = $this->Entries->get($id, [ 'contain' => ['Users'] ]); $this->set('entry', $entry);

    – JAVAnewbie
    Nov 13 '18 at 6:36














    I updated my ans.

    – Alimon Karim
    Nov 13 '18 at 6:53





    I updated my ans.

    – Alimon Karim
    Nov 13 '18 at 6:53













    @Karim Ahh, that is much better than my messy code. thanks

    – JAVAnewbie
    Nov 13 '18 at 14:41





    @Karim Ahh, that is much better than my messy code. thanks

    – JAVAnewbie
    Nov 13 '18 at 14:41













    0














    So in cakephp you were loading Model of its controller, which is done as default,you can directly create an find() method in same controller,if you want to find from other table/ model you can do is-



    a. $this->Entries->otherTableName()->find()->all();



    b. $tableName= $this->loadModel('tableName');
    $tableName->find();






    share|improve this answer



























      0














      So in cakephp you were loading Model of its controller, which is done as default,you can directly create an find() method in same controller,if you want to find from other table/ model you can do is-



      a. $this->Entries->otherTableName()->find()->all();



      b. $tableName= $this->loadModel('tableName');
      $tableName->find();






      share|improve this answer

























        0












        0








        0







        So in cakephp you were loading Model of its controller, which is done as default,you can directly create an find() method in same controller,if you want to find from other table/ model you can do is-



        a. $this->Entries->otherTableName()->find()->all();



        b. $tableName= $this->loadModel('tableName');
        $tableName->find();






        share|improve this answer













        So in cakephp you were loading Model of its controller, which is done as default,you can directly create an find() method in same controller,if you want to find from other table/ model you can do is-



        a. $this->Entries->otherTableName()->find()->all();



        b. $tableName= $this->loadModel('tableName');
        $tableName->find();







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 9 '18 at 13:36









        kunal rawatkunal rawat

        11




        11



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274029%2fretrieve-data-from-associated-database-in-cakephp%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Darth Vader #20

            Ondo